q_gen / app.py
krushna's picture
Update app.py
1e762bb
raw
history blame contribute delete
No virus
4.67 kB
import gradio as gr
import requests
import pandas as pd
import ast
import boto3
import re
import time
df= pd.DataFrame()
def _connect():
'''
Creates and returns the s3 connection by passing the access key and secret key to boto3.client
'''
s3 = boto3.client("s3",aws_access_key_id='AKIA2BAL5ERM4EL4HRAE',aws_secret_access_key='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')
return s3
def _upload(s3, tmp_filepath, s3_filepath, acl="public-read", content_type=None):
'''
This function uploads the files to specified bucket in s3 and returns the file url
'''
S3_LOCATION = 'https://{}.s3.ap-south-1.amazonaws.com/'.format('edwisely-academic-materials-v2')
_content_type = content_type
try:
with open(tmp_filepath, 'rb') as data:
s3.upload_fileobj(data, 'edwisely-academic-materials-v2', s3_filepath,
ExtraArgs={
"ACL": acl,
"ContentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
})
return "{}{}".format(S3_LOCATION, s3_filepath)
except Exception as e:
print("Something Happened: ", e)
raise
def upload_file(tmp_filepath, faculty_name):
s3_obj = _connect()
s3_filepath = "{}/{}/{}.xlsx".format('q_gen', 'test',int(time.time()))
s3_uploaded_path = _upload(s3_obj, tmp_filepath, s3_filepath)
return s3_uploaded_path
def q_gen(topics_list, count, difficulty):
url = "https://d5cdtgvt04.execute-api.ap-south-1.amazonaws.com/test/q_gen"
# params = {
# "grand_topic_ids": "16595,898,746",
# "topic_ids": "59559,59564,59565,59563",
# "sub_topic_ids": "54345,31152,4011",
# "count":"10",
# "difficulty":"8"
# }
# params = {
# "grand_topic_ids": "D04B01GT02,D07B03GT03,D12B190GT12",
# "topic_ids": "D13B10GT06T03,D13B10GT07T02,D13B10GT07T03,D13B10GT07T04",
# "sub_topic_ids": "D04B01GT04T02ST01,D03B29GT01T02ST010122,D12B190GT21T01ST02",
# "count":"10",
# "difficulty":"8"
# }
topics_list = topics_list.split(",")
grand_topics, topics, sub_topics = "","",""
for i in topics_list:
if len(i.split("T")) == 2:
temp = i.strip()
if grand_topics != "":
grand_topics = grand_topics+","+temp
else:
grand_topics = i
elif len(i.split("T")) == 3:
temp = i.strip()
if topics != "":
topics = topics+","+temp
else:
topics = temp
elif len(i.split("T")) == 4:
temp = i.strip()
if sub_topics != "":
sub_topics = sub_topics+","+temp
else:
sub_topics = temp
params = {
"grand_topic_ids": grand_topics,
"topic_ids": topics,
"sub_topic_ids": sub_topics,
"count":count,
"difficulty":difficulty,
"test_id":1,
"faculty_id":1
}
df = pd.DataFrame()
path = ""
try:
response = requests.get(url, params=params)
response.raise_for_status() # Raise an exception if the request was not successful
# Access the response content
content = response.text
df = pd.DataFrame(ast.literal_eval(content))
if df.columns[0] == "Not enough questions":
return "Not enough questions", df
options_df = pd.DataFrame(df['questions_options'])
columns_shape = pd.DataFrame(df['questions_options'].tolist()).shape[1]
for i in range(columns_shape):
options_df[f"option_{i+1}"] = pd.DataFrame(df['questions_options'].tolist())[i].apply(pd.Series)['name']
# df =df[['name', 'media','blooms_level', 'type_name', 'type_code']]
df['com'] = range(df.shape[0])
options_df['com'] = range(df.shape[0])
df = pd.merge(df, options_df, on=['com'])
df = df[['id','name', 'media','blooms_level','type_name', 'type_code', 'option_1', 'option_2', 'option_3', 'option_4']]
df.to_excel('temp.xlsx')
file_path = "temp.xlsx"
path = upload_file(file_path, 'krushna')
except requests.exceptions.RequestException as e:
print("Error occurred:", e)
return path, df
demo = gr.Interface(fn=q_gen, inputs=[
gr.Textbox(value="D04B01GT02,D12B190GT12, D13B10GT06T03,D13B10GT07T02, D12B190GT21T01ST02", label = "topics"),
gr.Textbox(value="10", label = "Count"),
gr.Textbox(value="8", label = "Difficulty")], outputs=["text","dataframe"])
demo.launch()