Spaces:
Running
Running
import gradio as gr | |
from datetime import date | |
import json | |
import datetime | |
import smtplib | |
import csv | |
from email.mime.text import MIMEText | |
import requests | |
from transformers import AutoTokenizer, AutoModelWithLMHead | |
import os | |
import numpy as np | |
import json | |
from tqdm import trange | |
import gc | |
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 | |
from urllib.request import urlopen | |
import re as r | |
HF_TOKEN = os.environ.get("HF_TOKEN") | |
DATASET_NAME = "bert_based_ner_dataset" | |
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_based_ner_dataset" | |
print("is none?", HF_TOKEN is None) | |
input_value = "The U.S. President Donald Trump came to visit Ahmedabad first time at Motera Stadium with our Prime Minister Narendra Modi in February 2020" | |
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(): | |
# if os.name == "nt": | |
# result = "Running on Windows" | |
# hostname = socket.gethostname() | |
# ip_address = socket.gethostbyname(hostname) | |
# return ip_address | |
# 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)) | |
# ip_address = s.getsockname()[0] | |
# gateway = gw[2] | |
# host = socket.gethostname() | |
# return ip_address | |
# else: | |
# result['id'] = os.name + " not supported yet." | |
# print(result) | |
# return result | |
# def get_location(ip_address): | |
# ip=ip_address | |
# # ip=str(request.remote_addr) | |
# req_data={ | |
# "ip":ip, | |
# "token":"pkml123" | |
# } | |
# url = "https://demos.pragnakalp.com/get-ip-location" | |
# # req_data=json.dumps(req_data) | |
# # print("req_data",req_data) | |
# headers = {'Content-Type': 'application/json'} | |
# response = requests.request("POST", url, headers=headers, data=json.dumps(req_data)) | |
# response = response.json() | |
# print("response======>>",response) | |
# return response | |
# def get_ip(): | |
# response = requests.get('https://api64.ipify.org?format=json').json() | |
# return response["ip"] | |
# def get_location(ip_addr): | |
# ip_address = ip_addr | |
# response = requests.get(f'https://ipapi.co/{ip_address}/json/').json() | |
# location_data = { | |
# "ip": ip_address, | |
# "city": response.get("city"), | |
# "region": response.get("region"), | |
# "country": response.get("country_name") | |
# } | |
# return location_data | |
def getIP(): | |
d = str(urlopen('http://checkip.dyndns.com/') | |
.read()) | |
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1) | |
def get_location(ip_addr): | |
ip=ip_addr | |
req_data={ | |
"ip":ip, | |
"token":"pkml123" | |
} | |
url = "https://demos.pragnakalp.com/get-ip-location" | |
# req_data=json.dumps(req_data) | |
# print("req_data",req_data) | |
headers = {'Content-Type': 'application/json'} | |
response = requests.request("POST", url, headers=headers, data=json.dumps(req_data)) | |
response = response.json() | |
print("response======>>",response) | |
return response | |
def generate_ner(article): | |
result = {'Entities Found':[], 'Entity Types':[]} | |
if article.strip(): | |
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) | |
else: | |
raise gr.Error("Please enter text in inputbox!!!!") | |
def save_data_and_sendmail(article,output): | |
try: | |
print("welcome") | |
ip_address = '' | |
ip_address= getIP() | |
print(ip_address) | |
location = get_location(ip_address) | |
print(location) | |
add_csv = [article,output,ip_address,location] | |
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) | |
# 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) | |
url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_bert_base_ner' | |
myobj = {'article': article,'gen_text':output,'ip_addr':ip_address,"location":location} | |
x = requests.post(url, json = myobj) | |
return "Successfully save data" | |
except Exception as e: | |
print("error") | |
return "Error while sending mail" + str(e) | |
input=gr.Textbox(lines=3, value=input_value, label="Input Text") | |
output = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(2, "fixed"), headers=["Entities Found","Entity Types"], lable="Here is the result"),wrap=True] | |
# with gr.Blocks(css=".gradio-container {background-color: lightgray}") as demo: | |
# gr.Markdown("<h1 style='text-align: center;'>"+ "Named Entity Recognition Using BERT" + "</h1><br/><br/>") | |
# with gr.Row(): | |
# with gr.Column(): | |
# input=gr.Textbox(lines=5, value=input_value, label="Input Text") | |
# sub_btn = gr.Button("Submit") | |
# output = gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), headers=["Entities Found","Entity Types"]) | |
# gr.Markdown( | |
# """ | |
# <p style='text-align: center;'>Feel free to give us your <a href="https://www.pragnakalp.com/contact/"> feedback </a> on this NER demo. | |
# For all your Named Entity Recognition related requirements, we are here to help you.<br /> | |
# Email us your requirement at <a href="mailto:letstalk@pragnakalp.com"> letstalk@pragnakalp.com </a>. | |
# And don't forget to check out more interesting <a href="https://www.pragnakalp.com/services/natural-language-processing-services/">NLP services</a> we are offering.<br/> | |
# <b>Developed by</b> : <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs </a></p> | |
# """) | |
# event = sub_btn.click(generate_emotion, inputs=input, outputs=output) | |
# demo.launch() | |
demo = gr.Interface( | |
generate_ner, | |
input, | |
output, | |
title="Named Entity Recognition Using BERT", | |
css=".gradio-container {background-color: lightgray} #inp_div {background-color: #7FB3D5;", | |
article="""<p style='text-align: center;'>Feel free to give us your <a href="https://www.pragnakalp.com/contact/" target="_blank">feedback</a> on this NER demo. | |
For all your Named Entity Recognition related requirements, we are here to help you. Email us your requirement at | |
<a href="mailto:letstalk@pragnakalp.com" target="_blank">letstalk@pragnakalp.com</a>. And don't forget to check out more interesting | |
<a href="https://www.pragnakalp.com/services/natural-language-processing-services/" target="_blank">NLP services</a> we are offering. | |
<p style='text-align: center;'>Developed by :<a href="https://www.pragnakalp.com" target="_blank"> Pragnakalp Techlabs</a></p>""" | |
) | |
demo.launch() |