anujjuna commited on
Commit
fdaeca3
·
verified ·
1 Parent(s): 91b56e9

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +27 -16
agent.py CHANGED
@@ -64,17 +64,23 @@ def _mistral(prompt, key):
64
 
65
  def _gemini(prompt, key):
66
  if not key: return {}
67
- try:
68
- r = requests.post(
69
- f"https://generativelanguage.googleapis.com/v1beta/models/"
70
- f"gemini-2.5-flash:generateContent?key={key}",
71
- headers={"Content-Type":"application/json"},
72
- json={"contents":[{"parts":[{"text":prompt}]}],
73
- "generationConfig":{"temperature":0.2}}, timeout=15)
74
- d = r.json()
75
- if "candidates" not in d: return {}
76
- return _parse(d["candidates"][0]["content"]["parts"][0]["text"])
77
- except Exception as e: logger.warning("Gemini: %s",e); return {}
 
 
 
 
 
 
78
 
79
  # ---------------------------------------------------------------------------
80
  # Topic labelling prompt
@@ -213,13 +219,18 @@ def llm_council(state: PipelineState) -> dict:
213
  "single": round((total - n_triple - n_two) / total * 100),
214
  }
215
 
216
- # Save outputs
217
- records = sheets[4]
218
- with open("topics.json","w") as f: json.dump(records, f, indent=2)
219
- pd.DataFrame(records).to_csv("topics.csv", index=False)
 
 
 
 
220
 
221
  return {"interpretations":interps, "sheets":sheets,
222
- "agreement_rates":rates, "json_path":"topics.json", "csv_path":"topics.csv"}
 
223
 
224
  # ---------------------------------------------------------------------------
225
  # LangGraph node: build mismatch table
 
64
 
65
  def _gemini(prompt, key):
66
  if not key: return {}
67
+ for attempt in range(2):
68
+ try:
69
+ r = requests.post(
70
+ f"https://generativelanguage.googleapis.com/v1beta/models/"
71
+ f"gemini-2.5-flash:generateContent?key={key}",
72
+ headers={"Content-Type":"application/json"},
73
+ json={"contents":[{"parts":[{"text":prompt}]}],
74
+ "generationConfig":{"temperature":0.2}}, timeout=30)
75
+ d = r.json()
76
+ if "candidates" not in d:
77
+ logger.warning("Gemini attempt %d: no candidates: %s", attempt+1, d.get("error",""))
78
+ time.sleep(2); continue
79
+ return _parse(d["candidates"][0]["content"]["parts"][0]["text"])
80
+ except Exception as e:
81
+ logger.warning("Gemini attempt %d: %s", attempt+1, e)
82
+ time.sleep(2)
83
+ return {}
84
 
85
  # ---------------------------------------------------------------------------
86
  # Topic labelling prompt
 
219
  "single": round((total - n_triple - n_two) / total * 100),
220
  }
221
 
222
+ # Save outputs — 4 separate sheet files
223
+ sheet_paths = {}
224
+ names = {1:"sheet1_groq",2:"sheet2_mistral",3:"sheet3_gemini",4:"sheet4_consolidated"}
225
+ for sn, name in names.items():
226
+ path = f"{name}.csv"
227
+ pd.DataFrame(sheets[sn]).to_csv(path, index=False)
228
+ sheet_paths[sn] = path
229
+ with open("topics.json","w") as f: json.dump(sheets[4], f, indent=2)
230
 
231
  return {"interpretations":interps, "sheets":sheets,
232
+ "agreement_rates":rates, "sheet_paths":sheet_paths,
233
+ "json_path":"topics.json"}
234
 
235
  # ---------------------------------------------------------------------------
236
  # LangGraph node: build mismatch table