Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
9ae5fda
1
Parent(s):
117a288
uppdate app and requirement.txt
Browse files- app.py +33 -20
- requirements.txt +38 -31
app.py
CHANGED
@@ -1,17 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
import spaces
|
3 |
import torch
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
from langgraph.graph import StateGraph, END
|
6 |
-
from typing import TypedDict, List, Dict
|
7 |
from datetime import datetime
|
8 |
-
import json
|
9 |
import re
|
10 |
-
import numpy as np
|
11 |
-
from sentence_transformers import SentenceTransformer
|
12 |
-
import faiss
|
13 |
-
import pickle
|
14 |
-
import os
|
15 |
|
16 |
# Enhanced State Management with RAG
|
17 |
class MedicalState(TypedDict):
|
@@ -127,7 +120,7 @@ class EnhancedMedicalAssistant:
|
|
127 |
self.conversation_count = {}
|
128 |
|
129 |
def load_models(self):
|
130 |
-
"""Load the AI models"""
|
131 |
print("Loading models...")
|
132 |
try:
|
133 |
# Llama-2 for conversation
|
@@ -152,16 +145,17 @@ class EnhancedMedicalAssistant:
|
|
152 |
device_map="auto"
|
153 |
)
|
154 |
print("Models loaded successfully!")
|
|
|
155 |
except Exception as e:
|
156 |
print(f"Error loading models: {e}")
|
157 |
-
# Fallback - use
|
158 |
self.tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
159 |
self.model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
160 |
self.meditron_tokenizer = self.tokenizer
|
161 |
self.meditron_model = self.model
|
162 |
|
163 |
def setup_langgraph(self):
|
164 |
-
"""Setup
|
165 |
workflow = StateGraph(MedicalState)
|
166 |
|
167 |
workflow.add_node("intake", self.patient_intake)
|
@@ -466,7 +460,7 @@ Be professional and caring.
|
|
466 |
# Initialize the medical assistant
|
467 |
medical_assistant = EnhancedMedicalAssistant()
|
468 |
|
469 |
-
|
470 |
def chat_interface(message, history):
|
471 |
"""Gradio chat interface"""
|
472 |
try:
|
@@ -475,7 +469,7 @@ def chat_interface(message, history):
|
|
475 |
print(f"Chat interface error: {e}")
|
476 |
return f"I apologize, but I encountered an error. Please try rephrasing your question. Error: {str(e)}"
|
477 |
|
478 |
-
# Create Gradio interface
|
479 |
demo = gr.ChatInterface(
|
480 |
fn=chat_interface,
|
481 |
title="🏥 Medical AI Assistant with medRAG",
|
@@ -498,20 +492,39 @@ demo = gr.ChatInterface(
|
|
498 |
],
|
499 |
theme="soft",
|
500 |
css="""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
501 |
.message.user {
|
502 |
background-color: #e3f2fd;
|
503 |
-
border-radius:
|
504 |
-
padding:
|
505 |
-
margin:
|
|
|
|
|
506 |
}
|
|
|
507 |
.message.bot {
|
508 |
background-color: #f1f8e9;
|
509 |
-
border-radius:
|
510 |
-
padding:
|
511 |
-
margin:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
}
|
513 |
"""
|
514 |
)
|
515 |
|
516 |
if __name__ == "__main__":
|
517 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from langgraph.graph import StateGraph, END
|
5 |
+
from typing import TypedDict, List, Dict
|
6 |
from datetime import datetime
|
|
|
7 |
import re
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Enhanced State Management with RAG
|
10 |
class MedicalState(TypedDict):
|
|
|
120 |
self.conversation_count = {}
|
121 |
|
122 |
def load_models(self):
|
123 |
+
"""Load the AI models with fallback options"""
|
124 |
print("Loading models...")
|
125 |
try:
|
126 |
# Llama-2 for conversation
|
|
|
145 |
device_map="auto"
|
146 |
)
|
147 |
print("Models loaded successfully!")
|
148 |
+
|
149 |
except Exception as e:
|
150 |
print(f"Error loading models: {e}")
|
151 |
+
# Fallback - use smaller models
|
152 |
self.tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
153 |
self.model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
154 |
self.meditron_tokenizer = self.tokenizer
|
155 |
self.meditron_model = self.model
|
156 |
|
157 |
def setup_langgraph(self):
|
158 |
+
"""Setup LangGraph workflow"""
|
159 |
workflow = StateGraph(MedicalState)
|
160 |
|
161 |
workflow.add_node("intake", self.patient_intake)
|
|
|
460 |
# Initialize the medical assistant
|
461 |
medical_assistant = EnhancedMedicalAssistant()
|
462 |
|
463 |
+
# Gradio chat interface function
|
464 |
def chat_interface(message, history):
|
465 |
"""Gradio chat interface"""
|
466 |
try:
|
|
|
469 |
print(f"Chat interface error: {e}")
|
470 |
return f"I apologize, but I encountered an error. Please try rephrasing your question. Error: {str(e)}"
|
471 |
|
472 |
+
# Create Gradio interface with enhanced styling
|
473 |
demo = gr.ChatInterface(
|
474 |
fn=chat_interface,
|
475 |
title="🏥 Medical AI Assistant with medRAG",
|
|
|
492 |
],
|
493 |
theme="soft",
|
494 |
css="""
|
495 |
+
/* Main container styling */
|
496 |
+
.gradio-container {
|
497 |
+
background: linear-gradient(to right, #f0f8ff, #f5f5f5);
|
498 |
+
font-family: 'Arial', sans-serif;
|
499 |
+
}
|
500 |
+
|
501 |
+
/* Chat message styling */
|
502 |
.message.user {
|
503 |
background-color: #e3f2fd;
|
504 |
+
border-radius: 12px;
|
505 |
+
padding: 12px;
|
506 |
+
margin: 8px;
|
507 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
508 |
+
border-left: 4px solid #2196F3;
|
509 |
}
|
510 |
+
|
511 |
.message.bot {
|
512 |
background-color: #f1f8e9;
|
513 |
+
border-radius: 12px;
|
514 |
+
padding: 12px;
|
515 |
+
margin: 8px;
|
516 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
517 |
+
border-left: 4px solid #4CAF50;
|
518 |
+
}
|
519 |
+
|
520 |
+
/* Enhanced medical styling */
|
521 |
+
.bot h2 {
|
522 |
+
color: #1976D2 !important;
|
523 |
+
border-bottom: 2px solid #E0E0E0 !important;
|
524 |
+
padding-bottom: 8px !important;
|
525 |
}
|
526 |
"""
|
527 |
)
|
528 |
|
529 |
if __name__ == "__main__":
|
530 |
+
demo.launch(share=True)
|
requirements.txt
CHANGED
@@ -1,38 +1,45 @@
|
|
1 |
-
# Core
|
|
|
2 |
torch>=2.0.0
|
3 |
transformers>=4.30.0
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
# Gradio for UI
|
8 |
-
gradio>=4.0.0
|
9 |
-
spaces>=0.19.4
|
10 |
-
|
11 |
-
# LangGraph for workflow management
|
12 |
-
langgraph>=0.0.40
|
13 |
-
langchain>=0.1.0
|
14 |
-
langchain-core>=0.1.0
|
15 |
|
16 |
-
#
|
17 |
sentence-transformers>=2.2.2
|
18 |
faiss-cpu>=1.7.4
|
19 |
-
# For GPU support, use: faiss-gpu>=1.7.4
|
20 |
|
21 |
-
# Data
|
22 |
numpy>=1.24.0
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Core dependencies
|
2 |
+
gradio>=4.0.0
|
3 |
torch>=2.0.0
|
4 |
transformers>=4.30.0
|
5 |
+
langgraph>=0.3.27
|
6 |
+
langchain-core>=0.2.38
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Vector search and embeddings
|
9 |
sentence-transformers>=2.2.2
|
10 |
faiss-cpu>=1.7.4
|
|
|
11 |
|
12 |
+
# Data processing
|
13 |
numpy>=1.24.0
|
14 |
+
typing-extensions>=4.5.0
|
15 |
+
|
16 |
+
# LangGraph ecosystem components
|
17 |
+
langsmith>=0.1.63
|
18 |
+
langgraph-sdk>=0.1.66
|
19 |
+
langgraph-checkpoint>=2.0.23
|
20 |
+
|
21 |
+
# Web serving utilities
|
22 |
+
httpx>=0.25.0
|
23 |
+
uvicorn>=0.26.0
|
24 |
+
sse-starlette>=2.1.0,<2.2.0
|
25 |
+
uvloop>=0.18.0
|
26 |
+
httptools>=0.5.0
|
27 |
+
|
28 |
+
# Serialization and utilities
|
29 |
+
orjson>=3.9.7,<3.10.17
|
30 |
+
jsonschema-rs>=0.20.0
|
31 |
+
structlog>=24.1.0
|
32 |
+
cloudpickle>=3.0.0
|
33 |
+
tenacity>=8.0.0
|
34 |
+
|
35 |
+
# Model acceleration (optional but recommended)
|
36 |
+
accelerate>=0.20.0
|
37 |
+
safetensors>=0.3.1
|
38 |
+
bitsandbytes>=0.40.0
|
39 |
+
|
40 |
+
# For Hugging Face model access
|
41 |
+
huggingface_hub>=0.16.0
|
42 |
+
|
43 |
+
# Optional - specific model support
|
44 |
+
langchain_anthropic>=0.0.5
|
45 |
+
langchain_openai>=0.0.2
|