Spaces:
Running on Zero
Running on Zero
| """ | |
| app.py โ NLP4ASD Gradio interface. | |
| Entry point for both local use and Hugging Face Spaces deployment. | |
| On first startup the knowledge base is built automatically if missing. | |
| """ | |
| import spaces | |
| import gradio as gr | |
| from config import PROFILES, LANGUAGES, VECTOR_INDEX_PATH | |
| from utils import file_exists | |
| from rag_pipeline import answer, build_knowledge_base | |
| # โโ Auto-build index on startup โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _ensure_index(): | |
| if not file_exists(VECTOR_INDEX_PATH): | |
| print("[app] Index not found โ building knowledge base on first startup...") | |
| try: | |
| build_knowledge_base() | |
| print("[app] Knowledge base ready.") | |
| except Exception as e: | |
| print(f"[app] โ ๏ธ Could not build index automatically: {e}") | |
| print("[app] Run manually: python build_knowledge_base.py") | |
| _ensure_index() | |
| # โโ Query handler โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def query_handler(question: str, profile: str, language: str): | |
| """Called by Gradio on each submission. Returns (answer, sources, disclaimer).""" | |
| if not question.strip(): | |
| return "Please enter a question.", "", "" | |
| result = answer(question, profile, language) | |
| if result["error"]: | |
| return f"โ ๏ธ {result['error']}", "", "" | |
| return result["answer"], result["sources"], result["disclaimer"] | |
| def rebuild_handler(): | |
| """Called by the Rebuild button in the Advanced accordion.""" | |
| try: | |
| build_knowledge_base() | |
| return "โ Knowledge base rebuilt successfully." | |
| except Exception as e: | |
| return f"โ {e}" | |
| # โโ Gradio UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| _DESCRIPTION = """ | |
| ## ๐งฉ NLP4ASD โ Autism Spectrum Disorder Assistant | |
| Answers questions about ASD using **retrieved scientific sources**. | |
| Response style adapts automatically to your selected profile. | |
| 1. Choose your **profile** and **language** | |
| 2. Type your **question** | |
| 3. The system retrieves relevant passages and generates a grounded answer | |
| """ | |
| with gr.Blocks( | |
| theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue", neutral_hue="slate"), | |
| title="NLP4ASD", | |
| css=""" | |
| .answer-box textarea { font-size:15px; line-height:1.7; } | |
| .source-box textarea { font-size:13px; color:#555; } | |
| .disclaimer textarea { font-size:12px; color:#888; font-style:italic; } | |
| footer { display:none !important; } | |
| """, | |
| ) as demo: | |
| gr.Markdown(_DESCRIPTION) | |
| with gr.Row(): | |
| # โโ Left column: inputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| with gr.Column(scale=1): | |
| profile_input = gr.Dropdown( | |
| choices=PROFILES, value=PROFILES[0], | |
| label="๐ค Your Profile", | |
| info="The answer style will adapt to your background.", | |
| ) | |
| language_input = gr.Dropdown( | |
| choices=LANGUAGES, value=LANGUAGES[0], | |
| label="๐ Language", | |
| ) | |
| question_input = gr.Textbox( | |
| label="โ Your Question", | |
| placeholder="e.g. What are the early signs of autism in toddlers?", | |
| lines=4, | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("๐ Ask", variant="primary", scale=3) | |
| clear_btn = gr.Button("๐ Clear", scale=1) | |
| # โโ Right column: outputs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| with gr.Column(scale=2): | |
| answer_output = gr.Textbox( | |
| label="๐ฌ Answer", | |
| lines=10, | |
| interactive=False, | |
| elem_classes=["answer-box"], | |
| ) | |
| sources_output = gr.Textbox( | |
| label="๐ Sources", | |
| lines=4, | |
| interactive=False, | |
| elem_classes=["source-box"], | |
| ) | |
| disclaimer_output = gr.Textbox( | |
| label="โ ๏ธ Disclaimer", | |
| lines=2, | |
| interactive=False, | |
| elem_classes=["disclaimer"], | |
| ) | |
| # โโ Example questions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| gr.Examples( | |
| examples=[ | |
| ["What are the early signs of autism in toddlers?", "Parent", "English"], | |
| ["How does sensory overload affect autistic people day to day?", "Patient / Autistic person", "English"], | |
| ["What pharmacological treatments are evidence-based for ASD?", "Healthcare Professional", "English"], | |
| ["What classroom accommodations help autistic students the most?", "Teacher / Educator", "English"], | |
| ["What does the genetic heritability research say about ASD?", "Researcher", "English"], | |
| ["Qu'est-ce que le burnout autistique et comment le reconnaรฎtre ?", "Patient / Autistic person", "French"], | |
| ], | |
| inputs=[question_input, profile_input, language_input], | |
| label="Example Questions", | |
| ) | |
| # โโ Advanced: rebuild index โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| with gr.Accordion("๐ง Advanced โ Rebuild Knowledge Base", open=False): | |
| gr.Markdown( | |
| "Use this after adding new `.txt` files to `data/raw/`. " | |
| "Rebuilds the FAISS index from scratch." | |
| ) | |
| rebuild_btn = gr.Button("โป๏ธ Rebuild Index", variant="secondary") | |
| rebuild_status = gr.Textbox(label="Status", interactive=False, lines=2) | |
| rebuild_btn.click(rebuild_handler, outputs=rebuild_status) | |
| # โโ Wire up events โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| submit_btn.click( | |
| query_handler, | |
| inputs=[question_input, profile_input, language_input], | |
| outputs=[answer_output, sources_output, disclaimer_output], | |
| ) | |
| question_input.submit( | |
| query_handler, | |
| inputs=[question_input, profile_input, language_input], | |
| outputs=[answer_output, sources_output, disclaimer_output], | |
| ) | |
| clear_btn.click( | |
| lambda: ("", "", "", ""), | |
| outputs=[question_input, answer_output, sources_output, disclaimer_output], | |
| ) | |
| gr.Markdown( | |
| "---\n*NLP4ASD is a research prototype. " | |
| "Always consult a qualified professional for medical decisions.*" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(show_error=True) |