Spaces:
Runtime error
Runtime error
Commit
·
f5bb7ae
1
Parent(s):
5cff6ab
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,134 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
st.title("Sentiment Analysis of Tweets about US Airlines")
|
| 12 |
-
st.sidebar.title("Sentiment Analysis of Tweets about US Airlines")
|
| 13 |
-
st.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
|
| 14 |
-
st.sidebar.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
sentiment_count = data['airline_sentiment'].value_counts()
|
| 32 |
-
sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values})
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
st.plotly_chart(fig)
|
| 39 |
-
else:
|
| 40 |
-
fig = px.pie(sentiment_count, values='Tweets', names='Sentiment')
|
| 41 |
-
st.plotly_chart(fig)
|
| 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 |
-
word_sentiment = st.sidebar.radio('Display word cloud for what sentiment?',('positive', 'neutral','negative'))
|
| 68 |
-
|
| 69 |
-
if not st.sidebar.checkbox("Close", True, key='3'):
|
| 70 |
-
st.header('Word cloud for %s sentiment' % (word_sentiment))
|
| 71 |
-
df = data[data['airline_sentiment']==word_sentiment]
|
| 72 |
-
words = ' '.join(df['text'])
|
| 73 |
-
processed_words = ' '.join([word for word in words.split() if 'http' not in word and not word.startswith('@') and word !='RT'])
|
| 74 |
-
wordcloud = WordCloud(stopwords=STOPWORDS,
|
| 75 |
-
background_color='white', height=640, width=800).generate(processed_words)
|
| 76 |
-
plt.imshow(wordcloud)
|
| 77 |
-
plt.xticks([])
|
| 78 |
-
plt.yticks([])
|
| 79 |
-
st.pyplot()
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if __name__ == '__main__':
|
| 83 |
-
run()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
import re as r
|
| 6 |
+
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
from huggingface_hub import Repository
|
| 10 |
+
from urllib.request import urlopen
|
|
|
|
| 11 |
|
| 12 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
+
DATASET_NAME = "huggingface_sentiment_analysis_dataset"
|
| 14 |
+
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/huggingface_sentiment_analysis_dataset"
|
| 15 |
+
DATA_FILENAME = "hf_sentiment_logs.csv"
|
| 16 |
+
DATA_FILE = os.path.join("hf_sentiment_logs", DATA_FILENAME)
|
| 17 |
+
DATASET_REPO_ID = "pragnakalp/huggingface_sentiment_analysis_dataset"
|
| 18 |
+
print("is none?", HF_TOKEN is None)
|
| 19 |
|
| 20 |
+
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"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
hf_hub_download(
|
| 24 |
+
repo_id=DATASET_REPO_ID,
|
| 25 |
+
filename=DATA_FILENAME,
|
| 26 |
+
cache_dir=DATA_DIRNAME,
|
| 27 |
+
force_filename=DATA_FILENAME
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
except:
|
| 31 |
+
print("file not found")
|
| 32 |
+
|
| 33 |
+
repo = Repository(
|
| 34 |
+
local_dir="hf_sentiment_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def getIP():
|
| 38 |
+
d = str(urlopen('http://checkip.dyndns.com/')
|
| 39 |
+
.read())
|
| 40 |
+
|
| 41 |
+
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
|
| 42 |
+
|
| 43 |
+
def get_location(ip_addr):
|
| 44 |
+
ip=ip_addr
|
| 45 |
+
|
| 46 |
+
req_data={
|
| 47 |
+
"ip":ip,
|
| 48 |
+
"token":"pkml123"
|
| 49 |
+
}
|
| 50 |
+
url = "https://demos.pragnakalp.com/get-ip-location"
|
| 51 |
+
|
| 52 |
+
# req_data=json.dumps(req_data)
|
| 53 |
+
# print("req_data",req_data)
|
| 54 |
+
headers = {'Content-Type': 'application/json'}
|
| 55 |
+
|
| 56 |
+
response = requests.request("POST", url, headers=headers, data=json.dumps(req_data))
|
| 57 |
+
response = response.json()
|
| 58 |
+
print("response======>>",response)
|
| 59 |
+
return response
|
| 60 |
+
|
| 61 |
+
def huggingface_result_page(paragraph):
|
| 62 |
+
if paragraph.strip():
|
| 63 |
+
model_base = pipeline('sentiment-analysis')
|
| 64 |
|
| 65 |
+
sen_list = paragraph
|
| 66 |
+
sen_list = sen_list.split('\n')
|
| 67 |
+
sen_list_temp = sen_list[0:]
|
| 68 |
+
results = []
|
| 69 |
+
temp_result_dict = []
|
| 70 |
|
| 71 |
+
for sen in sen_list_temp:
|
| 72 |
+
sen = sen.strip()
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
if sen:
|
| 75 |
+
cur_result = model_base(sen)[0]
|
| 76 |
+
temp_result_dict.append(sen)
|
| 77 |
+
results.append(cur_result['label'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
result = {
|
| 80 |
+
'Input': sen_list, 'Sentiment': results
|
| 81 |
+
}
|
| 82 |
+
print("LENGTH of results ====> ",str(len(results)))
|
| 83 |
+
print("LENGTH of sen_list ====> ",str(len(temp_result_dict)))
|
| 84 |
+
|
| 85 |
+
return pd.DataFrame(result)
|
| 86 |
+
else:
|
| 87 |
+
raise gr.Error("Please enter text in inputbox!!!!")
|
| 88 |
+
|
| 89 |
+
def save_data_and_sendmail(sen_list,results,result,paragraph):
|
| 90 |
+
try:
|
| 91 |
+
print("welcome")
|
| 92 |
+
ip_address = ''
|
| 93 |
+
|
| 94 |
+
ip_address= getIP()
|
| 95 |
+
print(ip_address)
|
| 96 |
+
location = get_location(ip_address)
|
| 97 |
+
print(location)
|
| 98 |
+
add_csv = [paragraph,result,ip_address,location]
|
| 99 |
+
with open(DATA_FILE, "a") as f:
|
| 100 |
+
writer = csv.writer(f)
|
| 101 |
+
# write the data
|
| 102 |
+
writer.writerow(add_csv)
|
| 103 |
+
commit_url = repo.push_to_hub()
|
| 104 |
+
print("commit data :",commit_url)
|
| 105 |
+
|
| 106 |
+
# url = 'https://pragnakalpdev35.pythonanywhere.com/HF_space_que_gen'
|
| 107 |
+
# # url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_question_generator'
|
| 108 |
+
# myobj = {'article': article,'total_que': num_que,'gen_que':result,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")}
|
| 109 |
+
# x = requests.post(url, json = myobj)
|
| 110 |
+
|
| 111 |
+
url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_sentiment'
|
| 112 |
+
myobj = {'para': sen_list,'result':results,'ip_addr':ip_address,"location":location}
|
| 113 |
+
x = requests.post(url, json = myobj)
|
| 114 |
+
|
| 115 |
+
return "Successfully save data"
|
| 116 |
|
| 117 |
+
except Exception as e:
|
| 118 |
+
print("error")
|
| 119 |
+
return "Error while sending mail" + str(e)
|
| 120 |
+
|
| 121 |
+
inputs = gr.Textbox(lines=3, label="Paragraph",value=input_para)
|
| 122 |
+
outputs = gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Input","Sentiment"],wrap=True)
|
| 123 |
+
|
| 124 |
+
demo = gr.Interface(
|
| 125 |
+
huggingface_result_page,
|
| 126 |
+
inputs,
|
| 127 |
+
outputs,
|
| 128 |
+
title="Huggingface Sentiment Analysis",
|
| 129 |
+
css=".gradio-container {background-color: lightgray}",
|
| 130 |
+
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>
|
| 131 |
+
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|