Spaces:
Runtime error
Runtime error
File size: 3,571 Bytes
71718dc c9d5ef6 71718dc e3562c3 |
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 |
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 |