Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import markdown
|
|
| 9 |
import cv2
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
-
from transformers import AutoProcessor, VisionEncoderDecoderModel
|
| 13 |
import torch
|
| 14 |
import os
|
| 15 |
import tempfile
|
|
@@ -312,10 +312,37 @@ except Exception as e:
|
|
| 312 |
dolphin_model = None
|
| 313 |
model_status = f"β Model failed to load: {str(e)}"
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
# Global state for managing tabs
|
| 317 |
processed_markdown = ""
|
| 318 |
show_results_tab = False
|
|
|
|
| 319 |
|
| 320 |
|
| 321 |
def process_uploaded_pdf(pdf_file, progress=gr.Progress()):
|
|
@@ -484,7 +511,7 @@ with gr.Blocks(
|
|
| 484 |
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 485 |
|
| 486 |
gr.Markdown(
|
| 487 |
-
"*
|
| 488 |
elem_id="chat-notice"
|
| 489 |
)
|
| 490 |
|
|
@@ -513,12 +540,74 @@ with gr.Blocks(
|
|
| 513 |
outputs=[chat_tab]
|
| 514 |
)
|
| 515 |
|
| 516 |
-
#
|
| 517 |
-
def
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
|
| 520 |
send_btn.click(
|
| 521 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
inputs=[msg_input, chatbot],
|
| 523 |
outputs=[chatbot]
|
| 524 |
).then(
|
|
|
|
| 9 |
import cv2
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
+
from transformers import AutoProcessor, VisionEncoderDecoderModel, Gemma3nForConditionalGeneration, pipeline
|
| 13 |
import torch
|
| 14 |
import os
|
| 15 |
import tempfile
|
|
|
|
| 312 |
dolphin_model = None
|
| 313 |
model_status = f"β Model failed to load: {str(e)}"
|
| 314 |
|
| 315 |
+
# Initialize chatbot model
|
| 316 |
+
try:
|
| 317 |
+
import os
|
| 318 |
+
# Get HuggingFace token from environment/secrets
|
| 319 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 320 |
+
if hf_token:
|
| 321 |
+
os.environ['HF_TOKEN'] = hf_token
|
| 322 |
+
|
| 323 |
+
chatbot_model = Gemma3nForConditionalGeneration.from_pretrained(
|
| 324 |
+
"google/gemma-3n-e4b-it",
|
| 325 |
+
device_map="auto",
|
| 326 |
+
torch_dtype=torch.bfloat16,
|
| 327 |
+
use_auth_token=hf_token
|
| 328 |
+
).eval()
|
| 329 |
+
|
| 330 |
+
chatbot_processor = AutoProcessor.from_pretrained(
|
| 331 |
+
"google/gemma-3n-e4b-it",
|
| 332 |
+
use_auth_token=hf_token
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
print("Chatbot model loaded successfully")
|
| 336 |
+
except Exception as e:
|
| 337 |
+
print(f"Error loading chatbot model: {e}")
|
| 338 |
+
chatbot_model = None
|
| 339 |
+
chatbot_processor = None
|
| 340 |
+
|
| 341 |
|
| 342 |
# Global state for managing tabs
|
| 343 |
processed_markdown = ""
|
| 344 |
show_results_tab = False
|
| 345 |
+
chatbot_model = None
|
| 346 |
|
| 347 |
|
| 348 |
def process_uploaded_pdf(pdf_file, progress=gr.Progress()):
|
|
|
|
| 511 |
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 512 |
|
| 513 |
gr.Markdown(
|
| 514 |
+
"*Ask questions about your processed document. The AI will use the document content to provide accurate answers.*",
|
| 515 |
elem_id="chat-notice"
|
| 516 |
)
|
| 517 |
|
|
|
|
| 540 |
outputs=[chat_tab]
|
| 541 |
)
|
| 542 |
|
| 543 |
+
# Chatbot functionality
|
| 544 |
+
def chatbot_response(message, history):
|
| 545 |
+
if not message.strip():
|
| 546 |
+
return history
|
| 547 |
+
|
| 548 |
+
if chatbot_model is None:
|
| 549 |
+
return history + [[message, "β Chatbot model not loaded. Please check your HuggingFace token."]]
|
| 550 |
+
|
| 551 |
+
if not processed_markdown:
|
| 552 |
+
return history + [[message, "β Please process a PDF document first before asking questions."]]
|
| 553 |
+
|
| 554 |
+
try:
|
| 555 |
+
# Create context with the processed document
|
| 556 |
+
context = f"Document content:\n{processed_markdown[:3000]}..." if len(processed_markdown) > 3000 else f"Document content:\n{processed_markdown}"
|
| 557 |
+
|
| 558 |
+
# Create chat messages
|
| 559 |
+
messages = [
|
| 560 |
+
{
|
| 561 |
+
"role": "system",
|
| 562 |
+
"content": [{"type": "text", "text": "You are a helpful assistant that answers questions about documents. Use the provided document content to answer questions accurately."}]
|
| 563 |
+
},
|
| 564 |
+
{
|
| 565 |
+
"role": "user",
|
| 566 |
+
"content": [{"type": "text", "text": f"{context}\n\nQuestion: {message}"}]
|
| 567 |
+
}
|
| 568 |
+
]
|
| 569 |
+
|
| 570 |
+
# Process with the model
|
| 571 |
+
inputs = chatbot_processor.apply_chat_template(
|
| 572 |
+
messages,
|
| 573 |
+
add_generation_prompt=True,
|
| 574 |
+
tokenize=True,
|
| 575 |
+
return_dict=True,
|
| 576 |
+
return_tensors="pt",
|
| 577 |
+
).to(chatbot_model.device)
|
| 578 |
+
|
| 579 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 580 |
+
|
| 581 |
+
with torch.inference_mode():
|
| 582 |
+
generation = chatbot_model.generate(
|
| 583 |
+
**inputs,
|
| 584 |
+
max_new_tokens=300,
|
| 585 |
+
do_sample=False,
|
| 586 |
+
temperature=0.7,
|
| 587 |
+
pad_token_id=chatbot_processor.tokenizer.pad_token_id
|
| 588 |
+
)
|
| 589 |
+
generation = generation[0][input_len:]
|
| 590 |
+
|
| 591 |
+
response = chatbot_processor.decode(generation, skip_special_tokens=True)
|
| 592 |
+
|
| 593 |
+
return history + [[message, response]]
|
| 594 |
+
|
| 595 |
+
except Exception as e:
|
| 596 |
+
error_msg = f"β Error generating response: {str(e)}"
|
| 597 |
+
return history + [[message, error_msg]]
|
| 598 |
|
| 599 |
send_btn.click(
|
| 600 |
+
fn=chatbot_response,
|
| 601 |
+
inputs=[msg_input, chatbot],
|
| 602 |
+
outputs=[chatbot]
|
| 603 |
+
).then(
|
| 604 |
+
lambda: "",
|
| 605 |
+
outputs=[msg_input]
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
# Also allow Enter key to send message
|
| 609 |
+
msg_input.submit(
|
| 610 |
+
fn=chatbot_response,
|
| 611 |
inputs=[msg_input, chatbot],
|
| 612 |
outputs=[chatbot]
|
| 613 |
).then(
|