Text2Question / app.py
bhaskartripathi's picture
Update app.py
feab799
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
from urllib.request import urlopen
import re as r
qg = QuestionGenerator()
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "Text2Question"
DATASET_REPO_URL = f"https://huggingface.co/spaces/bhaskartripathi/{DATASET_NAME}"
DATA_FILENAME = "que_gen_logs.csv"
DATA_FILE = os.path.join("que_gen_logs", DATA_FILENAME)
DATASET_REPO_ID = "bhaskartripathi/Text2Question"
print("is none?", HF_TOKEN is None)
article_value = """Affecting computing is an artificial intelligence area of study that recognizes, interprets, processes, and simulates human affects. The user’s emotional states can be sensed through electroencephalography (EEG)-based Brain Computer Interfaces (BCI) devices. Research in emotion recognition using these tools is a rapidly growing field with multiple inter-disciplinary applications. This article performs a survey of the pertinent scientific literature from 2015 to 2020. It presents trends and a comparative analysis of algorithm applications in new implementations from a computer science perspective. Our survey gives an overview of datasets, emotion elicitation methods, feature extraction and selection, classification algorithms, and performance evaluation. Lastly, we provide insights for future developments."""
# 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 getIP():
ip_address = ''
try:
d = str(urlopen('http://checkip.dyndns.com/')
.read())
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
except Exception as e:
print("Error while getting IP address -->",e)
return ip_address
def get_location(ip_addr):
location = {}
try:
ip=ip_addr
req_data={
"ip":ip,
"token":"pkml123"
}
url = "https://bhaskartripathi.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
except Exception as e:
print("Error while getting location -->",e)
return location
def generate_questions(article,num_que):
result = ''
if article.strip():
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)
print("sending result***!!!!!!", result)
return result
else:
raise gr.Error("Please enter text in inputbox!!!!")
"""
Save generated details
"""
def save_data_and_sendmail(article,generated_questions,num_que):
try:
ip_address= getIP()
print(ip_address)
location = get_location(ip_address)
print(location)
add_csv = [article, generated_questions, num_que, ip_address,location]
print("data^^^^^",add_csv)
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://bhaskartripathi.com/HF_space_que_gen'
myobj = {'article': article,'total_que': num_que,'gen_que':generated_questions,'ip_addr':ip_address,'loc':location}
x = requests.post(url, json = myobj)
print("myobj^^^^^",myobj)
except Exception as e:
return "Error while sending mail" + str(e)
return "Successfully save data"
## design 1
inputs=gr.Textbox(value=article_value, lines=5, label="Input Text/Article",elem_id="inp_div")
total_que = gr.Textbox(value=3, label="Enter the number of questions to generate",elem_id="inp_div")
outputs=gr.Textbox(label="Generated Questions",lines=6,elem_id="inp_div")
demo = gr.Interface(
generate_questions,
[inputs,total_que],
outputs,
title="Text2Question Generation with Text-to-Text-Transfer-Transformer",
css=".gradio-container {background-color: lightgray} #inp_div {background-color: #7FB3D5;}",
article="""<p style='text-align: center;'><a href="https://github.com/bhaskatripathi/QuestAnsGenerator/issues" target="_blank">Raise Issues</a></p>
<p style='text-align: center;'>MultiCloud4U Sandbox Env <a href="https://www.multicloud4u.com" target="_blank">Multicloud4U Technologies Pvt. Ltd.</a></p>"""
)
demo.launch()