DP_PRACA/gradio_app.py
2026-12-04 09:22:31 +01:00

157 lines
6.5 KiB
Python

"""Teacher-facing Gradio demo preserving graph evidence and visualization."""
from __future__ import annotations
import gradio as gr
from main import get_or_build_graph
from react_agent import react_agent
from tools import answer_general_question, check_teaching_suitability
GRAPH = get_or_build_graph()
def prepare_lesson(subject: str, grade: str, topic: str, duration: str, language: str):
subject = subject.strip() or "not specified"
grade = grade.strip() or "not specified"
topic = topic.strip() or "not specified"
duration = duration.strip() or "45"
language = language.strip() or "Slovak"
request = f"""Prepare a well-structured {duration}-minute lesson in {language}.
Subject: {subject}
Grade: {grade}
Topic: {topic}
Some fields may be unspecified. Make sensible, clearly stated assumptions for
missing fields and keep the lesson adaptable. If the topic is unspecified,
choose a suitable topic only when the graph evidence supports it; otherwise
propose a clearly labelled topic assumption rather than presenting it as a
confirmed curriculum fact.
Return a complete lesson plan with:
1. Assumptions and intended learners
2. Learning objectives
3. Materials and preparation
4. Step-by-step activities with approximate timings
5. Differentiation and inclusion
6. Assessment
7. Homework or extension
Use only the retrieved graph evidence for curriculum-specific claims."""
try:
response = react_agent(request, [], GRAPH, language)
except (RuntimeError, TimeoutError) as error:
return f"Unable to generate the lesson: {error}", "No answer was returned by the LLM service."
return response.answer, response.evidence or "No graph evidence retrieved."
def check_suitability(subject: str, grade: str, topic: str, conditions: str, language: str):
try:
result = check_teaching_suitability(subject, grade, topic, conditions, GRAPH, language)
except (RuntimeError, TimeoutError) as error:
return f"Unable to assess suitability: {error}", "No answer was returned by the LLM service."
return result["answer"], result["evidence"] or "No graph evidence retrieved."
def answer_question(question: str, language: str = "Slovak"):
question = question.strip()
if not question:
return "Please enter a question.", ""
try:
result = answer_general_question(question, GRAPH, language)
except (RuntimeError, TimeoutError) as error:
return f"Unable to answer the question: {error}", "No answer was returned by the LLM service."
return result["answer"], result["evidence"] or "No graph evidence retrieved."
def create_demo():
with gr.Blocks(title="GraphRAG Teacher Assistant") as demo:
gr.Markdown("# GraphRAG teacher assistant\nAnswers questions and prepares lessons using linked graph evidence.")
with gr.Tab("General Q&A"):
gr.Markdown(
"Ask a general question about curriculum, teaching topics, educational needs, "
"or documents. Answers are kept separate from lesson planning."
)
question = gr.Textbox(
label="Question",
placeholder="For example: What is the role of a pedagogical assistant in primary schools?",
lines=3,
)
general_language = gr.Dropdown(
choices=["Slovak", "English"],
value="Slovak",
label="Answer language",
)
ask_button = gr.Button("Ask", variant="primary")
general_answer = gr.Markdown(label="Answer")
general_evidence = gr.Textbox(label="Graph evidence", lines=8)
ask_button.click(
answer_question,
[question, general_language],
[general_answer, general_evidence],
)
with gr.Tab("Lesson preparation"):
with gr.Row():
subject = gr.Textbox(
label="Subject (optional)",
placeholder="Leave blank to let the assistant make an assumption",
)
grade = gr.Textbox(
label="Grade (optional)",
placeholder="Leave blank for an adaptable lesson",
)
topic = gr.Textbox(
label="Topic (optional)",
placeholder="Leave blank to request a suitable topic assumption",
)
with gr.Row():
duration = gr.Textbox(value="45", label="Duration (minutes, optional)")
language = gr.Dropdown(
choices=["Slovak", "English"],
value="Slovak",
label="Answer language",
)
button = gr.Button("Prepare lesson", variant="primary")
answer = gr.Markdown(label="Lesson plan")
evidence = gr.Textbox(label="Graph evidence", lines=8)
button.click(prepare_lesson, [subject, grade, topic, duration, language], [answer, evidence])
with gr.Tab("Teaching suitability and compliance"):
gr.Markdown(
"This advisory feature indicates whether available curriculum evidence supports a topic "
"or teaching condition. It is not a legal opinion."
)
suitability_subject = gr.Textbox(label="Subject")
suitability_grade = gr.Textbox(label="Grade")
suitability_topic = gr.Textbox(label="Topic")
suitability_conditions = gr.Textbox(
label="Specific conditions",
placeholder="For example: mixed ability class, ADHD, 30-minute class, outdoor teaching, no internet",
lines=4,
)
suitability_language = gr.Dropdown(
choices=["Slovak", "English"],
value="Slovak",
label="Answer language",
)
suitability_button = gr.Button("Check suitability", variant="primary")
suitability_answer = gr.Markdown(label="Suitability assessment")
suitability_evidence = gr.Textbox(label="Graph evidence", lines=8)
suitability_button.click(
check_suitability,
[
suitability_subject,
suitability_grade,
suitability_topic,
suitability_conditions,
suitability_language,
],
[suitability_answer, suitability_evidence],
)
return demo
if __name__ == "__main__":
create_demo().launch()