Spaces:
Runtime error
Runtime error
import requests | |
import gradio as gr | |
from helper import generate_access_token | |
def generate_content(grade, subject, topics, learning_outcomes, notes, stories, activities, group_dicussion, extras): | |
data = { | |
"institution_id": "inst789", | |
"teacher_id": "teacher456", | |
"grade": grade, | |
"subject": subject, | |
"topics": topics.split(", "), | |
"learning_outcomes": learning_outcomes.split(", "), | |
"content_type": { | |
"notes": notes, | |
"stories": stories, | |
"activities": activities, | |
"group_dicussion": group_dicussion | |
}, | |
"extras": extras.split(", ") | |
} | |
access_token = generate_access_token() | |
if access_token is None: | |
return {"Error": "Failed to generate access token"} | |
response = requests.post("http://20.193.151.200:8080/v1/k12/generate/content", | |
headers={ | |
"accept": "application/json", | |
"content-type": "application/json", | |
"Authorization": f"{access_token}"}, | |
json=data) | |
if(str(response.status_code)[0] != '2'): | |
return {"Error": f"{response.status_code}"} | |
return response.json() | |
def get_content_generation(request_id): | |
access_token = generate_access_token() | |
if access_token is None: | |
return {"Error": "Failed to generate access token"} | |
url = f"http://20.193.151.200:8080/v1/k12/generate/content/{request_id}" | |
headers = {"accept": "application/json", | |
"Authorization": access_token} | |
response = requests.get(url, headers=headers) | |
if str(response.status_code)[0] == "2": | |
return response.json() | |
else: | |
return {"Error" : f"{response.status_code}"} | |
def post_interface(): | |
with gr.Blocks() as post_page: | |
grade = gr.Textbox(label="Grade", value="8") | |
subject = gr.Textbox(label="Subject", value="Science") | |
topics = gr.Textbox(label="Topics (comma-separated)", value="Plant and Animal Cells, Reproduction in Humans and Animals") | |
learning_outcomes = gr.Textbox(label="Learning Outcomes (comma-separated)") | |
notes = gr.Radio(label="Notes", choices=["Yes", "No"], value="No") | |
stories = gr.Radio(label="Stories", choices=["Yes", "No"], value="No") | |
activities = gr.Radio(label="Activities", choices=["Yes", "No"], value="No") | |
group_dicussion = gr.Radio(label="Group Discussion", choices=["Yes", "No"], value="No") | |
extras = gr.Textbox(label="Extras (comma-separated)") | |
submit_button = gr.Button("Invoke Request") | |
output = gr.JSON(label="Content Generation ID") | |
submit_button.click( | |
fn=generate_content, | |
inputs=[grade, subject, topics, learning_outcomes, notes, stories, activities, group_dicussion, extras], | |
outputs=output, | |
) | |
return post_page | |
def get_interface(): | |
with gr.Blocks() as get_page: | |
interface = gr.Interface( | |
fn=get_content_generation, | |
inputs=gr.Textbox(label="Enter Request ID"), | |
outputs="json", | |
) | |
return get_page | |
def content_generation(): | |
gr.Markdown("# Content Generation") | |
with gr.Blocks() as content_generation: | |
with gr.Tabs(): | |
with gr.TabItem("POST"): | |
post_interface() | |
with gr.TabItem("GET"): | |
get_interface() | |
return content_generation |