File size: 4,594 Bytes
db40a0b
 
7f470bc
 
 
 
db40a0b
 
 
7f470bc
05e930f
7f470bc
 
 
36278b6
7f470bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db40a0b
 
7f470bc
db40a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5c06ce
db40a0b
 
 
7f470bc
df647c5
7f470bc
 
 
 
 
 
 
 
df647c5
7f470bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db40a0b
7f470bc
 
 
db40a0b
7f470bc
db40a0b
 
 
 
 
 
 
 
4abb74d
59fbb9c
db40a0b
 
fe4c667
db40a0b
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

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()