Spaces:
Sleeping
Sleeping
File size: 2,166 Bytes
b0730a4 fd097e2 b0730a4 fd097e2 b0730a4 fd097e2 b0730a4 |
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 |
# question_runner.py
import tempfile
from router_client import query_model
from doc_utils import get_questions_from_doc
from config import SYNTAX_DOC_URL, MORPHOLOGY_DOC_URL, SEMANTICS_DOC_URL
def run_tool(passage, doc_type):
if not passage.strip():
return "Please enter a passage to analyze.", None, None
if not doc_type:
return "Please select 'Syntax', 'Morphology', or 'Semantics'.", None, None
try:
if doc_type.lower() == "syntax":
doc_url = SYNTAX_DOC_URL
elif doc_type.lower() == "morphology":
doc_url = MORPHOLOGY_DOC_URL
else: # semantics
doc_url = SEMANTICS_DOC_URL
questions = get_questions_from_doc(doc_url)
if not questions or questions[0].startswith("Error"):
return questions[0], None, None
est_seconds = round(len(questions) * 2.5, 1)
estimated_time_message = f"Estimated generation time: ~{est_seconds} seconds"
responses = []
for idx, question in enumerate(questions):
prompt = f"""You are a classical language expert.
Given the following Latin or Greek passage:
{passage}
Answer the following question:
{question}
Answer:"""
raw_response, model_used = query_model(prompt)
if not raw_response or not model_used:
formatted_block = f"""Question: {question.strip()}
Answer:
<No answer – all models failed or quota exceeded.>
===
"""
else:
answer = raw_response.split("Answer:")[-1].strip()
formatted_block = f"""Question: {question.strip()}
Answer:
{answer}
Model used: {model_used}
===""" # Separator for logic tree parsing
responses.append(formatted_block)
result = "\n\n".join(responses)
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
f.write(result)
file_path = f.name
return result, file_path, estimated_time_message
except Exception as e:
return f"An error occurred: {str(e)}", None, None
|