Spaces:
Sleeping
Sleeping
added spaces import.
Browse files
main.py
CHANGED
|
@@ -1,89 +1,90 @@
|
|
| 1 |
-
# main.py
|
| 2 |
-
|
| 3 |
-
import subprocess
|
| 4 |
-
import sys
|
| 5 |
-
import webbrowser
|
| 6 |
-
import time
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
from
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
import webbrowser
|
| 6 |
+
import time
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import spaces
|
| 9 |
+
from question_runner import run_tool
|
| 10 |
+
from config import MODEL_PRIORITY, SYNTAX_DOC_URL, MORPHOLOGY_DOC_URL
|
| 11 |
+
from doc_utils import get_questions_from_doc
|
| 12 |
+
|
| 13 |
+
# Auto-install required packages if missing
|
| 14 |
+
def install_missing_packages():
|
| 15 |
+
from importlib.metadata import distributions
|
| 16 |
+
required = {"gradio", "requests"}
|
| 17 |
+
installed = {dist.metadata['Name'].lower() for dist in distributions()}
|
| 18 |
+
missing = required - installed
|
| 19 |
+
|
| 20 |
+
if missing:
|
| 21 |
+
print(f"Installing missing packages: {missing}")
|
| 22 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", *missing])
|
| 23 |
+
|
| 24 |
+
install_missing_packages()
|
| 25 |
+
|
| 26 |
+
# Estimate runtime based on # of questions
|
| 27 |
+
def estimate_runtime(passage, doc_type):
|
| 28 |
+
if not passage.strip() or not doc_type:
|
| 29 |
+
return ""
|
| 30 |
+
doc_url = SYNTAX_DOC_URL if doc_type.lower() == "syntax" else MORPHOLOGY_DOC_URL
|
| 31 |
+
questions = get_questions_from_doc(doc_url)
|
| 32 |
+
if not questions or questions[0].startswith("Error"):
|
| 33 |
+
return "Unable to load questions."
|
| 34 |
+
est_seconds = round(len(questions) * 2.5, 1)
|
| 35 |
+
return f"Estimated generation time: ~{est_seconds} seconds"
|
| 36 |
+
|
| 37 |
+
def launch_app():
|
| 38 |
+
with gr.Blocks(theme="soft") as demo:
|
| 39 |
+
gr.Markdown("""
|
| 40 |
+
## **Classical Language Query Assistant**
|
| 41 |
+
Submit a Latin or Greek passage and select the question type.
|
| 42 |
+
Answers are generated using a rotating chain of hosted AI models via OpenRouter.
|
| 43 |
+
|
| 44 |
+
- Models are attempted in descending priority, starting from the most accurate.
|
| 45 |
+
- The model that answers each question is recorded in the response.
|
| 46 |
+
- Model quota or errors may trigger automatic fallback to the next-best option.
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
passage_input = gr.Textbox(label="Latin or Greek Passage", lines=4)
|
| 51 |
+
question_type = gr.Radio(["Syntax", "Morphology"], label="Question Type")
|
| 52 |
+
|
| 53 |
+
top_model = MODEL_PRIORITY[0]
|
| 54 |
+
full_model_list = "\n".join(f"- `{m}`" for m in MODEL_PRIORITY)
|
| 55 |
+
demo_model_info = gr.Markdown(
|
| 56 |
+
f"""
|
| 57 |
+
**Currently prioritized model:** `{top_model}`
|
| 58 |
+
**Model fallback chain (if needed):**
|
| 59 |
+
{full_model_list}
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
output_text = gr.Textbox(label="Generated Answers", lines=25, interactive=False)
|
| 64 |
+
output_file = gr.File(label="Download Answers (.txt)", interactive=False)
|
| 65 |
+
|
| 66 |
+
estimated_time_box = gr.Textbox(label="Estimated Time", interactive=False)
|
| 67 |
+
|
| 68 |
+
# Trigger time estimate dynamically
|
| 69 |
+
passage_input.change(fn=estimate_runtime, inputs=[passage_input, question_type], outputs=estimated_time_box)
|
| 70 |
+
question_type.change(fn=estimate_runtime, inputs=[passage_input, question_type], outputs=estimated_time_box)
|
| 71 |
+
|
| 72 |
+
submit_button = gr.Button("Generate Answers")
|
| 73 |
+
|
| 74 |
+
submit_button.click(
|
| 75 |
+
fn=run_tool,
|
| 76 |
+
inputs=[passage_input, question_type],
|
| 77 |
+
outputs=[output_text, output_file, estimated_time_box]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Launch app and open browser
|
| 81 |
+
_, _, share_url = demo.launch(share=True, prevent_thread_lock=True)
|
| 82 |
+
if share_url:
|
| 83 |
+
webbrowser.open(share_url)
|
| 84 |
+
|
| 85 |
+
# Keep app running
|
| 86 |
+
while True:
|
| 87 |
+
time.sleep(1)
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
launch_app()
|