pragnakalp commited on
Commit
7f470bc
·
1 Parent(s): db40a0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -5
app.py CHANGED
@@ -1,13 +1,67 @@
1
- from __future__ import absolute_import, division, print_function, unicode_literals
2
 
3
  import os
4
- import gc
 
 
 
5
  import gradio as gr
6
  import pandas as pd
7
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def huggingface_result_page(paragraph):
10
- if article.strip():
11
  model_base = pipeline('sentiment-analysis')
12
 
13
  sen_list = paragraph
@@ -29,13 +83,44 @@ def huggingface_result_page(paragraph):
29
  }
30
  print("LENGTH of results ====> ",str(len(results)))
31
  print("LENGTH of sen_list ====> ",str(len(temp_result_dict)))
32
-
33
  return pd.DataFrame(result)
34
  else:
35
  raise gr.Error("Please enter text in inputbox!!!!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
37
 
38
- inputs = gr.Textbox(lines=10, label="Paragraph")
39
  outputs = gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Input","Sentiment"],wrap=True)
40
 
41
  demo = gr.Interface(
 
 
1
 
2
  import os
3
+ import csv
4
+ import json
5
+ import requests
6
+ import re as r
7
  import gradio as gr
8
  import pandas as pd
9
  from transformers import pipeline
10
+ from urllib.request import urlopen
11
+ from huggingface_hub import Repository
12
+
13
+
14
+ HF_TOKEN = os.environ.get("HF_TOKEN")
15
+ DATASET_NAME = "huggingface_sentiment_analysis_dataset"
16
+ DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
17
+ DATA_FILENAME = "hf_sentiment_logs.csv"
18
+ DATA_FILE = os.path.join("hf_sentiment_logs", DATA_FILENAME)
19
+ DATASET_REPO_ID = "pragnakalp/huggingface_sentiment_analysis_dataset"
20
+ print("is none?", HF_TOKEN is None)
21
+
22
+ 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"
23
+
24
+ try:
25
+ hf_hub_download(
26
+ repo_id=DATASET_REPO_ID,
27
+ filename=DATA_FILENAME,
28
+ cache_dir=DATA_DIRNAME,
29
+ force_filename=DATA_FILENAME
30
+ )
31
+
32
+ except:
33
+ print("file not found")
34
+
35
+ repo = Repository(
36
+ local_dir="hf_sentiment_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
37
+ )
38
+
39
+ def getIP():
40
+ d = str(urlopen('http://checkip.dyndns.com/')
41
+ .read())
42
+
43
+ return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
44
+
45
+ def get_location(ip_addr):
46
+ ip=ip_addr
47
+
48
+ req_data={
49
+ "ip":ip,
50
+ "token":"pkml123"
51
+ }
52
+ url = "https://demos.pragnakalp.com/get-ip-location"
53
+
54
+ # req_data=json.dumps(req_data)
55
+ # print("req_data",req_data)
56
+ headers = {'Content-Type': 'application/json'}
57
+
58
+ response = requests.request("POST", url, headers=headers, data=json.dumps(req_data))
59
+ response = response.json()
60
+ print("response======>>",response)
61
+ return response
62
 
63
  def huggingface_result_page(paragraph):
64
+ if paragraph.strip():
65
  model_base = pipeline('sentiment-analysis')
66
 
67
  sen_list = paragraph
 
83
  }
84
  print("LENGTH of results ====> ",str(len(results)))
85
  print("LENGTH of sen_list ====> ",str(len(temp_result_dict)))
86
+ save_data_and_sendmail(sen_list,results)
87
  return pd.DataFrame(result)
88
  else:
89
  raise gr.Error("Please enter text in inputbox!!!!")
90
+
91
+ def save_data_and_sendmail(sen_list,results):
92
+ try:
93
+ print("welcome")
94
+ ip_address = ''
95
+
96
+ ip_address= getIP()
97
+ print(ip_address)
98
+ location = get_location(ip_address)
99
+ print(location)
100
+ add_csv = [article,output,ip_address,location]
101
+ with open(DATA_FILE, "a") as f:
102
+ writer = csv.writer(f)
103
+ # write the data
104
+ writer.writerow(add_csv)
105
+ commit_url = repo.push_to_hub()
106
+ print("commit data :",commit_url)
107
+
108
+ # url = 'https://pragnakalpdev35.pythonanywhere.com/HF_space_que_gen'
109
+ # # url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_question_generator'
110
+ # myobj = {'article': article,'total_que': num_que,'gen_que':result,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")}
111
+ # x = requests.post(url, json = myobj)
112
+
113
+ url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_sentiment'
114
+ myobj = {'para': sen_list,'result':results,'ip_addr':ip_address,"location":location}
115
+ x = requests.post(url, json = myobj)
116
+
117
+ return "Successfully save data"
118
 
119
+ except Exception as e:
120
+ print("error")
121
+ return "Error while sending mail" + str(e)
122
 
123
+ inputs = gr.Textbox(lines=3, label="Paragraph",value=input_para)
124
  outputs = gr.Dataframe(row_count = (3, "dynamic"), col_count=(2, "fixed"), label="Here is the Result", headers=["Input","Sentiment"],wrap=True)
125
 
126
  demo = gr.Interface(