Spaces:
Runtime error
Runtime error
| import requests | |
| import gradio as gr | |
| from helper import generate_access_token | |
| def yes_no(value, array): | |
| if value in array: | |
| return "Yes" | |
| else: | |
| return "No" | |
| def generate_worksheet(grade, board, subject, topics, learning_objectives, question_tagging_options, | |
| number_of_questions, easy, medium, hard, remember, understand, apply, analyze, | |
| evaluate, create, mcq_single_answer, mcq_multiple_answer, true_false, fill_in_the_blanks, | |
| match_the_column, very_short_answer, short_answer, long_answer, solution_sheet, format, extras): | |
| data = { | |
| "institution_id": "inst789", | |
| "teacher_id": "teacher456", | |
| "grade": grade, | |
| "board": board, | |
| "subject": subject, | |
| "topics": topics.split(", "), | |
| "learning_objectives": learning_objectives.split(", "), | |
| "question_tagging_options": { | |
| "Hint": yes_no("Hint", question_tagging_options), | |
| "Curricular_Goal": yes_no("Curricular Goal", question_tagging_options), | |
| "Competency": yes_no("Competency", question_tagging_options), | |
| "LO": yes_no("LO", question_tagging_options), | |
| "LOB": yes_no("LOB", question_tagging_options), | |
| "Difficulty_level": yes_no("Difficulty Level", question_tagging_options), | |
| "Bloom_Taxonomy": yes_no("Bloom Taxonomy", question_tagging_options) | |
| }, | |
| "number_of_questions": number_of_questions, | |
| "difficulty_distribution_percentage": { | |
| "easy": easy, | |
| "medium": medium, | |
| "hard": hard | |
| }, | |
| "blooms_taxonomy_distribution_percentage": { | |
| "Remember": remember, | |
| "Understand": understand, | |
| "Apply": apply, | |
| "Analyze": analyze, | |
| "Evaluate": evaluate, | |
| "Create": create | |
| }, | |
| "question_type_distribution_absolute": { | |
| "MCQ_single_answer": mcq_single_answer, | |
| "MCQ_Multiple_answer": mcq_multiple_answer, | |
| "True_False": true_false, | |
| "Fill_in_the_blanks": fill_in_the_blanks, | |
| "Match_the_column": match_the_column, | |
| "Very_Short_answer": very_short_answer, | |
| "Short_answer": short_answer, | |
| "Long_answer": long_answer | |
| }, | |
| "solution_sheet": solution_sheet, | |
| "format": format, | |
| "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/worksheet", | |
| 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_worksheet(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/worksheet/{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") | |
| board = gr.Textbox(label="Board", value="NCERT") | |
| subject = gr.Textbox(label="Subject", value="Science") | |
| topics = gr.Textbox(label="Topics (comma-separated)") | |
| learning_objectives = gr.Textbox(label="Learning Objectives (comma-separated)") | |
| question_tagging_options = gr.CheckboxGroup(["Hint", "Curricular Goal", "Competency", "LO", "LOB", "Difficulty Level", "Bloom Taxonomy"], label="Question Tagging Options") | |
| number_of_questions = gr.Number(label="Number of Questions", value=10) | |
| gr.Markdown("## Difficulty Distribution (easy, medium, hard) - Percentage") | |
| # Difficulty Distribution (easy, medium, hard) | |
| with gr.Row(): | |
| with gr.Column(): | |
| easy = gr.Number(label="Easy", minimum=0, maximum=100, value=0) | |
| with gr.Column(): | |
| medium = gr.Number(label="Medium", minimum=0, maximum=100, value=0) | |
| with gr.Column(): | |
| hard = gr.Number(label="Hard", minimum=0, maximum=100, value=0) | |
| gr.Markdown("## Bloom Taxonomy Distribution (Remember, Understand, Apply, Analyze, Evaluate, Create) - Percentage") | |
| # Bloom Taxonomy Distribution (Remember, Understand, Apply, Analyze, Evaluate, Create) | |
| with gr.Row(): | |
| with gr.Column(): | |
| remember = gr.Number(minimum=0, maximum=100, step=1, label="Remember", value=0) | |
| with gr.Column(): | |
| understand = gr.Number(minimum=0, maximum=100, step=1, label="Understand", value=0) | |
| with gr.Column(): | |
| apply = gr.Number(minimum=0, maximum=100, step=1, label="Apply", value=0) | |
| with gr.Row(): | |
| with gr.Column(): | |
| analyze = gr.Number(minimum=0, maximum=100, step=1, label="Analyze", value=0) | |
| with gr.Column(): | |
| evaluate = gr.Number(minimum=0, maximum=100, step=1, label="Evaluate", value=0) | |
| with gr.Column(): | |
| create = gr.Number(minimum=0, maximum=100, step=1, label="Create", value=0) | |
| gr.Markdown("## Question Type Distribution (MCQ Single, MCQ Multiple, True/False, Fill in the Blanks, Match the Column, Very Short, Short, Long) - Absolute") | |
| with gr.Row(): | |
| with gr.Column(): | |
| mcq_single_answer = gr.Number(minimum=0, maximum=100, step=1, label="MCQ Single Answer", value=0) | |
| with gr.Column(): | |
| mcq_multiple_answer = gr.Number(minimum=0, maximum=100, step=1, label="MCQ Multiple Answer", value=0) | |
| with gr.Column(): | |
| true_false = gr.Number(minimum=0, maximum=100, step=1, label="True/False", value=0) | |
| with gr.Row(): | |
| with gr.Column(): | |
| fill_in_the_blanks = gr.Number(minimum=0, maximum=100, step=1, label="Fill in the Blanks", value=0) | |
| with gr.Column(): | |
| match_the_column = gr.Number(minimum=0, maximum=100, step=1, label="Match the Column", value=0) | |
| with gr.Column(): | |
| very_short_answer = gr.Number(minimum=0, maximum=100, step=1, label="Very Short Answer", value=0) | |
| with gr.Row(): | |
| with gr.Column(): | |
| short_answer = gr.Number(minimum=0, maximum=100, step=1, label="Short Answer", value=0) | |
| with gr.Column(): | |
| long_answer = gr.Number(minimum=0, maximum=100, step=1, label="Long Answer", value=0) | |
| solution_sheet = gr.Radio(label="Solution Sheet", choices=["Yes", "No"], value="Yes") | |
| format = gr.Textbox(label="Format", value="JSON") | |
| extras = gr.Textbox(label="Extras (comma-separated)") | |
| submit_button = gr.Button("Invoke Request") | |
| output = gr.JSON(label="Request Data") | |
| submit_button.click( | |
| generate_worksheet, | |
| inputs=[grade, board, subject, topics, learning_objectives, question_tagging_options, | |
| number_of_questions, easy, medium, hard, remember, understand, apply, analyze, | |
| evaluate, create, mcq_single_answer, mcq_multiple_answer, true_false, fill_in_the_blanks, | |
| match_the_column, very_short_answer, short_answer, long_answer, solution_sheet, format, extras], | |
| outputs=output | |
| ) | |
| return post_page | |
| def get_interface(): | |
| with gr.Blocks() as get_page: | |
| interface = gr.Interface( | |
| fn=get_worksheet, | |
| inputs=gr.Textbox(label="Enter Request ID"), | |
| outputs="json", | |
| ) | |
| return get_page | |
| def worksheet_generation(): | |
| gr.Markdown("# Worksheet Generation") | |
| with gr.Blocks() as worksheet_generation: | |
| with gr.Tabs(): | |
| with gr.TabItem("POST"): | |
| post_interface() | |
| with gr.TabItem("GET"): | |
| get_interface() | |
| return worksheet_generation |