File size: 5,496 Bytes
2c61a5c
 
 
 
caa8a51
2c61a5c
 
6acdbac
2c61a5c
 
 
715c74b
 
 
 
2c61a5c
 
 
 
 
 
 
 
a52a0db
 
2c61a5c
 
 
 
 
 
715c74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c61a5c
715c74b
 
 
 
 
 
 
 
 
2c61a5c
715c74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6368b03
 
715c74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0407bc0
715c74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8389f25
 
 
715c74b
 
 
 
 
 
 
0407bc0
715c74b
2c61a5c
 
 
 
 
715c74b
 
 
 
7b1df5d
715c74b
7b1df5d
2c61a5c
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import gradio as gr
from datetime import date
import csv
import datetime
import json
import smtplib
import requests
from email.mime.text import MIMEText
from transformers import AutoTokenizer, AutoModelWithLMHead 
import gc
import os
import re as r
from urllib.request import urlopen
import huggingface_hub
from huggingface_hub import Repository
import json
import numpy as np
from tqdm import trange
import torch
import torch.nn.functional as F
# from bert_ner_model_loader import biobert_model
from biobert_utils import *
import pandas as pd
import nltk
nltk.download('punkt')

cwd = os.getcwd()
bio_bert_ner_model = os.path.join(cwd)
Entities_Found =[]
Entity_Types = []
k = 0
input_value = "This expression of NT-3 in supporting cells in embryos and neonates may even preserve in Brn3c null mutants the numerous spiral sensory neurons in the apex of 8-day old animals."

HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "biobert_based_ner_dataset"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "biobert_base_ner_logs.csv"
DATA_FILE = os.path.join("biobert_base_ner_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/biobert_based_ner_dataset"
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="biobert_base_ner_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)

def getIP():
    ip_address = ''
    try:
    	d = str(urlopen('http://checkip.dyndns.com/')
    			.read())
    
    	return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
    except Exception as e:
        print("Error while getting IP address -->",e)
        return ip_address

def get_location(ip_addr):
    location = {}
    try:
        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
    except Exception as e:
        print("Error while getting location -->",e)
        return location
        
def generate_emotion(article):
    if article.strip():
        Entities_Found.clear()
        Entity_Types.clear()
        text = "Input sentence: "
        text += article
        
        biobert_model = BIOBERT_Ner(bio_bert_ner_model)
        
        output = biobert_model.predict(text)
        print(output)
        k = 0
        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}
        save_data_and_sendmail(article,output)
        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://pragnakalpdev33.pythonanywhere.com/HF_space_biobert_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)

inputs=gr.Textbox(lines=3, label="Input Text",elem_id="inp_div",value=input_value)
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(2, "fixed"), label="Entity Recognition For Input Text", headers=["Word","Entities found"],wrap=True)]

demo = gr.Interface(
    generate_emotion,
    inputs,
    outputs,
    title="Named Entity Recognition Using BIOBERT",
    css=".gradio-container {background-color: lightgray} #inp_div {background-color: [#7](https://www1.example.com/issues/7)FB3D5;",
    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>
                <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
)
demo.launch()