|
import gradio as gr |
|
import requests |
|
import os |
|
import numpy as np |
|
import pandas as pd |
|
import json |
|
import csv |
|
import huggingface_hub |
|
from huggingface_hub import Repository |
|
|
|
from questiongenerator import QuestionGenerator |
|
|
|
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) |
|
|
|
|
|
|
|
|
|
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 |
|
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 |
|
return result |
|
else: |
|
result = os.name + " not supported yet." |
|
return result |
|
|
|
def generate_questions(article,num_que): |
|
result = '' |
|
try: |
|
if num_que == None or num_que == '': |
|
num_que = 5 |
|
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: |
|
print("=====================================================>>>>>>>>>>>>>>>>") |
|
article1 = article |
|
generated_questions1 = [] |
|
generated_questions1 = generated_questions |
|
num_que1 = num_que |
|
result1 = result |
|
hostname = get_device_ip_address() |
|
|
|
|
|
url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_question_generator' |
|
myobj = {'article': article1,'gen_que':result1,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")} |
|
print("myobj ",myobj) |
|
x = requests.post(url, json = myobj) |
|
print(x) |
|
add_csv = [article, generated_questions, num_que] |
|
with open(DATA_FILE, "a") as f: |
|
writer = csv.writer(f) |
|
|
|
writer.writerow(add_csv) |
|
commit_url = repo.push_to_hub() |
|
print("commit data :",commit_url) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
return "Error while sending mail" |
|
|
|
return "Successfully save data" |
|
|
|
|
|
inputs=gr.Textbox(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) |
|
|