File size: 4,168 Bytes
3e0cc3d
 
 
 
 
 
 
 
 
 
 
 
6520201
3e0cc3d
 
 
 
 
be6f283
 
c9ea1f0
3e0cc3d
02ce532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb87008
02ce532
 
3e0cc3d
 
 
 
 
02ce532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e0cc3d
 
 
 
 
 
 
 
 
89475a9
 
83bfa74
3e0cc3d
 
 
 
 
 
 
 
365d35c
89475a9
3e0cc3d
 
 
89475a9
02ce532
e6dacc6
02ce532
 
 
 
 
 
89475a9
02ce532
365d35c
e6dacc6
02ce532
 
 
 
 
 
 
 
 
 
 
 
3e0cc3d
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import gradio as gr
from datetime import date
import json
import csv
import datetime
import smtplib
from email.mime.text import MIMEText
import requests
from transformers import AutoTokenizer, AutoModelWithLMHead 
import gc
import os
import numpy as np
import json
from tqdm import trange
import torch
import torch.nn.functional as F
from bert_ner_model_loader import Ner
import pandas as pd
from huggingface_hub import Repository
import huggingface_hub
import socket

HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "bert_base_ner"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "bert_base_ner_logs.csv"
DATA_FILE = os.path.join("bert_base_ner_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/bert_base_ner"
print("is none?", HF_TOKEN is None)

try:
    hf_hub_download(
        repo_id=DATASET_REPO_ID,
        filename=DATA_FILENAME,
        cache_dir=DATA_DIRNAME,
        force_filename=DATA_FILENAME
    )
    
except:
    print("file not found")

repo = Repository(
    local_dir="bert_base_ner_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)

cwd = os.getcwd()
bert_ner_model = os.path.join(cwd)
Entities_Found =[]
Entity_Types = []
k = 0
def get_device_ip_address():
    result = {}
    if os.name == "nt":
        result = "Running on Windows"
        hostname = socket.gethostname()
        ip_address = socket.gethostbyname(hostname)
        result['ip_addr'] = ip_address
        result['host'] = hostname
        print(result)
        return result
    elif os.name == "posix":
        gw = os.popen("ip -4 route show default").read().split()
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((gw[2], 0))
        ipaddr = s.getsockname()[0]
        gateway = gw[2]
        host = socket.gethostname()
        result['ip_addr'] = ipaddr
        result['host'] = host
        print(result)
        return result
    else:
        result['id'] = os.name + " not supported yet."
        print(result)
        return result


def generate_emotion(article):
    text = "Input sentence: "
    text += article
    
    model_ner = Ner(bert_ner_model)
    
    output = model_ner.predict(text)
    print(output)
    k = 0
    Entities_Found.clear()
    Entity_Types.clear()
    save_data_and_sendmail(article,output)
    for i in output:
        for j in i:
            if k == 0:
                Entities_Found.append(j)
                k += 1
            else:
                Entity_Types.append(j)
                k = 0
    result = {'Entities Found':Entities_Found, 'Entity Types':Entity_Types}  
    
    return pd.DataFrame(result)


def save_data_and_sendmail(article,output):
    try:
        print(">>"*30)
        hostname = {}
        hostname = get_device_ip_address()
        # url = 'https://pragnakalpdev35.pythonanywhere.com/HF_space_que_gen'
        # # url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_question_generator'
        # myobj = {'article': article,'total_que': num_que,'gen_que':result,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")}
        # x = requests.post(url, json = myobj) 
        inputdata = article
        ip = hostname.get("ip_addr","")
        print(ip)
        add_csv = [inputdata,output,ip]
        with open(DATA_FILE, "a") as f:
            writer = csv.writer(f)
            # write the data
            writer.writerow(add_csv)
            commit_url = repo.push_to_hub()
            print("commit data   :",commit_url)
        return "Successfully save data"
    
    except Exception as e:
        return "Error while sending mail" + str(e)
        

inputs=gr.Textbox(lines=10, label="Sentences",elem_id="inp_div")
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Entities Found","Entity Types"])]

demo = gr.Interface(
    generate_emotion,
    inputs,
    outputs,
    title="Entity Recognition For Input Text",
    description="Feel free to give your feedback", 
    css=".gradio-container {background-color: lightgray} #inp_div {background-color: [#7](https://www1.example.com/issues/7)FB3D5;"
)
demo.launch()