GitHub Actions commited on
Commit
800fbc2
Β·
1 Parent(s): cc80e6a

πŸš€ Automated sync from GitHub

Browse files
Files changed (1) hide show
  1. src/brain_agent.py +21 -5
src/brain_agent.py CHANGED
@@ -33,6 +33,20 @@ class AgentInterpretation:
33
  # 🟒 3. Restore your background knowledge refresh (if it relies on updating CSVs)
34
  self.refresh_knowledge_base()
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def _initialize_rag_databases(self):
37
  """Loads all heavy JSON personas into lightweight RAM vectors on startup."""
38
  if not os.path.exists(self.profiles_dir):
@@ -160,7 +174,7 @@ class AgentInterpretation:
160
  else:
161
  prompt = f"Generate a very short, engaging, single-sentence conversation starter or question about: {topic_str}."
162
 
163
- response = self.gemini_manager.generate_fast(prompt)
164
  #response = self.gemini_manager.client.models.generate_content(
165
  #model='gemini-2.0-flash',
166
  #contents=prompt
@@ -262,7 +276,7 @@ Return ONLY a valid JSON object with exactly these keys. Do not use markdown for
262
  # 🟒 3. API CALL & ERROR HANDLING (Kept exactly as it was)
263
  try:
264
  print(" -> 🧠 Awaiting API Response...")
265
- response = self.gemini_manager.generate_fast(prompt)
266
 
267
  print(" -> 🧠 Parsing JSON Response...")
268
 
@@ -297,7 +311,8 @@ Return ONLY a valid JSON object with exactly these keys. Do not use markdown for
297
  Output Strictly JSON: [ {{ "Dialect": "General", "Clarification": "...", "Tone": "...", "Context": "...", "Pragmatics": "..." }} ]
298
  """
299
  try:
300
- response = self.gemini_manager.generate_fast(prompt)
 
301
  clean_text = re.sub(r"```json|```", "", response.text).strip()
302
  data = json.loads(clean_text)
303
  return self.normalize_keys(data)
@@ -313,8 +328,9 @@ Return ONLY a valid JSON object with exactly these keys. Do not use markdown for
313
  Output JSON: {{ "clarification": "...", "pragmatics": "..." }}
314
  """
315
  try:
316
- response = self.gemini_manager.generate_fast(prompt)
317
- clean_json = re.search(r"\{.*\}", response.text, re.DOTALL)
 
318
  if clean_json:
319
  data = json.loads(clean_json.group(0))
320
  return data.get("clarification", db_row["Clarification"]), data.get("pragmatics", "AI Adapted Analysis")
 
33
  # 🟒 3. Restore your background knowledge refresh (if it relies on updating CSVs)
34
  self.refresh_knowledge_base()
35
 
36
+ def _safe_generate(self, prompt):
37
+ """
38
+ Safely runs the async Groq generation inside a synchronous thread
39
+ by creating an isolated event loop. This prevents Gradio UI freezing.
40
+ """
41
+ import asyncio
42
+ loop = asyncio.new_event_loop()
43
+ asyncio.set_event_loop(loop)
44
+ try:
45
+ # We await the async generate_fast method inside this isolated loop
46
+ return loop.run_until_complete(self.gemini_manager.generate_fast(prompt))
47
+ finally:
48
+ loop.close()
49
+
50
  def _initialize_rag_databases(self):
51
  """Loads all heavy JSON personas into lightweight RAM vectors on startup."""
52
  if not os.path.exists(self.profiles_dir):
 
174
  else:
175
  prompt = f"Generate a very short, engaging, single-sentence conversation starter or question about: {topic_str}."
176
 
177
+ response = self._safe_generate(prompt)
178
  #response = self.gemini_manager.client.models.generate_content(
179
  #model='gemini-2.0-flash',
180
  #contents=prompt
 
276
  # 🟒 3. API CALL & ERROR HANDLING (Kept exactly as it was)
277
  try:
278
  print(" -> 🧠 Awaiting API Response...")
279
+ response = self._safe_generate(prompt)
280
 
281
  print(" -> 🧠 Parsing JSON Response...")
282
 
 
311
  Output Strictly JSON: [ {{ "Dialect": "General", "Clarification": "...", "Tone": "...", "Context": "...", "Pragmatics": "..." }} ]
312
  """
313
  try:
314
+ response = self._safe_generate(prompt)
315
+
316
  clean_text = re.sub(r"```json|```", "", response.text).strip()
317
  data = json.loads(clean_text)
318
  return self.normalize_keys(data)
 
328
  Output JSON: {{ "clarification": "...", "pragmatics": "..." }}
329
  """
330
  try:
331
+ response = self._safe_generate(prompt)
332
+
333
+ clean_json = re.search(r"\{.*\}", response.text, re.DOTALL))
334
  if clean_json:
335
  data = json.loads(clean_json.group(0))
336
  return data.get("clarification", db_row["Clarification"]), data.get("pragmatics", "AI Adapted Analysis")