Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,62 +14,56 @@ vector_store = Chroma(
|
|
| 14 |
collection_name="geometry_sol"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# Load OpenAI key (
|
| 18 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 19 |
|
| 20 |
# Load the LLM (GPT-3.5)
|
| 21 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
You are a strict assistant for the Virginia Geometry SOL.
|
| 29 |
|
| 30 |
-
|
| 31 |
-
{
|
| 32 |
-
|
| 33 |
-
Answer the question: "{query}"
|
| 34 |
-
|
| 35 |
-
If the answer is in the SOL text, quote it exactly. Do not rephrase or summarize. Do not add your own explanation.
|
| 36 |
|
| 37 |
-
|
| 38 |
-
"""
|
| 39 |
-
),
|
| 40 |
-
"lesson plan": PromptTemplate(
|
| 41 |
-
input_variables=["context", "query"],
|
| 42 |
-
template="""
|
| 43 |
-
Given the following retrieved SOL text:
|
| 44 |
{context}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
1.
|
| 49 |
-
2.
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
-
|
| 54 |
-
"worksheet": PromptTemplate(
|
| 55 |
-
input_variables=["context", "query"],
|
| 56 |
-
template="""
|
| 57 |
-
{context}
|
| 58 |
-
|
| 59 |
-
Create a student worksheet for: "{query}"
|
| 60 |
-
Include: concept summary, a worked example, and 3 practice problems.
|
| 61 |
-
"""
|
| 62 |
-
),
|
| 63 |
-
"proofs": PromptTemplate(
|
| 64 |
-
input_variables=["context", "query"],
|
| 65 |
-
template="""
|
| 66 |
-
{context}
|
| 67 |
-
|
| 68 |
-
Generate a proof-focused geometry lesson plan for: "{query}"
|
| 69 |
-
Include: student-friendly explanation, real-world link, and activity.
|
| 70 |
-
"""
|
| 71 |
-
)
|
| 72 |
-
}
|
| 73 |
|
| 74 |
# Optional: shortcut to solve simple math problems (like area of rectangle)
|
| 75 |
def try_math_solver(query):
|
|
@@ -79,34 +73,31 @@ def try_math_solver(query):
|
|
| 79 |
return f"The area of the rectangle is {l} × {w} = {l * w} square units."
|
| 80 |
return None
|
| 81 |
|
| 82 |
-
# RAG function
|
| 83 |
-
def rag_query(query
|
| 84 |
docs = vector_store.similarity_search(query, k=2)
|
| 85 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 86 |
-
prompt =
|
| 87 |
return llm.invoke(prompt).content
|
| 88 |
|
| 89 |
# Gradio app function
|
| 90 |
-
def ask_geometry_sol(query
|
| 91 |
math_result = try_math_solver(query)
|
| 92 |
if math_result:
|
| 93 |
return math_result
|
| 94 |
try:
|
| 95 |
-
return rag_query(query
|
| 96 |
except Exception as e:
|
| 97 |
return f"⚠️ Error: {type(e).__name__} - {str(e)}"
|
| 98 |
|
| 99 |
-
# Gradio UI
|
| 100 |
iface = gr.Interface(
|
| 101 |
fn=ask_geometry_sol,
|
| 102 |
-
inputs=
|
| 103 |
-
gr.Textbox(label="Enter your Geometry SOL question or topic"),
|
| 104 |
-
gr.Radio(["general", "lesson plan", "worksheet", "proofs"], value="general", label="Response type")
|
| 105 |
-
],
|
| 106 |
outputs="text",
|
| 107 |
title="📘 Virginia Geometry SOL Assistant",
|
| 108 |
-
description="Ask about any 2023 Geometry SOL (Standards of Learning).
|
| 109 |
)
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
iface.launch()
|
|
|
|
| 14 |
collection_name="geometry_sol"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Load OpenAI key (set this in Hugging Face Space Secrets)
|
| 18 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 19 |
|
| 20 |
# Load the LLM (GPT-3.5)
|
| 21 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)
|
| 22 |
|
| 23 |
+
# Unified prompt to auto-detect intent
|
| 24 |
+
template = PromptTemplate(
|
| 25 |
+
input_variables=["context", "query"],
|
| 26 |
+
template="""
|
| 27 |
+
You are a Virginia high school Geometry assistant. Based on the user question below, determine the correct response type and answer accordingly:
|
|
|
|
| 28 |
|
| 29 |
+
User Question:
|
| 30 |
+
{query}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
Based on the following SOL text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
{context}
|
| 34 |
|
| 35 |
+
Response Rules:
|
| 36 |
+
- If the question is asking for an **SOL number**, respond with:
|
| 37 |
+
1. The exact SOL code (e.g., G.RLT.1)
|
| 38 |
+
2. The exact description line from the SOL guide
|
| 39 |
+
⚠️ Do not summarize. Only copy directly from the context.
|
| 40 |
+
|
| 41 |
+
- If the user asks for a **lesson plan**, provide:
|
| 42 |
+
- Simple explanation of the concept
|
| 43 |
+
- Real-world example
|
| 44 |
+
- Engaging class activity
|
| 45 |
+
Format the output clearly with bullet points.
|
| 46 |
+
|
| 47 |
+
- If the user asks for a **worksheet**, include:
|
| 48 |
+
- Concept summary
|
| 49 |
+
- A worked example
|
| 50 |
+
- 3 practice problems
|
| 51 |
+
Format the output clearly with bullet points.
|
| 52 |
+
|
| 53 |
+
- If the user asks for **proofs**, include:
|
| 54 |
+
- Student-friendly explanation
|
| 55 |
+
- Real-world connection
|
| 56 |
+
- One short class activity
|
| 57 |
+
Format the output clearly with bullet points.
|
| 58 |
+
|
| 59 |
+
- If the user asks for **flashcards**, generate 5 cards, each with:
|
| 60 |
+
- A clear question
|
| 61 |
+
- A short answer
|
| 62 |
+
Format the output clearly with bullet points.
|
| 63 |
+
|
| 64 |
+
Only answer one way depending on the intent of the question.
|
| 65 |
"""
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Optional: shortcut to solve simple math problems (like area of rectangle)
|
| 69 |
def try_math_solver(query):
|
|
|
|
| 73 |
return f"The area of the rectangle is {l} × {w} = {l * w} square units."
|
| 74 |
return None
|
| 75 |
|
| 76 |
+
# RAG function using unified intent-aware prompt
|
| 77 |
+
def rag_query(query):
|
| 78 |
docs = vector_store.similarity_search(query, k=2)
|
| 79 |
context = "\n\n".join([doc.page_content for doc in docs])
|
| 80 |
+
prompt = template.format_prompt(context=context, query=query).to_string()
|
| 81 |
return llm.invoke(prompt).content
|
| 82 |
|
| 83 |
# Gradio app function
|
| 84 |
+
def ask_geometry_sol(query):
|
| 85 |
math_result = try_math_solver(query)
|
| 86 |
if math_result:
|
| 87 |
return math_result
|
| 88 |
try:
|
| 89 |
+
return rag_query(query)
|
| 90 |
except Exception as e:
|
| 91 |
return f"⚠️ Error: {type(e).__name__} - {str(e)}"
|
| 92 |
|
| 93 |
+
# Gradio UI (no need for manual response type selection anymore!)
|
| 94 |
iface = gr.Interface(
|
| 95 |
fn=ask_geometry_sol,
|
| 96 |
+
inputs=gr.Textbox(label="Enter your Geometry SOL question or topic"),
|
|
|
|
|
|
|
|
|
|
| 97 |
outputs="text",
|
| 98 |
title="📘 Virginia Geometry SOL Assistant",
|
| 99 |
+
description="Ask about any 2023 Geometry SOL (Standards of Learning). The assistant will auto-detect if you want a lesson plan, worksheet, proof, flashcards, or SOL reference."
|
| 100 |
)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
+
iface.launch()
|