Delete app.py
Browse files
app.py
DELETED
|
@@ -1,214 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Gradio web interface for the Medical RAG (Retrieval-Augmented Generation) system.
|
| 3 |
-
|
| 4 |
-
This application provides a simple interface to ask medical questions and receive
|
| 5 |
-
answers retrieved from a medical knowledge base and augmented with a fine-tuned LLM.
|
| 6 |
-
|
| 7 |
-
Setup:
|
| 8 |
-
1. Ensure you have the vector database loaded at ./MedQuAD_db
|
| 9 |
-
2. Update the FINE_TUNED_MODEL_ID in model.py to point to your HuggingFace model
|
| 10 |
-
3. Install dependencies: pip install -r requirements.txt
|
| 11 |
-
4. Run: python app.py
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
import gradio as gr
|
| 15 |
-
from model import initialize_all, rag_pipeline
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Initialize all models on startup
|
| 19 |
-
print("=" * 60)
|
| 20 |
-
print("MEDICAL RAG SYSTEM - Initializing...")
|
| 21 |
-
print("=" * 60)
|
| 22 |
-
try:
|
| 23 |
-
initialize_all()
|
| 24 |
-
print("✓ All models loaded successfully!")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"✗ Error during initialization: {e}")
|
| 27 |
-
print("Some models may not be available. The app may still run with reduced functionality.")
|
| 28 |
-
|
| 29 |
-
print("=" * 60)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# ===========================
|
| 33 |
-
# GRADIO INTERFACE FUNCTIONS
|
| 34 |
-
# ===========================
|
| 35 |
-
|
| 36 |
-
def process_query(user_question, show_context):
|
| 37 |
-
"""
|
| 38 |
-
Process user question through RAG pipeline.
|
| 39 |
-
|
| 40 |
-
Args:
|
| 41 |
-
user_question (str): The user's medical question
|
| 42 |
-
show_context (bool): Whether to show retrieved context details
|
| 43 |
-
|
| 44 |
-
Returns:
|
| 45 |
-
str: Generated answer with confidence score
|
| 46 |
-
"""
|
| 47 |
-
if not user_question.strip():
|
| 48 |
-
return "Please enter a medical question."
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
answer = rag_pipeline(user_question, top_k=3, detail=show_context)
|
| 52 |
-
return answer
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return f"Error processing query: {str(e)}"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def example_questions():
|
| 58 |
-
"""Return example questions for users."""
|
| 59 |
-
return [
|
| 60 |
-
"What are the symptoms of type 2 diabetes?",
|
| 61 |
-
"How is hypertension treated?",
|
| 62 |
-
"What causes migraines?",
|
| 63 |
-
"What are the risk factors for heart disease?",
|
| 64 |
-
"How do I manage chronic pain?"
|
| 65 |
-
]
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# ===========================
|
| 69 |
-
# GRADIO APP CONFIGURATION
|
| 70 |
-
# ===========================
|
| 71 |
-
|
| 72 |
-
with gr.Blocks(
|
| 73 |
-
title="Medical Q&A System",
|
| 74 |
-
theme=gr.themes.Soft(),
|
| 75 |
-
css="""
|
| 76 |
-
.header-text {
|
| 77 |
-
text-align: center;
|
| 78 |
-
padding: 20px;
|
| 79 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 80 |
-
border-radius: 10px;
|
| 81 |
-
color: white;
|
| 82 |
-
}
|
| 83 |
-
.info-box {
|
| 84 |
-
background-color: #f0f0f0;
|
| 85 |
-
border-left: 4px solid #667eea;
|
| 86 |
-
padding: 15px;
|
| 87 |
-
margin: 10px 0;
|
| 88 |
-
border-radius: 5px;
|
| 89 |
-
}
|
| 90 |
-
"""
|
| 91 |
-
) as demo:
|
| 92 |
-
|
| 93 |
-
# Header
|
| 94 |
-
gr.HTML("""
|
| 95 |
-
<div class="header-text">
|
| 96 |
-
<h1>🏥 Medical Question & Answer System</h1>
|
| 97 |
-
<p>Powered by RAG (Retrieval-Augmented Generation) with Fine-tuned FLAN-T5</p>
|
| 98 |
-
</div>
|
| 99 |
-
""")
|
| 100 |
-
|
| 101 |
-
# Information box
|
| 102 |
-
gr.HTML("""
|
| 103 |
-
<div class="info-box">
|
| 104 |
-
<strong>⚠️ Disclaimer:</strong> This system is designed for informational purposes only.
|
| 105 |
-
It is NOT a substitute for professional medical advice, diagnosis, or treatment.
|
| 106 |
-
Always consult with a qualified healthcare provider for medical concerns.
|
| 107 |
-
</div>
|
| 108 |
-
""")
|
| 109 |
-
|
| 110 |
-
# Main interface
|
| 111 |
-
with gr.Group():
|
| 112 |
-
gr.Markdown("### Ask a Medical Question")
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
with gr.Column(scale=4):
|
| 116 |
-
question_input = gr.Textbox(
|
| 117 |
-
label="Your Question",
|
| 118 |
-
placeholder="e.g., What are the symptoms of diabetes?",
|
| 119 |
-
lines=3,
|
| 120 |
-
info="Enter your medical question in natural language."
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
with gr.Column(scale=1):
|
| 124 |
-
show_details = gr.Checkbox(
|
| 125 |
-
label="Show Retrieved Context",
|
| 126 |
-
value=False,
|
| 127 |
-
info="Display source documents"
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
submit_btn = gr.Button(
|
| 131 |
-
"Get Answer",
|
| 132 |
-
variant="primary",
|
| 133 |
-
size="lg"
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
answer_output = gr.Textbox(
|
| 137 |
-
label="Answer",
|
| 138 |
-
interactive=False,
|
| 139 |
-
lines=8,
|
| 140 |
-
show_copy_button=True
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
# Examples section
|
| 144 |
-
gr.Markdown("### 📋 Example Questions")
|
| 145 |
-
|
| 146 |
-
examples = example_questions()
|
| 147 |
-
|
| 148 |
-
gr.Examples(
|
| 149 |
-
examples=[[q] for q in examples],
|
| 150 |
-
inputs=[question_input],
|
| 151 |
-
label="Click an example to try it:",
|
| 152 |
-
run_on_click=False
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
# Information section
|
| 156 |
-
with gr.Accordion("ℹ️ How This System Works", open=False):
|
| 157 |
-
gr.Markdown("""
|
| 158 |
-
This Medical Q&A system uses **Retrieval-Augmented Generation (RAG)**:
|
| 159 |
-
|
| 160 |
-
1. **Retrieval**: Your question is matched against a knowledge base of medical documents
|
| 161 |
-
2. **Augmentation**: The retrieved documents provide context for generating an accurate answer
|
| 162 |
-
3. **Generation**: A fine-tuned language model generates an answer based on the retrieved context
|
| 163 |
-
4. **Confidence**: The system calculates confidence based on how well the retrieved documents match your question
|
| 164 |
-
|
| 165 |
-
**Key Features:**
|
| 166 |
-
- ✓ Query rewriting for better search
|
| 167 |
-
- ✓ Semantic search using embeddings
|
| 168 |
-
- ✓ Context re-ranking for relevance
|
| 169 |
-
- ✓ Fine-tuned medical model
|
| 170 |
-
- ✓ Confidence scoring
|
| 171 |
-
""")
|
| 172 |
-
|
| 173 |
-
with gr.Accordion("⚙️ System Configuration", open=False):
|
| 174 |
-
gr.Markdown("""
|
| 175 |
-
**Models Used:**
|
| 176 |
-
- **Embeddings**: sentence-transformers/all-MiniLM-L6-v2
|
| 177 |
-
- **Query Rewriter**: google/flan-t5-small
|
| 178 |
-
- **Reranker**: castorini/monot5-base-msmarco
|
| 179 |
-
- **Generator**: Fine-tuned google/flan-t5-small
|
| 180 |
-
|
| 181 |
-
**Vector Database:**
|
| 182 |
-
- ChromaDB with persistent storage
|
| 183 |
-
- ~5000+ medical Q&A chunks from MedQuAD dataset
|
| 184 |
-
""")
|
| 185 |
-
|
| 186 |
-
# Set up interactions
|
| 187 |
-
submit_btn.click(
|
| 188 |
-
fn=process_query,
|
| 189 |
-
inputs=[question_input, show_details],
|
| 190 |
-
outputs=answer_output,
|
| 191 |
-
api_name="ask"
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
# Allow Enter key to submit
|
| 195 |
-
question_input.submit(
|
| 196 |
-
fn=process_query,
|
| 197 |
-
inputs=[question_input, show_details],
|
| 198 |
-
outputs=answer_output
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
# ===========================
|
| 203 |
-
# LAUNCH CONFIGURATION
|
| 204 |
-
# ===========================
|
| 205 |
-
|
| 206 |
-
if __name__ == "__main__":
|
| 207 |
-
demo.launch(
|
| 208 |
-
server_name="0.0.0.0",
|
| 209 |
-
server_port=7860,
|
| 210 |
-
share=False,
|
| 211 |
-
show_error=True,
|
| 212 |
-
show_tips=True,
|
| 213 |
-
debug=False
|
| 214 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|