Spaces:
Sleeping
Sleeping
fixed main
Browse files
main.py
CHANGED
|
@@ -1,49 +1,35 @@
|
|
| 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 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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
|
| 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
|
| 38 |
-
with gr.Blocks(theme=
|
| 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():
|
|
@@ -52,8 +38,7 @@ def launch_app():
|
|
| 52 |
|
| 53 |
top_model = MODEL_PRIORITY[0]
|
| 54 |
full_model_list = "\n".join(f"- `{m}`" for m in MODEL_PRIORITY)
|
| 55 |
-
|
| 56 |
-
f"""
|
| 57 |
**Currently prioritized model:** `{top_model}`
|
| 58 |
**Model fallback chain (if needed):**
|
| 59 |
{full_model_list}
|
|
@@ -65,26 +50,20 @@ def launch_app():
|
|
| 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=
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
# main.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from gradio.themes import Soft
|
| 4 |
import spaces
|
| 5 |
+
|
| 6 |
from question_runner import run_tool
|
| 7 |
from config import MODEL_PRIORITY, SYNTAX_DOC_URL, MORPHOLOGY_DOC_URL
|
| 8 |
from doc_utils import get_questions_from_doc
|
| 9 |
|
| 10 |
+
# --- ZERO-GPU ENTRYPOINT (must be top-level and referenced by Gradio) ---
|
| 11 |
+
@spaces.GPU
|
| 12 |
+
def run_query(passage: str, doc_type: str):
|
| 13 |
+
# Delegate to your existing business logic
|
| 14 |
+
return run_tool(passage, doc_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Estimate runtime based on # of questions
|
| 17 |
def estimate_runtime(passage, doc_type):
|
| 18 |
+
if not passage or not doc_type:
|
| 19 |
return ""
|
| 20 |
doc_url = SYNTAX_DOC_URL if doc_type.lower() == "syntax" else MORPHOLOGY_DOC_URL
|
| 21 |
questions = get_questions_from_doc(doc_url)
|
| 22 |
+
if not questions or (isinstance(questions, list) and questions and str(questions[0]).startswith("Error")):
|
| 23 |
return "Unable to load questions."
|
| 24 |
est_seconds = round(len(questions) * 2.5, 1)
|
| 25 |
return f"Estimated generation time: ~{est_seconds} seconds"
|
| 26 |
|
| 27 |
+
def build_app():
|
| 28 |
+
with gr.Blocks(theme=Soft()) as demo:
|
| 29 |
gr.Markdown("""
|
| 30 |
## **Classical Language Query Assistant**
|
| 31 |
Submit a Latin or Greek passage and select the question type.
|
| 32 |
Answers are generated using a rotating chain of hosted AI models via OpenRouter.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
""")
|
| 34 |
|
| 35 |
with gr.Row():
|
|
|
|
| 38 |
|
| 39 |
top_model = MODEL_PRIORITY[0]
|
| 40 |
full_model_list = "\n".join(f"- `{m}`" for m in MODEL_PRIORITY)
|
| 41 |
+
gr.Markdown(f"""
|
|
|
|
| 42 |
**Currently prioritized model:** `{top_model}`
|
| 43 |
**Model fallback chain (if needed):**
|
| 44 |
{full_model_list}
|
|
|
|
| 50 |
|
| 51 |
estimated_time_box = gr.Textbox(label="Estimated Time", interactive=False)
|
| 52 |
|
|
|
|
| 53 |
passage_input.change(fn=estimate_runtime, inputs=[passage_input, question_type], outputs=estimated_time_box)
|
| 54 |
question_type.change(fn=estimate_runtime, inputs=[passage_input, question_type], outputs=estimated_time_box)
|
| 55 |
|
| 56 |
submit_button = gr.Button("Generate Answers")
|
| 57 |
|
| 58 |
+
# IMPORTANT: point Gradio at the decorated function
|
| 59 |
submit_button.click(
|
| 60 |
+
fn=run_query,
|
| 61 |
inputs=[passage_input, question_type],
|
| 62 |
+
outputs=[output_text, output_file, estimated_time_box],
|
| 63 |
)
|
| 64 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
# On Spaces: no share=True, no webbrowser.open, no infinite loop
|
| 68 |
+
app = build_app()
|
| 69 |
+
app.launch()
|