Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
# app.py - FULLY WORKING
|
|
|
|
| 2 |
import re
|
| 3 |
import logging
|
| 4 |
import tempfile
|
|
@@ -11,30 +12,38 @@ import faiss
|
|
| 11 |
import gradio as gr
|
| 12 |
from gtts import gTTS
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class AgenticRAGAgent:
|
| 18 |
def __init__(self):
|
| 19 |
self.chunks = []
|
| 20 |
self.index = None
|
| 21 |
self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 22 |
-
self.groq = None
|
| 23 |
-
|
| 24 |
-
# ===============================
|
| 25 |
-
# π Initialize Groq client here
|
| 26 |
-
# ===============================
|
| 27 |
-
try:
|
| 28 |
-
from groq import Groq
|
| 29 |
-
self.groq = Groq(api_key="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") # <- HARD-CODED
|
| 30 |
-
print("β
Groq client initialized successfully!")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
self.groq = None
|
| 33 |
-
print("β Groq initialization error:", e)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
# Emoji removal and clean-up
|
| 37 |
-
# -------------------------------
|
| 38 |
def remove_emojis(self, text: str) -> str:
|
| 39 |
emoji_pattern = re.compile("["
|
| 40 |
u"\U0001F600-\U0001F64F"
|
|
@@ -67,9 +76,6 @@ class AgenticRAGAgent:
|
|
| 67 |
logger.error(f"Voice generation failed: {e}")
|
| 68 |
return None
|
| 69 |
|
| 70 |
-
# -------------------------------
|
| 71 |
-
# PDF Upload & Embeddings
|
| 72 |
-
# -------------------------------
|
| 73 |
def upload_pdfs(self, files):
|
| 74 |
if not files:
|
| 75 |
return "No files selected."
|
|
@@ -87,7 +93,7 @@ class AgenticRAGAgent:
|
|
| 87 |
content = file.read() if hasattr(file, 'read') else open(file.name, 'rb').read()
|
| 88 |
with open(dest, "wb") as f:
|
| 89 |
f.write(content)
|
| 90 |
-
except Exception:
|
| 91 |
continue
|
| 92 |
|
| 93 |
text = ""
|
|
@@ -118,10 +124,8 @@ class AgenticRAGAgent:
|
|
| 118 |
|
| 119 |
return f"Loaded {count} PDF(s) β {len(all_chunks)} chunks ready!"
|
| 120 |
|
| 121 |
-
# -------------------------------
|
| 122 |
-
# Ask / Chat function
|
| 123 |
-
# -------------------------------
|
| 124 |
def ask(self, question: str, history: List):
|
|
|
|
| 125 |
if not question.strip():
|
| 126 |
return history, None
|
| 127 |
|
|
@@ -145,11 +149,11 @@ class AgenticRAGAgent:
|
|
| 145 |
|
| 146 |
prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
|
| 147 |
|
| 148 |
-
if
|
| 149 |
reply = "GROQ_API_KEY is missing or invalid."
|
| 150 |
else:
|
| 151 |
try:
|
| 152 |
-
resp =
|
| 153 |
model="llama-3.3-70b-versatile",
|
| 154 |
messages=[{"role": "user", "content": prompt}],
|
| 155 |
temperature=0.3,
|
|
@@ -162,9 +166,9 @@ class AgenticRAGAgent:
|
|
| 162 |
history.append([question, reply])
|
| 163 |
return history, self.generate_voice(reply)
|
| 164 |
|
| 165 |
-
# ===============================
|
| 166 |
-
#
|
| 167 |
-
# ===============================
|
| 168 |
def create_interface():
|
| 169 |
agent = AgenticRAGAgent()
|
| 170 |
|
|
|
|
| 1 |
+
# app.py - FULLY WORKING AI RESEARCH AGENT WITH GROQ CLIENT
|
| 2 |
+
import os
|
| 3 |
import re
|
| 4 |
import logging
|
| 5 |
import tempfile
|
|
|
|
| 12 |
import gradio as gr
|
| 13 |
from gtts import gTTS
|
| 14 |
|
| 15 |
+
# Safe Groq import
|
| 16 |
+
try:
|
| 17 |
+
from groq import Groq
|
| 18 |
+
GROQ_OK = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
GROQ_OK = False
|
| 21 |
+
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
+
# ===============================
|
| 26 |
+
# π HARDCODE YOUR GROQ API KEY HERE (GLOBAL)
|
| 27 |
+
# ===============================
|
| 28 |
+
GROQ_API_KEY = "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"
|
| 29 |
+
groq_client = None
|
| 30 |
+
|
| 31 |
+
if GROQ_OK:
|
| 32 |
+
try:
|
| 33 |
+
print("DEBUG β Initializing Groq client...")
|
| 34 |
+
groq_client = Groq(api_key=GROQ_API_KEY) # <- only api_key
|
| 35 |
+
print("DEBUG β Groq client initialized successfully!")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
groq_client = None
|
| 38 |
+
print("β Groq initialization error:", e)
|
| 39 |
+
|
| 40 |
class AgenticRAGAgent:
|
| 41 |
def __init__(self):
|
| 42 |
self.chunks = []
|
| 43 |
self.index = None
|
| 44 |
self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Remove emojis completely
|
|
|
|
|
|
|
| 47 |
def remove_emojis(self, text: str) -> str:
|
| 48 |
emoji_pattern = re.compile("["
|
| 49 |
u"\U0001F600-\U0001F64F"
|
|
|
|
| 76 |
logger.error(f"Voice generation failed: {e}")
|
| 77 |
return None
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
def upload_pdfs(self, files):
|
| 80 |
if not files:
|
| 81 |
return "No files selected."
|
|
|
|
| 93 |
content = file.read() if hasattr(file, 'read') else open(file.name, 'rb').read()
|
| 94 |
with open(dest, "wb") as f:
|
| 95 |
f.write(content)
|
| 96 |
+
except Exception as e:
|
| 97 |
continue
|
| 98 |
|
| 99 |
text = ""
|
|
|
|
| 124 |
|
| 125 |
return f"Loaded {count} PDF(s) β {len(all_chunks)} chunks ready!"
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
def ask(self, question: str, history: List):
|
| 128 |
+
global groq_client
|
| 129 |
if not question.strip():
|
| 130 |
return history, None
|
| 131 |
|
|
|
|
| 149 |
|
| 150 |
prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
|
| 151 |
|
| 152 |
+
if groq_client is None:
|
| 153 |
reply = "GROQ_API_KEY is missing or invalid."
|
| 154 |
else:
|
| 155 |
try:
|
| 156 |
+
resp = groq_client.chat.completions.create(
|
| 157 |
model="llama-3.3-70b-versatile",
|
| 158 |
messages=[{"role": "user", "content": prompt}],
|
| 159 |
temperature=0.3,
|
|
|
|
| 166 |
history.append([question, reply])
|
| 167 |
return history, self.generate_voice(reply)
|
| 168 |
|
| 169 |
+
# =========================================
|
| 170 |
+
# GRADIO UI
|
| 171 |
+
# =========================================
|
| 172 |
def create_interface():
|
| 173 |
agent = AgenticRAGAgent()
|
| 174 |
|