Spaces:
Runtime error
Runtime error
File size: 3,550 Bytes
17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 7a379ed 2340a8e 17e8bc6 2340a8e 7a379ed 2340a8e c4af1a1 2340a8e c4af1a1 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 2340a8e 17e8bc6 3e3f255 | 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 | import json
import gradio as gr
# 1. Load the data from gradio.json
with open("gradio.json", "r", encoding="utf-8") as f:
data = json.load(f)
# Convert the keys to integers (if they are numeric as strings like "1", "2", etc.)
# so we can more easily handle them in a slider.
question_ids = sorted([int(k) for k in data.keys()])
def render_question(question_id: int):
"""Given a question ID, returns the strings needed to populate our Gradio UI."""
# Convert question_id to string to retrieve from data.
q = data[str(question_id)]
# Extract the relevant fields.
skills = q["skills"]
problem_statement = q["problem_statement"]
function_name = q["function_name"]
function_signature = q["function_signature"]
function_docstring = q["function_docstring"]
gold_solution = q["gold_solution"]
unit_tests = q["unit_tests"]
qid = q["qid"]
skills = " ".join(skills.split("_"))
# We'll format each piece using Markdown-friendly text.
# Use .replace("\\n", "\n") to correctly interpret escaped \n in the JSON strings.
skills_md = f"## Question\n- **Function Name**: {function_name}\n\n- **Skills**: {skills}\n\n- **ID**: {qid}"
# Problem statement (and docstring) might contain literal "\n" characters in the JSON.
# Convert them to actual newlines so Markdown displays them properly.
problem_template = """## Problem Statement
{problem_statement}
**Signature**:
```cpp
{function_signature}
```
**Docstring**:
```cpp
{function_docstring}
```"""
problem_md = problem_template.format(
problem_statement=problem_statement.replace("\\n", "\n"),
function_name=function_name,
function_signature=function_signature,
function_docstring=function_docstring.replace("\\n", "\n")
)
gold_solution_md = f"## Gold Solution\n```cpp\n{gold_solution}\n```"
unit_tests_md = f"## Unit Tests\n```cpp\n{unit_tests}\n```"
return skills_md, problem_md, gold_solution_md, unit_tests_md
# 2. Set up the Gradio interface
def update_display(question_number):
"""This function is called whenever the slider changes; it updates the displayed content."""
return render_question(question_number)
with gr.Blocks() as demo:
gr.Markdown("## Code Skill-Mix Viewer")
# Slider for selecting question index
with gr.Row():
question_slider = gr.Slider(
minimum=min(question_ids),
maximum=max(question_ids),
step=1,
value=min(question_ids),
label="Select Question"
)
# Top panels (left: question, right: gold solution)
with gr.Row():
with gr.Column():
skills_box = gr.Markdown()
problem_box = gr.Markdown()
with gr.Column():
gold_solution_box = gr.Markdown()
# Bottom panel (unit tests)
with gr.Row():
unit_test_box = gr.Markdown()
# Whenever the slider changes, update all displays
question_slider.change(
fn=update_display,
inputs=question_slider,
outputs=[skills_box, problem_box, gold_solution_box, unit_test_box]
)
# Initialize the interface with the first question.
# We'll manually set them for the default value so that something appears on load.
default_skills, default_problem, default_solution, default_tests = render_question(question_ids[0])
skills_box.value = default_skills
problem_box.value = default_problem
gold_solution_box.value = default_solution
unit_test_box.value = default_tests
demo.launch() |