Spaces:
Runtime error
Runtime error
thomas cerniglia
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from docx import Document
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import tempfile
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Load Mistral model
|
| 8 |
+
chatbot = pipeline(
|
| 9 |
+
"text-generation",
|
| 10 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 11 |
+
device_map="auto",
|
| 12 |
+
trust_remote_code=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Read .docx file and extract questions
|
| 16 |
+
def read_questions_from_docx(document_path):
|
| 17 |
+
doc = Document(document_path)
|
| 18 |
+
return [p.text.strip() for p in doc.paragraphs if p.text.strip().endswith("?")]
|
| 19 |
+
|
| 20 |
+
# --- Load .docx once into memory ---
|
| 21 |
+
syntax_questions = read_questions_from_docx("Syntax Questions.docx")
|
| 22 |
+
morphology_questions = read_questions_from_docx("Morphology Questions.docx")
|
| 23 |
+
|
| 24 |
+
# Main function
|
| 25 |
+
def run_tool(passage, question_type):
|
| 26 |
+
if not passage.strip():
|
| 27 |
+
return "Please enter a passage to analyze.", None, None
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
questions = syntax_questions if question_type.lower() == "syntax" else morphology_questions
|
| 31 |
+
if not questions:
|
| 32 |
+
return "No valid questions found.", None, None
|
| 33 |
+
|
| 34 |
+
est_seconds = round(len(questions) * 3.5, 1)
|
| 35 |
+
estimated_time = f"Estimated generation time: ~{est_seconds} seconds"
|
| 36 |
+
|
| 37 |
+
prompt = f"You are a classical language expert.\n\nHere is the passage:\n{passage}\n\n"
|
| 38 |
+
prompt += "Answer the following questions clearly and completely:\n\n"
|
| 39 |
+
for idx, q in enumerate(questions):
|
| 40 |
+
prompt += f"Q{idx+1}: {q}\n"
|
| 41 |
+
|
| 42 |
+
result = chatbot(prompt, max_new_tokens=1600, do_sample=False)[0]["generated_text"].strip()
|
| 43 |
+
|
| 44 |
+
# Save result to .txt
|
| 45 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
|
| 46 |
+
f.write(result)
|
| 47 |
+
file_path = f.name
|
| 48 |
+
|
| 49 |
+
return result, file_path, estimated_time
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"An error occurred: {str(e)}", None, None
|
| 53 |
+
|
| 54 |
+
# --- Gradio UI ---
|
| 55 |
+
with gr.Blocks(theme="soft") as demo:
|
| 56 |
+
gr.Markdown("""
|
| 57 |
+
## **Classical Language Query Assistant**
|
| 58 |
+
Submit a Latin or Greek passage and select whether to analyze **syntax** or **morphology**.
|
| 59 |
+
Answers are generated using **Mistral-7B-Instruct**, hosted via Hugging Face.
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
passage_input = gr.Textbox(label="Latin or Greek Passage", placeholder="Paste your passage here...", lines=4)
|
| 64 |
+
question_type = gr.Radio(["Syntax", "Morphology"], label="Select Question Set")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
output_text = gr.Textbox(label="Generated Answers", lines=25, interactive=False)
|
| 68 |
+
output_file = gr.File(label="Download Answers (.txt)", interactive=False)
|
| 69 |
+
|
| 70 |
+
est_time = gr.Textbox(label="Estimated Time", interactive=False)
|
| 71 |
+
|
| 72 |
+
submit = gr.Button("Generate Answers")
|
| 73 |
+
submit.click(fn=run_tool, inputs=[passage_input, question_type], outputs=[output_text, output_file, est_time])
|
| 74 |
+
|
| 75 |
+
demo.launch()
|