Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py - FULLY WORKING END-TO-END (
|
| 2 |
import re
|
| 3 |
import logging
|
| 4 |
import tempfile
|
|
@@ -11,42 +11,30 @@ import faiss
|
|
| 11 |
import gradio as gr
|
| 12 |
from gtts import gTTS
|
| 13 |
|
| 14 |
-
# ===============================
|
| 15 |
-
# Safe Groq import
|
| 16 |
-
# ===============================
|
| 17 |
-
try:
|
| 18 |
-
from groq import Groq
|
| 19 |
-
GROQ_OK = True
|
| 20 |
-
except ImportError:
|
| 21 |
-
GROQ_OK = False
|
| 22 |
-
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
-
# ===============================
|
| 27 |
-
# π Hardcoded Groq API Key (GLOBAL)
|
| 28 |
-
# ===============================
|
| 29 |
-
GROQ_API_KEY = "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"
|
| 30 |
-
groq_client = None
|
| 31 |
-
|
| 32 |
-
if GROQ_OK:
|
| 33 |
-
try:
|
| 34 |
-
print("DEBUG β Initializing Groq client...")
|
| 35 |
-
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 36 |
-
print("DEBUG β Groq client initialized successfully!")
|
| 37 |
-
except Exception as e:
|
| 38 |
-
groq_client = None
|
| 39 |
-
print("DEBUG β Groq initialization error:", e)
|
| 40 |
-
|
| 41 |
-
# ===============================
|
| 42 |
-
# Agentic RAG Agent
|
| 43 |
-
# ===============================
|
| 44 |
class AgenticRAGAgent:
|
| 45 |
def __init__(self):
|
| 46 |
self.chunks = []
|
| 47 |
self.index = None
|
| 48 |
self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
def remove_emojis(self, text: str) -> str:
|
| 51 |
emoji_pattern = re.compile("["
|
| 52 |
u"\U0001F600-\U0001F64F"
|
|
@@ -79,6 +67,9 @@ class AgenticRAGAgent:
|
|
| 79 |
logger.error(f"Voice generation failed: {e}")
|
| 80 |
return None
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
def upload_pdfs(self, files):
|
| 83 |
if not files:
|
| 84 |
return "No files selected."
|
|
@@ -127,8 +118,10 @@ class AgenticRAGAgent:
|
|
| 127 |
|
| 128 |
return f"Loaded {count} PDF(s) β {len(all_chunks)} chunks ready!"
|
| 129 |
|
|
|
|
|
|
|
|
|
|
| 130 |
def ask(self, question: str, history: List):
|
| 131 |
-
global groq_client
|
| 132 |
if not question.strip():
|
| 133 |
return history, None
|
| 134 |
|
|
@@ -152,11 +145,11 @@ class AgenticRAGAgent:
|
|
| 152 |
|
| 153 |
prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
|
| 154 |
|
| 155 |
-
if
|
| 156 |
reply = "GROQ_API_KEY is missing or invalid."
|
| 157 |
else:
|
| 158 |
try:
|
| 159 |
-
resp =
|
| 160 |
model="llama-3.3-70b-versatile",
|
| 161 |
messages=[{"role": "user", "content": prompt}],
|
| 162 |
temperature=0.3,
|
|
|
|
| 1 |
+
# app.py - FULLY WORKING END-TO-END (Groq key inside agent)
|
| 2 |
import re
|
| 3 |
import logging
|
| 4 |
import tempfile
|
|
|
|
| 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 |
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."
|
|
|
|
| 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 |
|
| 146 |
prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
|
| 147 |
|
| 148 |
+
if self.groq is None:
|
| 149 |
reply = "GROQ_API_KEY is missing or invalid."
|
| 150 |
else:
|
| 151 |
try:
|
| 152 |
+
resp = self.groq.chat.completions.create(
|
| 153 |
model="llama-3.3-70b-versatile",
|
| 154 |
messages=[{"role": "user", "content": prompt}],
|
| 155 |
temperature=0.3,
|