Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,11 +97,32 @@ Do not summarize. Copy the official description exactly.
|
|
| 97 |
}
|
| 98 |
|
| 99 |
def generate_output(prompt_type, query):
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
chain = LLMChain(llm=llm, prompt=templates[prompt_type])
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
|
| 107 |
# ✅ Gradio UI
|
|
@@ -112,7 +133,7 @@ with gr.Blocks() as demo:
|
|
| 112 |
query = gr.Textbox(label="Enter a geometry topic")
|
| 113 |
prompt_type = gr.Dropdown(
|
| 114 |
["general question", "lesson plan", "worksheet", "proofs", "flashcard"],
|
| 115 |
-
value="
|
| 116 |
label="Prompt Type"
|
| 117 |
)
|
| 118 |
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
def generate_output(prompt_type, query):
|
| 100 |
+
import re
|
| 101 |
+
|
| 102 |
+
# Check for exact SOL code in query
|
| 103 |
+
sol_match = re.search(r"\bG\.[A-Z]+\.\d+\b", query)
|
| 104 |
+
matched_code = sol_match.group(0) if sol_match else None
|
| 105 |
+
|
| 106 |
+
if matched_code:
|
| 107 |
+
# Find matching document by metadata
|
| 108 |
+
docs_with_code = vectordb.similarity_search_with_score(query)
|
| 109 |
+
filtered_docs = [doc for doc, _ in docs_with_code if doc.metadata.get("standard") == matched_code]
|
| 110 |
+
|
| 111 |
+
if filtered_docs:
|
| 112 |
+
context = "\n\n".join([doc.page_content for doc in filtered_docs])
|
| 113 |
+
else:
|
| 114 |
+
# fallback to regular retrieval if not found
|
| 115 |
+
docs = retriever.get_relevant_documents(query)
|
| 116 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 117 |
+
else:
|
| 118 |
+
# regular query
|
| 119 |
+
docs = retriever.get_relevant_documents(query)
|
| 120 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 121 |
+
|
| 122 |
+
# Run the prompt
|
| 123 |
chain = LLMChain(llm=llm, prompt=templates[prompt_type])
|
| 124 |
+
return chain.run({"context": context, "query": query}).strip()
|
| 125 |
+
|
| 126 |
|
| 127 |
|
| 128 |
# ✅ Gradio UI
|
|
|
|
| 133 |
query = gr.Textbox(label="Enter a geometry topic")
|
| 134 |
prompt_type = gr.Dropdown(
|
| 135 |
["general question", "lesson plan", "worksheet", "proofs", "flashcard"],
|
| 136 |
+
value="general question",
|
| 137 |
label="Prompt Type"
|
| 138 |
)
|
| 139 |
|