Spaces:
Running
Running
File size: 6,033 Bytes
ac6d4c1 ffe0ae6 65b16be 89c7070 ffe0ae6 cb5876d ffe0ae6 5b4920d cb5876d 7023483 cb5876d c6a2c72 c44a830 c6a2c72 c44a830 c6a2c72 b457150 57e4f69 b457150 cb5876d 0683b76 84c894b 0683b76 cb5876d 3c82b91 7023483 3c82b91 7023483 3c82b91 a612f12 5946634 a612f12 89a2478 a612f12 cb5876d c6a2c72 1d07330 cb5876d 0683b76 c6a2c72 0683b76 cb5876d 1d07330 3a7422a 0d9cdc0 bc08c08 0683b76 ba64d69 066b4d7 cb5876d bd0f1db cb5876d c44a830 a612f12 7023483 c44a830 a612f12 698c7e0 a612f12 7023483 |
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import gc
import csv
import os
import socket
import json
import requests
import huggingface_hub
import re as r
import gradio as gr
import pandas as pd
from urllib.request import urlopen
from huggingface_hub import Repository
from transformers import AutoTokenizer, AutoModelWithLMHead
## connection with HF datasets
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "emotion_detection_dataset"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "emotion_detection_logs.csv"
DATA_FILE = os.path.join("emotion_detection_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/emotion_detection_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="emotion_detection_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
SENTENCES_VALUE = """Raj loves Simran.\nLast year I lost my Dog.\nI bought a new phone!\nShe is scared of cockroaches.\nWow! I was not expecting that.\nShe got mad at him."""
## load model
cwd = os.getcwd()
model_path = os.path.join(cwd)
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
model_base = AutoModelWithLMHead.from_pretrained(model_path)
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
"""
generate emotions of the sentences
"""
def get_emotion(text):
# input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
input_ids = tokenizer.encode(text, return_tensors='pt')
output = model_base.generate(input_ids=input_ids,
max_length=2)
dec = [tokenizer.decode(ids) for ids in output]
label = dec[0]
gc.collect()
return label
def generate_emotion(article):
table = {'Input':[], 'Detected Emotion':[]}
if article.strip():
sen_list = article
sen_list = sen_list.split('\n')
while("" in sen_list):
sen_list.remove("")
sen_list_temp = sen_list[0:]
print(sen_list_temp)
results_dict = []
results = []
for sen in sen_list_temp:
if(sen.strip()):
cur_result = get_emotion(sen)
results.append(cur_result)
results_dict.append(
{
'sentence': sen,
'emotion': cur_result
}
)
table = {'Input':sen_list_temp, 'Detected Emotion':results}
gc.collect()
save_data_and_sendmail(article,results_dict,sen_list, results)
return pd.DataFrame(table)
else:
raise gr.Error("Please enter text in inputbox!!!!")
"""
Save generated details
"""
def save_data_and_sendmail(article,results_dict,sen_list,results):
try:
ip_address= getIP()
print(ip_address)
location = get_location(ip_address)
print(location)
add_csv = [article,results_dict,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_emotion_detection_demo'
# url = 'https://pragnakalpdev35.pythonanywhere.com/HF_space_emotion_detection'
myobj = {"sentences":sen_list,"gen_results":results,"ip_addr":ip_address,'loc':location}
response = requests.post(url, json = myobj)
print("response=-----=",response.status_code)
except Exception as e:
return "Error while sending mail" + str(e)
return "Successfully save data"
"""
UI design for demo using gradio app
"""
inputs = gr.Textbox(value=SENTENCES_VALUE,lines=3, label="Sentences",elem_id="inp_div")
outputs = [gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Input","Detected Emotion"],wrap=True)]
demo = gr.Interface(
generate_emotion,
inputs,
outputs,
title="Emotion Detection",
css=".gradio-container {background-color: lightgray} #inp_div {background-color: #FB3D5;}",
article="""<p style='text-align: center;'>Provide us your <a href="https://www.pragnakalp.com/contact/" target="_blank">feedback</a> on this demo and feel free
to contact us at <a href="mailto:letstalk@pragnakalp.com" target="_blank">letstalk@pragnakalp.com</a> if you want to have your own Emotion Detection system.
We will be happy to serve you for your requirement. 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() |