Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,33 +38,38 @@ def initialize_models():
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
# Initialize embedding model (CPU to save GPU memory)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
# Initialize chatbot model
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
print("β
Gemma 3n model loaded successfully")
|
| 64 |
return True, "All models loaded successfully"
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
print(f"Error loading models: {e}")
|
|
|
|
|
|
|
| 68 |
return False, f"Error: {str(e)}"
|
| 69 |
|
| 70 |
def extract_text_from_pdf(pdf_file):
|
|
@@ -169,14 +174,20 @@ def process_pdf(pdf_file, progress=gr.Progress()):
|
|
| 169 |
|
| 170 |
def chat_with_pdf(message, history):
|
| 171 |
"""Generate response using RAG"""
|
|
|
|
|
|
|
| 172 |
if not message.strip():
|
| 173 |
return history
|
| 174 |
|
| 175 |
if not processed_text:
|
| 176 |
return history + [[message, "β Please upload and process a PDF first"]]
|
| 177 |
|
|
|
|
| 178 |
if chatbot_model is None or chatbot_processor is None:
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
try:
|
| 182 |
# Retrieve relevant chunks
|
|
@@ -242,6 +253,24 @@ def clear_chat():
|
|
| 242 |
|
| 243 |
return [], "Ready to process a new PDF"
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# Initialize models on startup
|
| 246 |
model_status = "β³ Initializing models..."
|
| 247 |
if RAG_AVAILABLE:
|
|
@@ -265,7 +294,18 @@ with gr.Blocks(
|
|
| 265 |
gr.Markdown("### Upload a PDF and ask questions about it using Retrieval-Augmented Generation")
|
| 266 |
|
| 267 |
with gr.Row():
|
| 268 |
-
gr.Markdown(f"**Status:** {model_status}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
with gr.Row():
|
| 271 |
# Left column - PDF upload
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
# Initialize embedding model (CPU to save GPU memory)
|
| 41 |
+
if embedding_model is None:
|
| 42 |
+
print("Loading embedding model...")
|
| 43 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2', device='cpu')
|
| 44 |
+
print("β
Embedding model loaded successfully")
|
| 45 |
|
| 46 |
# Initialize chatbot model
|
| 47 |
+
if chatbot_model is None or chatbot_processor is None:
|
| 48 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 49 |
+
if not hf_token:
|
| 50 |
+
return False, "HF_TOKEN not found in environment"
|
| 51 |
+
|
| 52 |
+
print("Loading Gemma 3n model...")
|
| 53 |
+
chatbot_model = Gemma3nForConditionalGeneration.from_pretrained(
|
| 54 |
+
"google/gemma-3n-e4b-it",
|
| 55 |
+
device_map="auto",
|
| 56 |
+
torch_dtype=torch.bfloat16,
|
| 57 |
+
token=hf_token
|
| 58 |
+
).eval()
|
| 59 |
+
|
| 60 |
+
chatbot_processor = AutoProcessor.from_pretrained(
|
| 61 |
+
"google/gemma-3n-e4b-it",
|
| 62 |
+
token=hf_token
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
print("β
Gemma 3n model loaded successfully")
|
| 66 |
|
|
|
|
| 67 |
return True, "All models loaded successfully"
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
print(f"Error loading models: {e}")
|
| 71 |
+
import traceback
|
| 72 |
+
traceback.print_exc()
|
| 73 |
return False, f"Error: {str(e)}"
|
| 74 |
|
| 75 |
def extract_text_from_pdf(pdf_file):
|
|
|
|
| 174 |
|
| 175 |
def chat_with_pdf(message, history):
|
| 176 |
"""Generate response using RAG"""
|
| 177 |
+
global chatbot_model, chatbot_processor
|
| 178 |
+
|
| 179 |
if not message.strip():
|
| 180 |
return history
|
| 181 |
|
| 182 |
if not processed_text:
|
| 183 |
return history + [[message, "β Please upload and process a PDF first"]]
|
| 184 |
|
| 185 |
+
# Check if models are loaded
|
| 186 |
if chatbot_model is None or chatbot_processor is None:
|
| 187 |
+
print("Models not loaded, attempting to reload...")
|
| 188 |
+
success, error_msg = initialize_models()
|
| 189 |
+
if not success:
|
| 190 |
+
return history + [[message, f"β Failed to load models: {error_msg}"]]
|
| 191 |
|
| 192 |
try:
|
| 193 |
# Retrieve relevant chunks
|
|
|
|
| 253 |
|
| 254 |
return [], "Ready to process a new PDF"
|
| 255 |
|
| 256 |
+
def get_model_status():
|
| 257 |
+
"""Get current model loading status"""
|
| 258 |
+
global chatbot_model, chatbot_processor, embedding_model
|
| 259 |
+
|
| 260 |
+
statuses = []
|
| 261 |
+
|
| 262 |
+
if embedding_model is not None:
|
| 263 |
+
statuses.append("β
Embedding model loaded")
|
| 264 |
+
else:
|
| 265 |
+
statuses.append("β Embedding model not loaded")
|
| 266 |
+
|
| 267 |
+
if chatbot_model is not None and chatbot_processor is not None:
|
| 268 |
+
statuses.append("β
Chatbot model loaded")
|
| 269 |
+
else:
|
| 270 |
+
statuses.append("β Chatbot model not loaded")
|
| 271 |
+
|
| 272 |
+
return " | ".join(statuses)
|
| 273 |
+
|
| 274 |
# Initialize models on startup
|
| 275 |
model_status = "β³ Initializing models..."
|
| 276 |
if RAG_AVAILABLE:
|
|
|
|
| 294 |
gr.Markdown("### Upload a PDF and ask questions about it using Retrieval-Augmented Generation")
|
| 295 |
|
| 296 |
with gr.Row():
|
| 297 |
+
status_display = gr.Markdown(f"**Status:** {model_status}")
|
| 298 |
+
|
| 299 |
+
# Add refresh button for status
|
| 300 |
+
refresh_btn = gr.Button("βΎοΈ Refresh Status", size="sm")
|
| 301 |
+
|
| 302 |
+
def update_status():
|
| 303 |
+
return get_model_status()
|
| 304 |
+
|
| 305 |
+
refresh_btn.click(
|
| 306 |
+
fn=update_status,
|
| 307 |
+
outputs=[status_display]
|
| 308 |
+
)
|
| 309 |
|
| 310 |
with gr.Row():
|
| 311 |
# Left column - PDF upload
|