vaibhavi18092002 commited on
Commit
5d0f6c3
Β·
verified Β·
1 Parent(s): 751db97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -31
app.py CHANGED
@@ -1,50 +1,133 @@
1
- # Add this class replacing the current GeminiAgent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  class MultiAgent:
3
  def __init__(self):
4
  print("MultiAgent initialized with Gemini fallback.")
5
 
6
- # Load primary Gemini key
7
- self.gemini_key = os.getenv("GEMINI_API_KEY")
8
- self.alt_key = os.getenv("GEMINI_API_KEY_2") # Second key (optional)
9
 
10
- if not self.gemini_key:
11
  raise ValueError("GEMINI_API_KEY environment variable not set.")
12
 
13
- import google.generativeai as genai
14
- self.genai = genai
15
-
16
- # Primary client
17
- self.client_primary = genai.Client(api_key=self.gemini_key)
18
- # Secondary client (fallback)
19
- self.client_secondary = genai.Client(api_key=self.alt_key) if self.alt_key else None
20
-
21
  self.model_name = "gemini-2.5-flash-preview-04-17"
22
 
23
- def _query_model(self, client, question):
 
 
 
 
 
24
  prompt = f"Answer concisely without explanation: {question}"
25
- response = client.models.generate_content(
26
- model=self.model_name,
27
- contents=prompt
28
- )
29
  return response.text.strip().rstrip(".!?")
30
 
31
- def __call__(self, question: str) -> str:
32
- print(f"MultiAgent received question: {question[:50]}...")
33
  try:
34
- return self._query_model(self.client_primary, question)
35
  except Exception as e:
36
  print(f"⚠️ Primary Gemini failed: {e}")
37
  if self.client_secondary:
38
  try:
39
- print("Attempting fallback Gemini key...")
40
- return self._query_model(self.client_secondary, question)
 
 
41
  except Exception as e2:
42
  print(f"❌ Secondary Gemini failed: {e2}")
43
- return "AGENT ERROR"
44
- else:
45
- return "AGENT ERROR"
46
-
47
- # Then replace:
48
- # agent = GeminiAgent()
49
- # with:
50
- # agent = MultiAgent()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+
6
+ # Try to import Google GenAI SDK
7
+ try:
8
+ from google import generativeai as genai
9
+ except ImportError:
10
+ raise ImportError("Please install the 'google-generativeai' package: pip install google-generativeai")
11
+
12
+ # --- Constants ---
13
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
+
15
+ # --- Multi-Key Gemini Agent ---
16
  class MultiAgent:
17
  def __init__(self):
18
  print("MultiAgent initialized with Gemini fallback.")
19
 
20
+ self.primary_key = os.getenv("GEMINI_API_KEY")
21
+ self.secondary_key = os.getenv("GEMINI_API_KEY_2")
 
22
 
23
+ if not self.primary_key:
24
  raise ValueError("GEMINI_API_KEY environment variable not set.")
25
 
26
+ genai.configure(api_key=self.primary_key)
 
 
 
 
 
 
 
27
  self.model_name = "gemini-2.5-flash-preview-04-17"
28
 
29
+ self.client_primary = genai.GenerativeModel(self.model_name)
30
+ self.client_secondary = None
31
+ if self.secondary_key:
32
+ self.client_secondary = genai.GenerativeModel(self.model_name)
33
+
34
+ def query(self, client, question):
35
  prompt = f"Answer concisely without explanation: {question}"
36
+ response = client.generate_content(prompt)
 
 
 
37
  return response.text.strip().rstrip(".!?")
38
 
39
+ def __call__(self, question):
40
+ print(f"🧠 GeminiAgent received question: {question[:60]}")
41
  try:
42
+ return self.query(self.client_primary, question)
43
  except Exception as e:
44
  print(f"⚠️ Primary Gemini failed: {e}")
45
  if self.client_secondary:
46
  try:
47
+ print("πŸ” Trying fallback Gemini key...")
48
+ genai.configure(api_key=self.secondary_key)
49
+ self.client_secondary = genai.GenerativeModel(self.model_name)
50
+ return self.query(self.client_secondary, question)
51
  except Exception as e2:
52
  print(f"❌ Secondary Gemini failed: {e2}")
53
+ return "AGENT ERROR"
54
+
55
+ # --- Run and Submit ---
56
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
57
+ space_id = os.getenv("SPACE_ID")
58
+ if not profile:
59
+ return "Please Login to Hugging Face with the button.", None
60
+ username = f"{profile.username}"
61
+
62
+ questions_url = f"{DEFAULT_API_URL}/questions"
63
+ submit_url = f"{DEFAULT_API_URL}/submit"
64
+ try:
65
+ agent = MultiAgent()
66
+ except Exception as e:
67
+ return f"Agent initialization failed: {e}", None
68
+
69
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
70
+ try:
71
+ response = requests.get(questions_url, timeout=15)
72
+ response.raise_for_status()
73
+ questions_data = response.json()
74
+ if not questions_data:
75
+ return "No questions received.", None
76
+ except Exception as e:
77
+ return f"Failed to fetch questions: {e}", None
78
+
79
+ results_log = []
80
+ answers_payload = []
81
+ for item in questions_data:
82
+ task_id = item.get("task_id")
83
+ question = item.get("question")
84
+ if not task_id or not question:
85
+ continue
86
+ try:
87
+ answer = agent(question)
88
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
89
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
90
+ except Exception as e:
91
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"AGENT ERROR: {e}"})
92
+
93
+ if not answers_payload:
94
+ return "No answers submitted.", pd.DataFrame(results_log)
95
+
96
+ submission_data = {
97
+ "username": username.strip(),
98
+ "agent_code": agent_code,
99
+ "answers": answers_payload
100
+ }
101
+
102
+ try:
103
+ response = requests.post(submit_url, json=submission_data, timeout=60)
104
+ response.raise_for_status()
105
+ result = response.json()
106
+ status = (
107
+ f"βœ… Submission Successful!\n"
108
+ f"User: {result.get('username')}\n"
109
+ f"Overall Score: {result.get('score', 'N/A')}% "
110
+ f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
111
+ f"Message: {result.get('message', '')}"
112
+ )
113
+ return status, pd.DataFrame(results_log)
114
+ except Exception as e:
115
+ return f"Submission failed: {e}", pd.DataFrame(results_log)
116
+
117
+ # --- Gradio UI ---
118
+ with gr.Blocks() as demo:
119
+ gr.Markdown("""# 🌟 Gemini Agent Evaluation
120
+ 1. Login to Hugging Face
121
+ 2. Click below to run your Gemini agent
122
+ 3. Submit answers and view your score
123
+ """)
124
+ gr.LoginButton()
125
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
126
+ status_box = gr.Textbox(label="Submission Status", lines=6, interactive=False)
127
+ results_df = gr.DataFrame(label="Results Table", wrap=True)
128
+
129
+ run_btn.click(fn=run_and_submit_all, outputs=[status_box, results_df])
130
+
131
+ if __name__ == "__main__":
132
+ print("\n--- App Starting ---")
133
+ demo.launch(debug=True, share=False)