pragnakalp's picture
Update app.py
e5c06ce
raw
history blame
4.59 kB
import os
import csv
import json
import requests
import re as r
import gradio as gr
import pandas as pd
from transformers import pipeline
from huggingface_hub import Repository
from urllib.request import urlopen
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "huggingface_sentiment_analysis_dataset"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/huggingface_sentiment_analysis_dataset"
DATA_FILENAME = "hf_sentiment_logs.csv"
DATA_FILE = os.path.join("hf_sentiment_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/huggingface_sentiment_analysis_dataset"
print("is none?", HF_TOKEN is None)
input_para = "I am happy\nI am sad\nI am not feeling well\nHe is a very good person\nHe is bad person\nI love pineapple\nI hate mangoes"
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="hf_sentiment_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
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 huggingface_result_page(paragraph):
if paragraph.strip():
model_base = pipeline('sentiment-analysis')
sen_list = paragraph
sen_list = sen_list.split('\n')
sen_list_temp = sen_list[0:]
results = []
temp_result_dict = []
for sen in sen_list_temp:
sen = sen.strip()
if sen:
cur_result = model_base(sen)[0]
temp_result_dict.append(sen)
results.append(cur_result['label'])
result = {
'Input': sen_list, 'Sentiment': results
}
print("LENGTH of results ====> ",str(len(results)))
print("LENGTH of sen_list ====> ",str(len(temp_result_dict)))
return pd.DataFrame(result)
else:
raise gr.Error("Please enter text in inputbox!!!!")
def save_data_and_sendmail(sen_list,results,result,paragraph):
try:
print("welcome")
ip_address = ''
ip_address= getIP()
print(ip_address)
location = get_location(ip_address)
print(location)
add_csv = [paragraph,result,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_sentiment'
myobj = {'para': sen_list,'result':results,'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="Paragraph",value=input_para)
outputs = gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Input","Sentiment"],wrap=True)
demo = gr.Interface(
huggingface_result_page,
inputs,
outputs,
title="Huggingface Sentiment Analysis",
css=".gradio-container {background-color: lightgray}",
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 Question Generation using T5 demo.</p>
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
)
demo.launch()