File size: 5,350 Bytes
2a5b9f4
 
 
 
 
233fac4
3e63657
2274456
 
2a5b9f4
d512dba
9e56dd0
2a5b9f4
 
207d8a5
 
 
 
9c0d256
207d8a5
 
 
7de25cf
207d8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a5b9f4
bbd4ef9
1bff3b0
 
 
 
 
 
 
7efe916
1bff3b0
 
 
 
 
 
 
 
 
 
7efe916
1bff3b0
 
7efe916
 
1bff3b0
 
2a5b9f4
 
1bff3b0
 
7de25cf
1bff3b0
 
 
 
 
 
 
 
 
 
 
f1a99c7
1bff3b0
 
 
 
 
 
c807374
1bff3b0
bbd4ef9
 
2ca183b
 
44b114c
bbd4ef9
1bff3b0
 
 
 
 
 
 
651b271
 
1bff3b0
651b271
1bff3b0
 
 
 
 
 
 
bbd4ef9
207d8a5
1bff3b0
2a5b9f4
 
7de25cf
2a5b9f4
 
 
 
 
 
 
 
 
fce1df6
2a5b9f4
 
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 gradio as gr
import requests
import os
import numpy as np
import pandas as pd
import json
import socket
import huggingface_hub
from huggingface_hub import Repository
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification
from questiongenerator import QuestionGenerator
import csv

qg = QuestionGenerator()

HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "Question_Generation_T5"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "que_gen_logs.csv"
DATA_FILE = os.path.join("que_gen_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/Question_Generation_T5"
print("is none?", HF_TOKEN is None)
article_value = """Google was founded in 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University in California. Together they own about 14 percent of its shares and control 56 percent of the stockholder voting power through supervoting stock. They incorporated Google as a privately held company on September 4, 1998. An initial public offering (IPO) took place on August 19, 2004, and Google moved to its headquarters in Mountain View, California, nicknamed the Googleplex. In August 2015, Google announced plans to reorganize its various interests as a conglomerate called Alphabet Inc. Google is Alphabet's leading subsidiary and will continue to be the umbrella company for Alphabet's Internet interests. Sundar Pichai was appointed CEO of Google, replacing Larry Page who became the CEO of Alphabet."""
# REPOSITORY_DIR = "data"
# LOCAL_DIR = 'data_local'
# os.makedirs(LOCAL_DIR,exist_ok=True)

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="que_gen_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)

def get_device_ip_address():
    result = {}
    if os.name == "nt":
        result = "Running on Windows"
        hostname = socket.gethostname()
        ip_address = socket.gethostbyname(hostname)
        result['ip_addr'] = ip_address
        result['host'] = hostname
        print(result)
        return result
    elif os.name == "posix":
        gw = os.popen("ip -4 route show default").read().split()
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((gw[2], 0))
        ipaddr = s.getsockname()[0]
        gateway = gw[2]
        host = socket.gethostname()
        result['ip_addr'] = ipaddr
        result['host'] = host
        print(result)
        return result
    else:
        result['id'] = os.name + " not supported yet."
        print(result)
        return result
        
def generate_questions(article,num_que):
    result = ''
    try:
        if num_que == None or num_que == '':
            num_que = 3
        else:
            num_que = num_que
        generated_questions_list = qg.generate(article, num_questions=int(num_que))
        summarized_data = {
            "generated_questions" : generated_questions_list
        }
        generated_questions = summarized_data.get("generated_questions",'')
            
        for q in generated_questions:
            print(q)
            result = result + q + '\n'
        save_data_and_sendmail(article,generated_questions,num_que,result)
        return result
    except Exception as e:
        return "Error while generating question -->" + e
"""
Save generated details
"""
def save_data_and_sendmail(article,generated_questions,num_que,result):
    try:
        hostname = {}
        hostname = get_device_ip_address()
        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) 
        add_csv = [article, generated_questions, num_que]
        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)
    # except Exception as e:
    #     return "Error while storing data -->" + e
        
    # try:  
        # with open(DATA_FILE, "r") as file:
        #     data = json.load(file)
        # data.append(entry)
        # with open(DATA_FILE, "w") as file:
        #     json.dump(data, file)
        #     commit_url = repo.push_to_hub()
    except Exception as e:
        return "Error while sending mail" + e
        
    return "Successfully save data"

## design 1
inputs=gr.Textbox(value=article_value, lines=5, label="Article/Text",elem_id="inp_div")
total_que = gr.Textbox(label="Number of Question want to generate",elem_id="inp_div")
outputs=gr.Textbox(lines=5, label="Generated Questions",elem_id="inp_div")

demo = gr.Interface(
    generate_questions,
    [inputs,total_que],
    outputs,
    title="Question Generation using T5",
    description="Feel free to give your feedback", 
    css=".gradio-container {background-color: lightgray} #inp_div {background-color: #7FB3D5;}"
)
demo.launch(enable_queue = False)