Zahid0123 commited on
Commit
7b26e18
Β·
verified Β·
1 Parent(s): 7df4687

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,5 +1,4 @@
1
- # app.py - COMPLETE, FINAL, 100% WORKING VERSION (November 21, 2025)
2
- # Your original UI + PDF upload fixed + Voice on every answer + Emojis removed from voice + Groq key hardcoded + debug
3
  import os
4
  import re
5
  import logging
@@ -23,28 +22,27 @@ except ImportError:
23
  logging.basicConfig(level=logging.INFO)
24
  logger = logging.getLogger(__name__)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  class AgenticRAGAgent:
27
  def __init__(self):
28
  self.chunks = []
29
  self.index = None
30
  self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
31
-
32
- # ==========================
33
- # Hardcoded Groq API key + debug
34
- # ==========================
35
- self.groq = None
36
- if GROQ_OK:
37
- try:
38
- # Hardcode your key here
39
- self.groq = Groq(api_key="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")
40
- print("DEBUG β†’ Groq client initialized successfully!")
41
- logger.info("Groq API key loaded and working!")
42
- except Exception as e:
43
- self.groq = None
44
- print("DEBUG β†’ Groq initialization failed:", e)
45
- logger.error(f"Groq init error: {e}")
46
 
47
- # Remove emojis completely from voice
48
  def remove_emojis(self, text: str) -> str:
49
  emoji_pattern = re.compile("["
50
  u"\U0001F600-\U0001F64F"
@@ -126,6 +124,7 @@ class AgenticRAGAgent:
126
  return f"Loaded {count} PDF(s) β†’ {len(all_chunks)} chunks ready!"
127
 
128
  def ask(self, question: str, history: List):
 
129
  if not question.strip():
130
  return history, None
131
 
@@ -149,12 +148,12 @@ class AgenticRAGAgent:
149
 
150
  prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
151
 
152
- if not self.groq:
153
  reply = "GROQ_API_KEY is missing or invalid."
154
  else:
155
  try:
156
- resp = self.groq.chat.completions.create(
157
- model="llama-3.3-70b-versatile", # Updated supported model
158
  messages=[{"role": "user", "content": prompt}],
159
  temperature=0.3,
160
  max_tokens=700
@@ -166,7 +165,7 @@ class AgenticRAGAgent:
166
  history.append([question, reply])
167
  return history, self.generate_voice(reply)
168
 
169
- # YOUR ORIGINAL UI - 100% EXACT AS YOU WANTED
170
  def create_interface():
171
  agent = AgenticRAGAgent()
172
 
 
1
+ # app.py - FULLY WORKING WITH GLOBAL GROQ CLIENT
 
2
  import os
3
  import re
4
  import logging
 
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)
35
+ print("DEBUG β†’ Groq client initialized successfully!")
36
+ except Exception as e:
37
+ groq_client = None
38
+ print("DEBUG β†’ 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
  def remove_emojis(self, text: str) -> str:
47
  emoji_pattern = re.compile("["
48
  u"\U0001F600-\U0001F64F"
 
124
  return f"Loaded {count} PDF(s) β†’ {len(all_chunks)} chunks ready!"
125
 
126
  def ask(self, question: str, history: List):
127
+ global groq_client # use global client
128
  if not question.strip():
129
  return history, None
130
 
 
148
 
149
  prompt = f"Context from documents:\n{context}\n\nQuestion: {question}\nAnswer clearly and accurately:"
150
 
151
+ if groq_client is None:
152
  reply = "GROQ_API_KEY is missing or invalid."
153
  else:
154
  try:
155
+ resp = groq_client.chat.completions.create(
156
+ model="llama-3.3-70b-versatile",
157
  messages=[{"role": "user", "content": prompt}],
158
  temperature=0.3,
159
  max_tokens=700
 
165
  history.append([question, reply])
166
  return history, self.generate_voice(reply)
167
 
168
+ # YOUR ORIGINAL UI
169
  def create_interface():
170
  agent = AgenticRAGAgent()
171