Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,13 @@ import requests
|
|
| 15 |
import json
|
| 16 |
import re
|
| 17 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Load environment variables
|
| 20 |
load_dotenv()
|
|
@@ -29,6 +36,25 @@ if not os.path.exists(FAISS_INDEX_DIR):
|
|
| 29 |
# Dictionary to store user-specific vectorstores
|
| 30 |
user_vectorstores = {}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Custom CSS for Tech theme
|
| 33 |
custom_css = """
|
| 34 |
:root {
|
|
@@ -70,6 +96,102 @@ body { background-color: var(--light-background); font-family: 'Google Sans', 'R
|
|
| 70 |
.qa-body { color: var(--dark-text); font-size: 0.95rem; margin-bottom: 10px; }
|
| 71 |
.qa-meta { display: flex; justify-content: space-between; color: #5F6368; font-size: 0.85rem; }
|
| 72 |
.tag { background-color: #E8F0FE; color: var(--primary-color); padding: 4px 8px; border-radius: 4px; font-size: 0.8rem; margin-right: 5px; display: inline-block; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
|
| 75 |
# Function to process PDF files
|
|
@@ -109,8 +231,207 @@ def process_pdf(pdf_file):
|
|
| 109 |
os.unlink(pdf_path)
|
| 110 |
return None, f"Error processing PDF: {str(e)}", {"page_images": [], "total_pages": 0, "total_words": 0}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Function to generate chatbot responses with Tech theme
|
| 113 |
-
def generate_response(message, session_id, model_name, history):
|
| 114 |
if not message:
|
| 115 |
return history
|
| 116 |
try:
|
|
@@ -121,8 +442,8 @@ def generate_response(message, session_id, model_name, history):
|
|
| 121 |
if docs:
|
| 122 |
context = "\n\nRelevant information from uploaded PDF:\n" + "\n".join(f"- {doc.page_content}" for doc in docs)
|
| 123 |
|
| 124 |
-
# Check if it's a GitHub repo search
|
| 125 |
-
if re.match(r'^/github\s+.+', message, re.IGNORECASE):
|
| 126 |
query = re.sub(r'^/github\s+', '', message, flags=re.IGNORECASE)
|
| 127 |
repo_results = search_github_repos(query)
|
| 128 |
if repo_results:
|
|
@@ -139,8 +460,8 @@ def generate_response(message, session_id, model_name, history):
|
|
| 139 |
history.append((message, "No GitHub repositories found for your query."))
|
| 140 |
return history
|
| 141 |
|
| 142 |
-
# Check if it's a Stack Overflow search
|
| 143 |
-
if re.match(r'^/stack\s+.+', message, re.IGNORECASE):
|
| 144 |
query = re.sub(r'^/stack\s+', '', message, flags=re.IGNORECASE)
|
| 145 |
qa_results = search_stackoverflow(query)
|
| 146 |
if qa_results:
|
|
@@ -437,106 +758,144 @@ def perform_stack_search(query, tag, sort_by):
|
|
| 437 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 438 |
current_session_id = gr.State(None)
|
| 439 |
pdf_state = gr.State({"page_images": [], "total_pages": 0, "total_words": 0})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
gr.HTML("""
|
| 441 |
<div class="header">
|
| 442 |
-
<div class="header-title">Tech-Vision</div>
|
| 443 |
-
<div class="header-subtitle">Analyze technical documents
|
| 444 |
</div>
|
| 445 |
""")
|
| 446 |
with gr.Row(elem_classes="container"):
|
| 447 |
with gr.Column(scale=1, min_width=300):
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
model_dropdown = gr.Dropdown(
|
| 452 |
choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
|
| 453 |
value="llama3-70b-8192",
|
| 454 |
label="Select Groq Model"
|
| 455 |
)
|
| 456 |
-
|
| 457 |
-
# Tech Tools Section
|
| 458 |
-
gr.Markdown("### Developer Tools", elem_classes="tool-title")
|
| 459 |
-
with gr.Group(elem_classes="tool-container"):
|
| 460 |
-
with gr.Tabs():
|
| 461 |
-
with gr.TabItem("GitHub Search"):
|
| 462 |
-
repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
|
| 463 |
-
with gr.Row():
|
| 464 |
-
language = gr.Dropdown(
|
| 465 |
-
choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
|
| 466 |
-
value="any",
|
| 467 |
-
label="Language"
|
| 468 |
-
)
|
| 469 |
-
min_stars = gr.Dropdown(
|
| 470 |
-
choices=["0", "10", "50", "100", "1000", "10000"],
|
| 471 |
-
value="0",
|
| 472 |
-
label="Min Stars"
|
| 473 |
-
)
|
| 474 |
-
sort_by = gr.Dropdown(
|
| 475 |
-
choices=["stars", "forks", "updated"],
|
| 476 |
-
value="stars",
|
| 477 |
-
label="Sort By"
|
| 478 |
-
)
|
| 479 |
-
repo_search_btn = gr.Button("Search Repositories")
|
| 480 |
-
|
| 481 |
-
with gr.TabItem("Stack Overflow"):
|
| 482 |
-
stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
|
| 483 |
-
with gr.Row():
|
| 484 |
-
tag = gr.Dropdown(
|
| 485 |
-
choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
|
| 486 |
-
value="any",
|
| 487 |
-
label="Tag"
|
| 488 |
-
)
|
| 489 |
-
so_sort_by = gr.Dropdown(
|
| 490 |
-
choices=["votes", "newest", "activity"],
|
| 491 |
-
value="votes",
|
| 492 |
-
label="Sort By"
|
| 493 |
-
)
|
| 494 |
-
so_search_btn = gr.Button("Search Stack Overflow")
|
| 495 |
-
|
| 496 |
-
with gr.TabItem("Code Explainer"):
|
| 497 |
-
code_input = gr.Textbox(
|
| 498 |
-
label="Code to Explain",
|
| 499 |
-
placeholder="Paste your code here...",
|
| 500 |
-
lines=10
|
| 501 |
-
)
|
| 502 |
-
explain_btn = gr.Button("Explain Code")
|
| 503 |
-
|
| 504 |
with gr.Column(scale=2, min_width=600):
|
| 505 |
with gr.Tabs():
|
| 506 |
with gr.TabItem("PDF Viewer"):
|
| 507 |
with gr.Column(elem_classes="pdf-viewer-container"):
|
| 508 |
page_slider = gr.Slider(minimum=1, maximum=1, step=1, label="Page Number", value=1)
|
| 509 |
pdf_image = gr.Image(label="PDF Page", type="pil", elem_classes="pdf-viewer-image")
|
| 510 |
-
|
| 511 |
|
| 512 |
-
with gr.TabItem("
|
| 513 |
-
|
|
|
|
| 514 |
|
| 515 |
-
with gr.TabItem("
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
|
|
|
| 521 |
with gr.Row(elem_classes="container"):
|
| 522 |
with gr.Column(scale=2, min_width=600):
|
| 523 |
-
chatbot = gr.Chatbot(height=
|
| 524 |
with gr.Row():
|
| 525 |
-
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
send_btn = gr.Button("Send", scale=1)
|
| 527 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
-
# Event Handlers
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
process_pdf,
|
| 532 |
inputs=[pdf_file],
|
| 533 |
-
outputs=[current_session_id,
|
| 534 |
).then(
|
| 535 |
update_pdf_viewer,
|
| 536 |
inputs=[pdf_state],
|
| 537 |
-
outputs=[page_slider, pdf_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
)
|
| 539 |
|
|
|
|
| 540 |
msg.submit(
|
| 541 |
generate_response,
|
| 542 |
inputs=[msg, current_session_id, model_dropdown, chatbot],
|
|
@@ -549,43 +908,55 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 549 |
outputs=[chatbot]
|
| 550 |
).then(lambda: "", None, [msg])
|
| 551 |
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
)
|
| 557 |
|
|
|
|
| 558 |
page_slider.change(
|
| 559 |
update_image,
|
| 560 |
inputs=[page_slider, pdf_state],
|
| 561 |
outputs=[pdf_image]
|
| 562 |
)
|
| 563 |
|
| 564 |
-
#
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
outputs=[code_explanation]
|
| 581 |
)
|
| 582 |
|
| 583 |
-
# Add footer with attribution
|
| 584 |
-
gr.HTML("""
|
| 585 |
-
<div style="text-align: center; margin-top: 20px; padding: 10px; color: #666; font-size: 0.8rem; border-top: 1px solid #eee;">
|
| 586 |
-
|
| 587 |
-
</div>
|
| 588 |
-
""")
|
| 589 |
|
| 590 |
# Launch the app
|
| 591 |
if __name__ == "__main__":
|
|
|
|
| 15 |
import json
|
| 16 |
import re
|
| 17 |
from datetime import datetime, timedelta
|
| 18 |
+
import speech_recognition as sr
|
| 19 |
+
import pyttsx3
|
| 20 |
+
import torch
|
| 21 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 22 |
+
import numpy as np
|
| 23 |
+
import pandas as pd
|
| 24 |
+
import openpyxl
|
| 25 |
|
| 26 |
# Load environment variables
|
| 27 |
load_dotenv()
|
|
|
|
| 36 |
# Dictionary to store user-specific vectorstores
|
| 37 |
user_vectorstores = {}
|
| 38 |
|
| 39 |
+
# Load SmolDocling model for image analysis
|
| 40 |
+
def load_docling_model():
|
| 41 |
+
try:
|
| 42 |
+
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
| 43 |
+
model = AutoModelForVision2Seq.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
| 44 |
+
return processor, model
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error loading SmolDocling model: {e}")
|
| 47 |
+
return None, None
|
| 48 |
+
|
| 49 |
+
# Initialize SmolDocling model
|
| 50 |
+
docling_processor, docling_model = load_docling_model()
|
| 51 |
+
|
| 52 |
+
# Initialize text-to-speech engine
|
| 53 |
+
tts_engine = pyttsx3.init()
|
| 54 |
+
# Set properties for better speech
|
| 55 |
+
tts_engine.setProperty('rate', 150) # Speed of speech
|
| 56 |
+
tts_engine.setProperty('volume', 0.9) # Volume (0.0 to 1.0)
|
| 57 |
+
|
| 58 |
# Custom CSS for Tech theme
|
| 59 |
custom_css = """
|
| 60 |
:root {
|
|
|
|
| 96 |
.qa-body { color: var(--dark-text); font-size: 0.95rem; margin-bottom: 10px; }
|
| 97 |
.qa-meta { display: flex; justify-content: space-between; color: #5F6368; font-size: 0.85rem; }
|
| 98 |
.tag { background-color: #E8F0FE; color: var(--primary-color); padding: 4px 8px; border-radius: 4px; font-size: 0.8rem; margin-right: 5px; display: inline-block; }
|
| 99 |
+
.toggle-container { display: flex; align-items: center; margin-bottom: 15px; }
|
| 100 |
+
.toggle-label { margin-right: 10px; font-weight: 500; }
|
| 101 |
+
.search-toggle { margin-left: 5px; }
|
| 102 |
+
.voice-btn { background-color: var(--primary-color) !important; border-radius: 50% !important; width: 44px !important; height: 44px !important; display: flex !important; align-items: center !important; justify-content: center !important; color: var(--white) !important; box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important; }
|
| 103 |
+
.speak-btn { background-color: var(--secondary-color) !important; border-radius: 24px !important; color: var(--white) !important; padding: 8px 16px !important; font-weight: 500 !important; margin-left: 10px !important; }
|
| 104 |
+
.audio-controls { display: flex; align-items: center; margin-top: 10px; }
|
| 105 |
+
|
| 106 |
+
/* Audio Visualization Elements */
|
| 107 |
+
.audio-visualization {
|
| 108 |
+
display: flex;
|
| 109 |
+
align-items: center;
|
| 110 |
+
justify-content: center;
|
| 111 |
+
gap: 4px;
|
| 112 |
+
height: 40px;
|
| 113 |
+
padding: 10px;
|
| 114 |
+
background-color: rgba(0,0,0,0.05);
|
| 115 |
+
border-radius: 12px;
|
| 116 |
+
margin: 10px 0;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.audio-bar {
|
| 120 |
+
width: 3px;
|
| 121 |
+
background-color: var(--accent-color);
|
| 122 |
+
border-radius: 2px;
|
| 123 |
+
height: 5px;
|
| 124 |
+
transition: height 0.1s ease;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
.audio-status {
|
| 128 |
+
font-size: 0.85rem;
|
| 129 |
+
color: var(--secondary-color);
|
| 130 |
+
text-align: center;
|
| 131 |
+
margin-top: 5px;
|
| 132 |
+
font-style: italic;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.recording-indicator {
|
| 136 |
+
width: 12px;
|
| 137 |
+
height: 12px;
|
| 138 |
+
border-radius: 50%;
|
| 139 |
+
background-color: #ff4b4b;
|
| 140 |
+
margin-right: 8px;
|
| 141 |
+
animation: blink 1s infinite;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.playing-indicator {
|
| 145 |
+
width: 12px;
|
| 146 |
+
height: 12px;
|
| 147 |
+
border-radius: 50%;
|
| 148 |
+
background-color: #4bff4b;
|
| 149 |
+
margin-right: 8px;
|
| 150 |
+
animation: pulse 1s infinite;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
@keyframes blink {
|
| 154 |
+
0% { opacity: 1; }
|
| 155 |
+
50% { opacity: 0.4; }
|
| 156 |
+
100% { opacity: 1; }
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
@keyframes pulse {
|
| 160 |
+
0% { transform: scale(1); }
|
| 161 |
+
50% { transform: scale(1.2); }
|
| 162 |
+
100% { transform: scale(1); }
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
.file-upload-enhancement .file-preview {
|
| 166 |
+
max-height: 200px;
|
| 167 |
+
overflow: auto;
|
| 168 |
+
border: 1px solid var(--border-color);
|
| 169 |
+
border-radius: 8px;
|
| 170 |
+
padding: 10px;
|
| 171 |
+
margin-top: 10px;
|
| 172 |
+
background-color: rgba(0,0,0,0.02);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.excel-preview-table {
|
| 176 |
+
width: 100%;
|
| 177 |
+
border-collapse: collapse;
|
| 178 |
+
font-size: 0.85rem;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.excel-preview-table th, .excel-preview-table td {
|
| 182 |
+
border: 1px solid #ddd;
|
| 183 |
+
padding: 4px 8px;
|
| 184 |
+
text-align: left;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.excel-preview-table th {
|
| 188 |
+
background-color: var(--secondary-color);
|
| 189 |
+
color: white;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.excel-preview-table tr:nth-child(even) {
|
| 193 |
+
background-color: rgba(0,0,0,0.03);
|
| 194 |
+
}
|
| 195 |
"""
|
| 196 |
|
| 197 |
# Function to process PDF files
|
|
|
|
| 231 |
os.unlink(pdf_path)
|
| 232 |
return None, f"Error processing PDF: {str(e)}", {"page_images": [], "total_pages": 0, "total_words": 0}
|
| 233 |
|
| 234 |
+
# New function to process Excel files
|
| 235 |
+
def process_excel(excel_file):
|
| 236 |
+
if excel_file is None:
|
| 237 |
+
return None, "No file uploaded", {"data_preview": "", "total_sheets": 0, "total_rows": 0}
|
| 238 |
+
|
| 239 |
+
try:
|
| 240 |
+
session_id = str(uuid.uuid4())
|
| 241 |
+
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as temp_file:
|
| 242 |
+
temp_file.write(excel_file)
|
| 243 |
+
excel_path = temp_file.name
|
| 244 |
+
|
| 245 |
+
# Read Excel file with pandas
|
| 246 |
+
excel_data = pd.ExcelFile(excel_path)
|
| 247 |
+
sheet_names = excel_data.sheet_names
|
| 248 |
+
all_texts = []
|
| 249 |
+
total_rows = 0
|
| 250 |
+
|
| 251 |
+
# Process each sheet
|
| 252 |
+
for sheet in sheet_names:
|
| 253 |
+
df = pd.read_excel(excel_path, sheet_name=sheet)
|
| 254 |
+
total_rows += len(df)
|
| 255 |
+
|
| 256 |
+
# Convert dataframe to text for vectorization
|
| 257 |
+
sheet_text = f"Sheet: {sheet}\n"
|
| 258 |
+
sheet_text += df.to_string(index=False)
|
| 259 |
+
all_texts.append(sheet_text)
|
| 260 |
+
|
| 261 |
+
# Generate HTML preview of first sheet
|
| 262 |
+
first_df = pd.read_excel(excel_path, sheet_name=0)
|
| 263 |
+
preview_rows = min(10, len(first_df))
|
| 264 |
+
data_preview = first_df.head(preview_rows).to_html(classes="excel-preview-table", index=False)
|
| 265 |
+
|
| 266 |
+
# Process for vectorstore
|
| 267 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 268 |
+
chunks = text_splitter.create_documents(all_texts)
|
| 269 |
+
vectorstore = FAISS.from_documents(chunks, embeddings)
|
| 270 |
+
index_path = os.path.join(FAISS_INDEX_DIR, session_id)
|
| 271 |
+
vectorstore.save_local(index_path)
|
| 272 |
+
user_vectorstores[session_id] = vectorstore
|
| 273 |
+
|
| 274 |
+
os.unlink(excel_path)
|
| 275 |
+
excel_state = {"data_preview": data_preview, "total_sheets": len(sheet_names), "total_rows": total_rows}
|
| 276 |
+
return session_id, f"✅ Successfully processed {len(chunks)} text chunks from Excel file", excel_state
|
| 277 |
+
except Exception as e:
|
| 278 |
+
if "excel_path" in locals() and os.path.exists(excel_path):
|
| 279 |
+
os.unlink(excel_path)
|
| 280 |
+
return None, f"Error processing Excel file: {str(e)}", {"data_preview": "", "total_sheets": 0, "total_rows": 0}
|
| 281 |
+
|
| 282 |
+
# Function to analyze image using SmolDocling
|
| 283 |
+
def analyze_image(image_file):
|
| 284 |
+
if image_file is None:
|
| 285 |
+
return "No image uploaded. Please upload an image to analyze."
|
| 286 |
+
|
| 287 |
+
if docling_processor is None or docling_model is None:
|
| 288 |
+
return "SmolDocling model not loaded. Please check your installation."
|
| 289 |
+
|
| 290 |
+
try:
|
| 291 |
+
# Process the image - image_file is a filepath string from Gradio
|
| 292 |
+
image = Image.open(image_file)
|
| 293 |
+
|
| 294 |
+
# Use the SmolDocling model
|
| 295 |
+
inputs = docling_processor(images=image, return_tensors="pt")
|
| 296 |
+
with torch.no_grad():
|
| 297 |
+
outputs = docling_model.generate(
|
| 298 |
+
**inputs,
|
| 299 |
+
max_new_tokens=512,
|
| 300 |
+
temperature=0.1,
|
| 301 |
+
do_sample=False
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
# Decode the output
|
| 305 |
+
result = docling_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 306 |
+
|
| 307 |
+
# Format the result for display with technical emphasis
|
| 308 |
+
analysis = f"## Technical Document Analysis Results\n\n{result}\n\n"
|
| 309 |
+
analysis += "### Technical Insights\n\n"
|
| 310 |
+
analysis += "* The analysis provides technical information extracted from the document image.\n"
|
| 311 |
+
analysis += "* Consider this information as a starting point for further technical investigation.\n"
|
| 312 |
+
analysis += "* For code snippets or technical specifications, verify accuracy before implementation.\n"
|
| 313 |
+
|
| 314 |
+
return analysis
|
| 315 |
+
except Exception as e:
|
| 316 |
+
return f"Error analyzing image: {str(e)}"
|
| 317 |
+
|
| 318 |
+
# Improved function for speech-to-text conversion with status updates
|
| 319 |
+
def speech_to_text(audio_status):
|
| 320 |
+
try:
|
| 321 |
+
# Update status to show we're listening
|
| 322 |
+
audio_status = "Listening... Speak now"
|
| 323 |
+
yield audio_status, gr.update(visible=True), None
|
| 324 |
+
|
| 325 |
+
r = sr.Recognizer()
|
| 326 |
+
with sr.Microphone() as source:
|
| 327 |
+
r.adjust_for_ambient_noise(source)
|
| 328 |
+
audio = r.listen(source, timeout=5, phrase_time_limit=15)
|
| 329 |
+
|
| 330 |
+
# Update status to show processing
|
| 331 |
+
audio_status = "Processing speech..."
|
| 332 |
+
yield audio_status, gr.update(visible=True), None
|
| 333 |
+
|
| 334 |
+
text = r.recognize_google(audio)
|
| 335 |
+
audio_status = "Speech recognized!"
|
| 336 |
+
return audio_status, gr.update(visible=False), text
|
| 337 |
+
except sr.UnknownValueError:
|
| 338 |
+
audio_status = "Could not understand audio. Please try again."
|
| 339 |
+
return audio_status, gr.update(visible=False), None
|
| 340 |
+
except sr.RequestError as e:
|
| 341 |
+
audio_status = f"Error with speech recognition service: {e}"
|
| 342 |
+
return audio_status, gr.update(visible=False), None
|
| 343 |
+
except Exception as e:
|
| 344 |
+
audio_status = f"Error: {str(e)}"
|
| 345 |
+
return audio_status, gr.update(visible=False), None
|
| 346 |
+
|
| 347 |
+
# Improved function for text-to-speech conversion with pyttsx3
|
| 348 |
+
def text_to_speech(audio_status, history):
|
| 349 |
+
if not history:
|
| 350 |
+
return "No text to speak", gr.update(visible=False), None
|
| 351 |
+
|
| 352 |
+
try:
|
| 353 |
+
# Get the last bot response
|
| 354 |
+
last_response = history[-1][1]
|
| 355 |
+
|
| 356 |
+
# Clean up the text (remove markdown and other formatting)
|
| 357 |
+
clean_text = re.sub(r'\*\*|__', '', last_response) # Remove bold/underline
|
| 358 |
+
clean_text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', clean_text) # Remove links
|
| 359 |
+
clean_text = re.sub(r'#+ ', '', clean_text) # Remove headers
|
| 360 |
+
clean_text = re.sub(r'```[^`]*```', ' Code block removed for speech. ', clean_text) # Remove code blocks
|
| 361 |
+
|
| 362 |
+
# Update status
|
| 363 |
+
audio_status = "Generating speech..."
|
| 364 |
+
yield audio_status, gr.update(visible=True), None
|
| 365 |
+
|
| 366 |
+
# Save to a temporary file
|
| 367 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 368 |
+
|
| 369 |
+
# Use pyttsx3 to generate speech
|
| 370 |
+
tts_engine.save_to_file(clean_text, temp_file.name)
|
| 371 |
+
tts_engine.runAndWait()
|
| 372 |
+
|
| 373 |
+
audio_status = "Speech ready!"
|
| 374 |
+
return audio_status, gr.update(visible=False), temp_file.name
|
| 375 |
+
except Exception as e:
|
| 376 |
+
audio_status = f"Error in text-to-speech: {str(e)}"
|
| 377 |
+
return audio_status, gr.update(visible=False), None
|
| 378 |
+
|
| 379 |
+
# Function to handle different file types
|
| 380 |
+
def process_file(file_data, file_type):
|
| 381 |
+
if file_data is None:
|
| 382 |
+
return None, "No file uploaded", None
|
| 383 |
+
|
| 384 |
+
if file_type == "pdf":
|
| 385 |
+
return process_pdf(file_data)
|
| 386 |
+
elif file_type == "excel":
|
| 387 |
+
return process_excel(file_data)
|
| 388 |
+
elif file_type == "image":
|
| 389 |
+
# For image files, we'll just use them directly for analysis
|
| 390 |
+
# But we'll return a session ID to maintain consistency
|
| 391 |
+
session_id = str(uuid.uuid4())
|
| 392 |
+
return session_id, "✅ Image file ready for analysis", None
|
| 393 |
+
else:
|
| 394 |
+
return None, "Unsupported file type", None
|
| 395 |
+
|
| 396 |
+
# Function for speech-to-text conversion
|
| 397 |
+
def speech_to_text():
|
| 398 |
+
try:
|
| 399 |
+
r = sr.Recognizer()
|
| 400 |
+
with sr.Microphone() as source:
|
| 401 |
+
r.adjust_for_ambient_noise(source)
|
| 402 |
+
audio = r.listen(source)
|
| 403 |
+
text = r.recognize_google(audio)
|
| 404 |
+
return text
|
| 405 |
+
except sr.UnknownValueError:
|
| 406 |
+
return "Could not understand audio. Please try again."
|
| 407 |
+
except sr.RequestError as e:
|
| 408 |
+
return f"Error with speech recognition service: {e}"
|
| 409 |
+
except Exception as e:
|
| 410 |
+
return f"Error converting speech to text: {str(e)}"
|
| 411 |
+
|
| 412 |
+
# Function for text-to-speech conversion
|
| 413 |
+
def text_to_speech(text, history):
|
| 414 |
+
if not text or not history:
|
| 415 |
+
return None
|
| 416 |
+
|
| 417 |
+
try:
|
| 418 |
+
# Get the last bot response
|
| 419 |
+
last_response = history[-1][1]
|
| 420 |
+
|
| 421 |
+
# Convert text to speech
|
| 422 |
+
tts = pyttsx3.init()
|
| 423 |
+
tts.setProperty('rate', 150)
|
| 424 |
+
tts.setProperty('volume', 0.9)
|
| 425 |
+
tts.save_to_file(last_response, "temp_output.mp3")
|
| 426 |
+
tts.runAndWait()
|
| 427 |
+
|
| 428 |
+
return "temp_output.mp3"
|
| 429 |
+
except Exception as e:
|
| 430 |
+
print(f"Error in text-to-speech: {e}")
|
| 431 |
+
return None
|
| 432 |
+
|
| 433 |
# Function to generate chatbot responses with Tech theme
|
| 434 |
+
def generate_response(message, session_id, model_name, history, web_search_enabled=True):
|
| 435 |
if not message:
|
| 436 |
return history
|
| 437 |
try:
|
|
|
|
| 442 |
if docs:
|
| 443 |
context = "\n\nRelevant information from uploaded PDF:\n" + "\n".join(f"- {doc.page_content}" for doc in docs)
|
| 444 |
|
| 445 |
+
# Check if it's a GitHub repo search and web search is enabled
|
| 446 |
+
if web_search_enabled and re.match(r'^/github\s+.+', message, re.IGNORECASE):
|
| 447 |
query = re.sub(r'^/github\s+', '', message, flags=re.IGNORECASE)
|
| 448 |
repo_results = search_github_repos(query)
|
| 449 |
if repo_results:
|
|
|
|
| 460 |
history.append((message, "No GitHub repositories found for your query."))
|
| 461 |
return history
|
| 462 |
|
| 463 |
+
# Check if it's a Stack Overflow search and web search is enabled
|
| 464 |
+
if web_search_enabled and re.match(r'^/stack\s+.+', message, re.IGNORECASE):
|
| 465 |
query = re.sub(r'^/stack\s+', '', message, flags=re.IGNORECASE)
|
| 466 |
qa_results = search_stackoverflow(query)
|
| 467 |
if qa_results:
|
|
|
|
| 758 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 759 |
current_session_id = gr.State(None)
|
| 760 |
pdf_state = gr.State({"page_images": [], "total_pages": 0, "total_words": 0})
|
| 761 |
+
excel_state = gr.State({"data_preview": "", "total_sheets": 0, "total_rows": 0})
|
| 762 |
+
file_type = gr.State("none")
|
| 763 |
+
audio_status = gr.State("Ready")
|
| 764 |
+
|
| 765 |
gr.HTML("""
|
| 766 |
<div class="header">
|
| 767 |
+
<div class="header-title">Tech-Vision Enhanced</div>
|
| 768 |
+
<div class="header-subtitle">Analyze technical documents, spreadsheets, and images with AI</div>
|
| 769 |
</div>
|
| 770 |
""")
|
| 771 |
with gr.Row(elem_classes="container"):
|
| 772 |
with gr.Column(scale=1, min_width=300):
|
| 773 |
+
with gr.Tabs():
|
| 774 |
+
with gr.TabItem("PDF"):
|
| 775 |
+
pdf_file = gr.File(label="Upload PDF Document", file_types=[".pdf"], type="binary")
|
| 776 |
+
pdf_upload_button = gr.Button("Process PDF", variant="primary")
|
| 777 |
+
|
| 778 |
+
with gr.TabItem("Excel"):
|
| 779 |
+
excel_file = gr.File(label="Upload Excel File", file_types=[".xlsx", ".xls"], type="binary")
|
| 780 |
+
excel_upload_button = gr.Button("Process Excel", variant="primary")
|
| 781 |
+
|
| 782 |
+
with gr.TabItem("Image"):
|
| 783 |
+
image_input = gr.File(
|
| 784 |
+
label="Upload Image",
|
| 785 |
+
file_types=["image"],
|
| 786 |
+
type="filepath"
|
| 787 |
+
)
|
| 788 |
+
analyze_btn = gr.Button("Analyze Image")
|
| 789 |
+
|
| 790 |
+
file_status = gr.Markdown("No file uploaded yet")
|
| 791 |
+
|
| 792 |
+
# Model selector
|
| 793 |
model_dropdown = gr.Dropdown(
|
| 794 |
choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
|
| 795 |
value="llama3-70b-8192",
|
| 796 |
label="Select Groq Model"
|
| 797 |
)
|
| 798 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
with gr.Column(scale=2, min_width=600):
|
| 800 |
with gr.Tabs():
|
| 801 |
with gr.TabItem("PDF Viewer"):
|
| 802 |
with gr.Column(elem_classes="pdf-viewer-container"):
|
| 803 |
page_slider = gr.Slider(minimum=1, maximum=1, step=1, label="Page Number", value=1)
|
| 804 |
pdf_image = gr.Image(label="PDF Page", type="pil", elem_classes="pdf-viewer-image")
|
| 805 |
+
pdf_stats = gr.Markdown("No PDF uploaded yet", elem_classes="stats-box")
|
| 806 |
|
| 807 |
+
with gr.TabItem("Excel Viewer"):
|
| 808 |
+
excel_preview = gr.HTML(label="Excel Preview", elem_classes="file-preview")
|
| 809 |
+
excel_stats = gr.Markdown("No Excel file uploaded yet", elem_classes="stats-box")
|
| 810 |
|
| 811 |
+
with gr.TabItem("Image Analysis"):
|
| 812 |
+
image_preview = gr.Image(label="Image Preview", type="pil")
|
| 813 |
+
image_analysis_results = gr.Markdown("Upload an image and click 'Analyze Image' to see analysis results")
|
| 814 |
+
|
| 815 |
+
# Audio visualization elements
|
| 816 |
+
with gr.Row(elem_classes="container"):
|
| 817 |
+
with gr.Column():
|
| 818 |
+
audio_vis = gr.HTML("""
|
| 819 |
+
<div class="audio-visualization">
|
| 820 |
+
<div class="audio-bar" style="height: 5px;"></div>
|
| 821 |
+
<div class="audio-bar" style="height: 12px;"></div>
|
| 822 |
+
<div class="audio-bar" style="height: 18px;"></div>
|
| 823 |
+
<div class="audio-bar" style="height: 15px;"></div>
|
| 824 |
+
<div class="audio-bar" style="height: 10px;"></div>
|
| 825 |
+
<div class="audio-bar" style="height: 20px;"></div>
|
| 826 |
+
<div class="audio-bar" style="height: 14px;"></div>
|
| 827 |
+
<div class="audio-bar" style="height: 8px;"></div>
|
| 828 |
+
</div>
|
| 829 |
+
""", visible=False)
|
| 830 |
+
audio_status_display = gr.Markdown("", elem_classes="audio-status")
|
| 831 |
|
| 832 |
+
# Chat interface
|
| 833 |
with gr.Row(elem_classes="container"):
|
| 834 |
with gr.Column(scale=2, min_width=600):
|
| 835 |
+
chatbot = gr.Chatbot(height=400, bubble_full_width=False, show_copy_button=True, elem_classes="chat-container")
|
| 836 |
with gr.Row():
|
| 837 |
+
msg = gr.Textbox(
|
| 838 |
+
show_label=False,
|
| 839 |
+
placeholder="Ask about your document or click the microphone to speak...",
|
| 840 |
+
scale=5
|
| 841 |
+
)
|
| 842 |
+
voice_btn = gr.Button("🎤", elem_classes="voice-btn")
|
| 843 |
send_btn = gr.Button("Send", scale=1)
|
| 844 |
+
|
| 845 |
+
with gr.Row(elem_classes="audio-controls"):
|
| 846 |
+
clear_btn = gr.Button("Clear Conversation")
|
| 847 |
+
speak_btn = gr.Button("🔊 Speak Response", elem_classes="speak-btn")
|
| 848 |
+
audio_player = gr.Audio(label="Response Audio", type="filepath", visible=False)
|
| 849 |
|
| 850 |
+
# Event Handlers for PDF processing
|
| 851 |
+
pdf_upload_button.click(
|
| 852 |
+
lambda x: ("pdf", x),
|
| 853 |
+
inputs=[pdf_file],
|
| 854 |
+
outputs=[file_type, file_status]
|
| 855 |
+
).then(
|
| 856 |
process_pdf,
|
| 857 |
inputs=[pdf_file],
|
| 858 |
+
outputs=[current_session_id, file_status, pdf_state]
|
| 859 |
).then(
|
| 860 |
update_pdf_viewer,
|
| 861 |
inputs=[pdf_state],
|
| 862 |
+
outputs=[page_slider, pdf_image, pdf_stats]
|
| 863 |
+
)
|
| 864 |
+
|
| 865 |
+
# Event Handlers for Excel processing
|
| 866 |
+
excel_upload_button.click(
|
| 867 |
+
lambda x: ("excel", x),
|
| 868 |
+
inputs=[excel_file],
|
| 869 |
+
outputs=[file_type, file_status]
|
| 870 |
+
).then(
|
| 871 |
+
process_excel,
|
| 872 |
+
inputs=[excel_file],
|
| 873 |
+
outputs=[current_session_id, file_status, excel_state]
|
| 874 |
+
).then(
|
| 875 |
+
lambda preview, sheets, rows: (
|
| 876 |
+
preview,
|
| 877 |
+
f"**Excel Statistics:**\nSheets: {sheets}\nTotal Rows: {rows}"
|
| 878 |
+
),
|
| 879 |
+
inputs=[excel_state["data_preview"], excel_state["total_sheets"], excel_state["total_rows"]],
|
| 880 |
+
outputs=[excel_preview, excel_stats]
|
| 881 |
+
)
|
| 882 |
+
|
| 883 |
+
# Event Handlers for Image Analysis
|
| 884 |
+
analyze_btn.click(
|
| 885 |
+
lambda x: ("image", x),
|
| 886 |
+
inputs=[image_input],
|
| 887 |
+
outputs=[file_type, file_status]
|
| 888 |
+
).then(
|
| 889 |
+
analyze_image,
|
| 890 |
+
inputs=[image_input],
|
| 891 |
+
outputs=[image_analysis_results]
|
| 892 |
+
).then(
|
| 893 |
+
lambda x: Image.open(x) if x else None,
|
| 894 |
+
inputs=[image_input],
|
| 895 |
+
outputs=[image_preview]
|
| 896 |
)
|
| 897 |
|
| 898 |
+
# Chat message handling
|
| 899 |
msg.submit(
|
| 900 |
generate_response,
|
| 901 |
inputs=[msg, current_session_id, model_dropdown, chatbot],
|
|
|
|
| 908 |
outputs=[chatbot]
|
| 909 |
).then(lambda: "", None, [msg])
|
| 910 |
|
| 911 |
+
# Improved speech-to-text with visual feedback
|
| 912 |
+
voice_btn.click(
|
| 913 |
+
speech_to_text,
|
| 914 |
+
inputs=[audio_status],
|
| 915 |
+
outputs=[audio_status_display, audio_vis, msg]
|
| 916 |
+
)
|
| 917 |
+
|
| 918 |
+
# Improved text-to-speech with visual feedback
|
| 919 |
+
speak_btn.click(
|
| 920 |
+
text_to_speech,
|
| 921 |
+
inputs=[audio_status, chatbot],
|
| 922 |
+
outputs=[audio_status_display, audio_vis, audio_player]
|
| 923 |
+
).then(
|
| 924 |
+
lambda x: gr.update(visible=True) if x else gr.update(visible=False),
|
| 925 |
+
inputs=[audio_player],
|
| 926 |
+
outputs=[audio_player]
|
| 927 |
)
|
| 928 |
|
| 929 |
+
# Page navigation for PDF
|
| 930 |
page_slider.change(
|
| 931 |
update_image,
|
| 932 |
inputs=[page_slider, pdf_state],
|
| 933 |
outputs=[pdf_image]
|
| 934 |
)
|
| 935 |
|
| 936 |
+
# Clear conversation and reset UI
|
| 937 |
+
clear_btn.click(
|
| 938 |
+
lambda: (
|
| 939 |
+
[], None, "No file uploaded yet",
|
| 940 |
+
{"page_images": [], "total_pages": 0, "total_words": 0},
|
| 941 |
+
{"data_preview": "", "total_sheets": 0, "total_rows": 0},
|
| 942 |
+
"none", 0, None, "No PDF uploaded yet", "",
|
| 943 |
+
"No Excel file uploaded yet", None,
|
| 944 |
+
"Upload an image and click 'Analyze Image' to see results", None,
|
| 945 |
+
gr.update(visible=False), "Ready"
|
| 946 |
+
),
|
| 947 |
+
None,
|
| 948 |
+
[chatbot, current_session_id, file_status, pdf_state, excel_state,
|
| 949 |
+
file_type, page_slider, pdf_image, pdf_stats, excel_preview,
|
| 950 |
+
excel_stats, image_preview, image_analysis_results, audio_player,
|
| 951 |
+
audio_vis, audio_status_display]
|
|
|
|
| 952 |
)
|
| 953 |
|
| 954 |
+
# Add footer with creator attribution
|
| 955 |
+
gr.HTML("""
|
| 956 |
+
<div style="text-align: center; margin-top: 20px; padding: 10px; color: #666; font-size: 0.8rem; border-top: 1px solid #eee;">
|
| 957 |
+
Created by Calvin Allen Crawford
|
| 958 |
+
</div>
|
| 959 |
+
""")
|
| 960 |
|
| 961 |
# Launch the app
|
| 962 |
if __name__ == "__main__":
|