thomascerniglia commited on
Commit
e7c1df9
·
verified ·
1 Parent(s): efbc1cc

added spaces import.

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