Spaces:
Running
Running
pragnakalp
commited on
Commit
•
02ce532
1
Parent(s):
84a67cd
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,61 @@ import torch.nn.functional as F
|
|
17 |
from bert_ner_model_loader import Ner
|
18 |
import pandas as pd
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
cwd = os.getcwd()
|
21 |
bert_ner_model = os.path.join(cwd)
|
22 |
Entities_Found =[]
|
23 |
Entity_Types = []
|
24 |
k = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def generate_emotion(article):
|
26 |
text = "Input sentence: "
|
27 |
text += article
|
@@ -31,6 +81,7 @@ def generate_emotion(article):
|
|
31 |
output = model_ner.predict(text)
|
32 |
print(output)
|
33 |
k = 0
|
|
|
34 |
for i in output:
|
35 |
for j in i:
|
36 |
if k == 0:
|
@@ -43,6 +94,29 @@ def generate_emotion(article):
|
|
43 |
return pd.DataFrame(result)
|
44 |
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
inputs=gr.Textbox(lines=10, label="Sentences",elem_id="inp_div")
|
47 |
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Entities Found","Entity Types"])]
|
48 |
|
|
|
17 |
from bert_ner_model_loader import Ner
|
18 |
import pandas as pd
|
19 |
|
20 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
21 |
+
DATASET_NAME = "bert_base_ner"
|
22 |
+
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
|
23 |
+
DATA_FILENAME = "bert_base_ner_logs.csv"
|
24 |
+
DATA_FILE = os.path.join("bert_base_ner_logs", DATA_FILENAME)
|
25 |
+
DATASET_REPO_ID = "pragnakalp/bert_base_ner"
|
26 |
+
print("is none?", HF_TOKEN is None)
|
27 |
+
|
28 |
+
try:
|
29 |
+
hf_hub_download(
|
30 |
+
repo_id=DATASET_REPO_ID,
|
31 |
+
filename=DATA_FILENAME,
|
32 |
+
cache_dir=DATA_DIRNAME,
|
33 |
+
force_filename=DATA_FILENAME
|
34 |
+
)
|
35 |
+
|
36 |
+
except:
|
37 |
+
print("file not found")
|
38 |
+
|
39 |
+
repo = Repository(
|
40 |
+
local_dir="bert_base_ner_logs, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
41 |
+
)
|
42 |
+
|
43 |
cwd = os.getcwd()
|
44 |
bert_ner_model = os.path.join(cwd)
|
45 |
Entities_Found =[]
|
46 |
Entity_Types = []
|
47 |
k = 0
|
48 |
+
def get_device_ip_address():
|
49 |
+
result = {}
|
50 |
+
if os.name == "nt":
|
51 |
+
result = "Running on Windows"
|
52 |
+
hostname = socket.gethostname()
|
53 |
+
ip_address = socket.gethostbyname(hostname)
|
54 |
+
result['ip_addr'] = ip_address
|
55 |
+
result['host'] = hostname
|
56 |
+
print(result)
|
57 |
+
return result
|
58 |
+
elif os.name == "posix":
|
59 |
+
gw = os.popen("ip -4 route show default").read().split()
|
60 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
61 |
+
s.connect((gw[2], 0))
|
62 |
+
ipaddr = s.getsockname()[0]
|
63 |
+
gateway = gw[2]
|
64 |
+
host = socket.gethostname()
|
65 |
+
result['ip_addr'] = ipaddr
|
66 |
+
result['host'] = host
|
67 |
+
print(result)
|
68 |
+
return result
|
69 |
+
else:
|
70 |
+
result['id'] = os.name + " not supported yet."
|
71 |
+
print(result)
|
72 |
+
return result
|
73 |
+
|
74 |
+
|
75 |
def generate_emotion(article):
|
76 |
text = "Input sentence: "
|
77 |
text += article
|
|
|
81 |
output = model_ner.predict(text)
|
82 |
print(output)
|
83 |
k = 0
|
84 |
+
save_data_and_sendmail(article,output):
|
85 |
for i in output:
|
86 |
for j in i:
|
87 |
if k == 0:
|
|
|
94 |
return pd.DataFrame(result)
|
95 |
|
96 |
|
97 |
+
def save_data_and_sendmail(Inputdata,output):
|
98 |
+
try:
|
99 |
+
hostname = {}
|
100 |
+
hostname = get_device_ip_address()
|
101 |
+
# url = 'https://pragnakalpdev35.pythonanywhere.com/HF_space_que_gen'
|
102 |
+
# # url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_question_generator'
|
103 |
+
# myobj = {'article': article,'total_que': num_que,'gen_que':result,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")}
|
104 |
+
# x = requests.post(url, json = myobj)
|
105 |
+
|
106 |
+
ip = hostname.get("ip_addr","")
|
107 |
+
add_csv = [Inputdata,Generate_text,IP]
|
108 |
+
with open(DATA_FILE, "a") as f:
|
109 |
+
writer = csv.writer(f)
|
110 |
+
# write the data
|
111 |
+
writer.writerow(add_csv)
|
112 |
+
commit_url = repo.push_to_hub()
|
113 |
+
print("commit data :",commit_url)
|
114 |
+
return "Successfully save data"
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
return "Error while sending mail" + str(e)
|
118 |
+
|
119 |
+
|
120 |
inputs=gr.Textbox(lines=10, label="Sentences",elem_id="inp_div")
|
121 |
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Entities Found","Entity Types"])]
|
122 |
|