Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,13 @@ from pathlib import Path
|
|
11 |
# Core dependencies
|
12 |
try:
|
13 |
from together import Together
|
14 |
-
import PyPDF2
|
15 |
import speech_recognition as sr
|
16 |
import io
|
17 |
import subprocess
|
18 |
import sys
|
19 |
except ImportError as e:
|
20 |
print(f"Missing dependency: {e}")
|
21 |
-
print("Install with: pip install together
|
22 |
sys.exit(1)
|
23 |
|
24 |
class ConversationMemory:
|
@@ -45,7 +44,7 @@ class ConversationMemory:
|
|
45 |
self.context_graph = {}
|
46 |
self.session_data = {}
|
47 |
|
48 |
-
def get_relevant_context(self, query: str,
|
49 |
relevant = []
|
50 |
query_lower = query.lower()
|
51 |
|
@@ -85,18 +84,6 @@ class NexusAI:
|
|
85 |
except Exception as e:
|
86 |
return False, f"❌ Failed to initialize API key: {str(e)}"
|
87 |
|
88 |
-
def extract_text_from_pdf(self, pdf_path: str) -> str:
|
89 |
-
"""Extract text from PDF file"""
|
90 |
-
try:
|
91 |
-
with open(pdf_path, 'rb') as file:
|
92 |
-
pdf_reader = PyPDF2.PdfReader(file)
|
93 |
-
text = ""
|
94 |
-
for page in pdf_reader.pages:
|
95 |
-
text += page.extract_text() + "\n"
|
96 |
-
return text.strip()
|
97 |
-
except Exception as e:
|
98 |
-
return f"Error reading PDF: {str(e)}"
|
99 |
-
|
100 |
def transcribe_audio(self, audio_path: str) -> str:
|
101 |
"""Transcribe audio to text"""
|
102 |
try:
|
@@ -137,7 +124,7 @@ class NexusAI:
|
|
137 |
messages = []
|
138 |
|
139 |
system_msg = """You are Nexus AI, a creative multimodal assistant that helps users across different types of content.
|
140 |
-
You excel at connecting insights across text,
|
141 |
contextual responses that build on previous interactions when relevant."""
|
142 |
|
143 |
messages.append({"role": "system", "content": system_msg})
|
@@ -209,21 +196,6 @@ def process_text_input(user_input: str, api_key_status: str) -> str:
|
|
209 |
|
210 |
return nexus_ai.generate_response(user_input, "text")
|
211 |
|
212 |
-
def process_pdf_input(pdf_file, user_question: str, api_key_status: str) -> str:
|
213 |
-
"""Process PDF input with question"""
|
214 |
-
if api_key_status != "success":
|
215 |
-
return "❌ Please initialize your Together AI API key first!"
|
216 |
-
|
217 |
-
if pdf_file is None:
|
218 |
-
return "Please upload a PDF file first!"
|
219 |
-
|
220 |
-
extracted_text = nexus_ai.extract_text_from_pdf(pdf_file)
|
221 |
-
|
222 |
-
if user_question.strip():
|
223 |
-
return nexus_ai.generate_response(user_question, "pdf", extracted_text)
|
224 |
-
else:
|
225 |
-
return nexus_ai.generate_response("Please summarize this document", "pdf", extracted_text)
|
226 |
-
|
227 |
def process_audio_input(audio_file, user_question: str, api_key_status: str) -> str:
|
228 |
"""Process audio input with question"""
|
229 |
if api_key_status != "success":
|
@@ -285,10 +257,6 @@ def clear_text_inputs():
|
|
285 |
"""Clear text input and output"""
|
286 |
return "", ""
|
287 |
|
288 |
-
def clear_pdf_inputs():
|
289 |
-
"""Clear PDF input and output"""
|
290 |
-
return None, "", ""
|
291 |
-
|
292 |
def clear_audio_inputs():
|
293 |
"""Clear audio input and output"""
|
294 |
return None, "", ""
|
@@ -496,39 +464,6 @@ def create_nexus_interface():
|
|
496 |
elem_classes=["scrollable-textarea"]
|
497 |
)
|
498 |
|
499 |
-
with gr.Tab("📄 PDF Analysis"):
|
500 |
-
with gr.Row():
|
501 |
-
with gr.Column(scale=1, elem_classes=["input-card"]):
|
502 |
-
pdf_file = gr.File(
|
503 |
-
label="Upload PDF",
|
504 |
-
file_types=[".pdf"]
|
505 |
-
)
|
506 |
-
pdf_question = gr.Textbox(
|
507 |
-
label="Question about PDF (optional)",
|
508 |
-
placeholder="What would you like to know about this document?",
|
509 |
-
lines=3,
|
510 |
-
elem_classes=["scrollable-textarea"]
|
511 |
-
)
|
512 |
-
with gr.Row():
|
513 |
-
pdf_btn = gr.Button(
|
514 |
-
"Analyze PDF",
|
515 |
-
variant="primary",
|
516 |
-
elem_classes=["primary-button"]
|
517 |
-
)
|
518 |
-
pdf_clear_btn = gr.Button(
|
519 |
-
"Clear",
|
520 |
-
variant="secondary",
|
521 |
-
elem_classes=["danger-button"]
|
522 |
-
)
|
523 |
-
|
524 |
-
with gr.Column(scale=1, elem_classes=["output-card"]):
|
525 |
-
pdf_output = gr.Textbox(
|
526 |
-
label="Analysis Result",
|
527 |
-
lines=12,
|
528 |
-
interactive=False,
|
529 |
-
elem_classes=["scrollable-textarea"]
|
530 |
-
)
|
531 |
-
|
532 |
with gr.Tab("🎤 Voice Processing"):
|
533 |
with gr.Row():
|
534 |
with gr.Column(scale=1, elem_classes=["input-card"]):
|
@@ -654,18 +589,6 @@ def create_nexus_interface():
|
|
654 |
outputs=[text_input, text_output]
|
655 |
)
|
656 |
|
657 |
-
pdf_btn.click(
|
658 |
-
fn=process_pdf_input,
|
659 |
-
inputs=[pdf_file, pdf_question, api_key_state],
|
660 |
-
outputs=[pdf_output]
|
661 |
-
)
|
662 |
-
|
663 |
-
pdf_clear_btn.click(
|
664 |
-
fn=clear_pdf_inputs,
|
665 |
-
inputs=[],
|
666 |
-
outputs=[pdf_file, pdf_question, pdf_output]
|
667 |
-
)
|
668 |
-
|
669 |
audio_btn.click(
|
670 |
fn=process_audio_input,
|
671 |
inputs=[audio_file, audio_question, api_key_state],
|
|
|
11 |
# Core dependencies
|
12 |
try:
|
13 |
from together import Together
|
|
|
14 |
import speech_recognition as sr
|
15 |
import io
|
16 |
import subprocess
|
17 |
import sys
|
18 |
except ImportError as e:
|
19 |
print(f"Missing dependency: {e}")
|
20 |
+
print("Install with: pip install together speechrecognition pyaudio")
|
21 |
sys.exit(1)
|
22 |
|
23 |
class ConversationMemory:
|
|
|
44 |
self.context_graph = {}
|
45 |
self.session_data = {}
|
46 |
|
47 |
+
def get_relevant_context(self, query: str, hope: int = 3) -> List[Dict]:
|
48 |
relevant = []
|
49 |
query_lower = query.lower()
|
50 |
|
|
|
84 |
except Exception as e:
|
85 |
return False, f"❌ Failed to initialize API key: {str(e)}"
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
def transcribe_audio(self, audio_path: str) -> str:
|
88 |
"""Transcribe audio to text"""
|
89 |
try:
|
|
|
124 |
messages = []
|
125 |
|
126 |
system_msg = """You are Nexus AI, a creative multimodal assistant that helps users across different types of content.
|
127 |
+
You excel at connecting insights across text, voice, and code. Always provide helpful,
|
128 |
contextual responses that build on previous interactions when relevant."""
|
129 |
|
130 |
messages.append({"role": "system", "content": system_msg})
|
|
|
196 |
|
197 |
return nexus_ai.generate_response(user_input, "text")
|
198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
def process_audio_input(audio_file, user_question: str, api_key_status: str) -> str:
|
200 |
"""Process audio input with question"""
|
201 |
if api_key_status != "success":
|
|
|
257 |
"""Clear text input and output"""
|
258 |
return "", ""
|
259 |
|
|
|
|
|
|
|
|
|
260 |
def clear_audio_inputs():
|
261 |
"""Clear audio input and output"""
|
262 |
return None, "", ""
|
|
|
464 |
elem_classes=["scrollable-textarea"]
|
465 |
)
|
466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
with gr.Tab("🎤 Voice Processing"):
|
468 |
with gr.Row():
|
469 |
with gr.Column(scale=1, elem_classes=["input-card"]):
|
|
|
589 |
outputs=[text_input, text_output]
|
590 |
)
|
591 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
592 |
audio_btn.click(
|
593 |
fn=process_audio_input,
|
594 |
inputs=[audio_file, audio_question, api_key_state],
|