Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import pandas as pd
|
|
7 |
|
8 |
import google.generativeai as genai
|
9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
10 |
-
from smolagents.models.base import BaseModel
|
11 |
|
12 |
# Define the system prompt
|
13 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
@@ -18,13 +17,13 @@ If you're asked for a string, don’t use articles or abbreviations (e.g. for ci
|
|
18 |
|
19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
20 |
|
21 |
-
# Gemini model wrapper
|
22 |
-
class GeminiFlashModel
|
23 |
def __init__(self, model_name="gemini-1.5-flash", api_key=None):
|
24 |
self.model_name = model_name
|
25 |
-
self.api_key = api_key or os.getenv("
|
26 |
if not self.api_key:
|
27 |
-
raise ValueError("
|
28 |
genai.configure(api_key=self.api_key)
|
29 |
self.model = genai.GenerativeModel(model_name)
|
30 |
|
@@ -34,10 +33,9 @@ class GeminiFlashModel(BaseModel):
|
|
34 |
if not any(m["role"] == "system" for m in messages):
|
35 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
|
36 |
else:
|
37 |
-
raise TypeError("Expected 'messages' to be a list of
|
38 |
|
39 |
-
|
40 |
-
prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages])
|
41 |
|
42 |
try:
|
43 |
response = self.model.generate_content(prompt)
|
@@ -45,7 +43,7 @@ class GeminiFlashModel(BaseModel):
|
|
45 |
except Exception as e:
|
46 |
return f"GENERATION ERROR: {e}"
|
47 |
|
48 |
-
# Agent using Gemini
|
49 |
class MyAgent:
|
50 |
def __init__(self):
|
51 |
self.model = GeminiFlashModel(model_name="gemini-1.5-flash")
|
@@ -54,7 +52,6 @@ class MyAgent:
|
|
54 |
def __call__(self, question: str) -> str:
|
55 |
return self.agent.run(question)
|
56 |
|
57 |
-
# Evaluation & submission flow
|
58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
59 |
space_id = os.getenv("SPACE_ID")
|
60 |
|
@@ -63,31 +60,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
63 |
print(f"User logged in: {username}")
|
64 |
else:
|
65 |
print("User not logged in.")
|
66 |
-
return "Please login to Hugging Face
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
submit_url = f"{api_url}/submit"
|
71 |
|
72 |
try:
|
73 |
agent = MyAgent()
|
74 |
except Exception as e:
|
75 |
return f"Error initializing agent: {e}", None
|
76 |
|
77 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
78 |
-
print(f"Fetching questions from: {questions_url}")
|
79 |
try:
|
80 |
response = requests.get(questions_url, timeout=15)
|
81 |
response.raise_for_status()
|
82 |
questions_data = response.json()
|
83 |
-
if not questions_data:
|
84 |
-
return "Fetched questions list is empty or invalid format.", None
|
85 |
-
print(f"Fetched {len(questions_data)} questions.")
|
86 |
except Exception as e:
|
87 |
return f"Error fetching questions: {e}", None
|
88 |
|
89 |
results_log = []
|
90 |
answers_payload = []
|
|
|
91 |
for item in questions_data:
|
92 |
task_id = item.get("task_id")
|
93 |
question_text = item.get("question")
|
@@ -98,15 +90,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
98 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
99 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
100 |
except Exception as e:
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
103 |
|
104 |
if not answers_payload:
|
105 |
-
return "Agent did not
|
106 |
|
107 |
submission_data = {
|
108 |
-
"username": username.strip(),
|
109 |
-
"agent_code":
|
110 |
"answers": answers_payload
|
111 |
}
|
112 |
|
@@ -117,56 +112,33 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
117 |
final_status = (
|
118 |
f"Submission Successful!\n"
|
119 |
f"User: {result_data.get('username')}\n"
|
120 |
-
f"
|
121 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
122 |
f"Message: {result_data.get('message', 'No message received.')}"
|
123 |
)
|
124 |
-
|
125 |
-
return final_status, results_df
|
126 |
-
except requests.exceptions.HTTPError as e:
|
127 |
-
try:
|
128 |
-
detail = e.response.json().get("detail", e.response.text)
|
129 |
-
except Exception:
|
130 |
-
detail = e.response.text[:500]
|
131 |
-
return f"Submission Failed: {detail}", pd.DataFrame(results_log)
|
132 |
-
except requests.exceptions.Timeout:
|
133 |
-
return "Submission Failed: The request timed out.", pd.DataFrame(results_log)
|
134 |
except Exception as e:
|
135 |
-
return f"
|
136 |
|
137 |
# Gradio UI
|
138 |
with gr.Blocks() as demo:
|
139 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
140 |
gr.Markdown("""
|
141 |
**Instructions:**
|
142 |
-
1. Clone this space and
|
143 |
-
2. Log in
|
144 |
-
3.
|
145 |
""")
|
146 |
|
147 |
gr.LoginButton()
|
148 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
149 |
-
|
150 |
-
|
151 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
152 |
|
153 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
-
print("
|
157 |
-
space_host = os.getenv("SPACE_HOST")
|
158 |
-
space_id = os.getenv("SPACE_ID")
|
159 |
-
|
160 |
-
if space_host:
|
161 |
-
print(f"✅ SPACE_HOST: {space_host} -> https://{space_host}.hf.space")
|
162 |
-
else:
|
163 |
-
print("ℹ️ SPACE_HOST not set.")
|
164 |
-
|
165 |
-
if space_id:
|
166 |
-
print(f"✅ SPACE_ID: {space_id}")
|
167 |
-
else:
|
168 |
-
print("ℹ️ SPACE_ID not set.")
|
169 |
-
|
170 |
demo.launch(debug=True, share=False)
|
171 |
|
172 |
|
|
|
7 |
|
8 |
import google.generativeai as genai
|
9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
|
|
10 |
|
11 |
# Define the system prompt
|
12 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
|
|
17 |
|
18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
19 |
|
20 |
+
# Gemini model wrapper (lightweight, no smolagents.model.base)
|
21 |
+
class GeminiFlashModel:
|
22 |
def __init__(self, model_name="gemini-1.5-flash", api_key=None):
|
23 |
self.model_name = model_name
|
24 |
+
self.api_key = api_key or os.getenv("GEMINI_API_KEY")
|
25 |
if not self.api_key:
|
26 |
+
raise ValueError("GEMINI_API_KEY is not set.")
|
27 |
genai.configure(api_key=self.api_key)
|
28 |
self.model = genai.GenerativeModel(model_name)
|
29 |
|
|
|
33 |
if not any(m["role"] == "system" for m in messages):
|
34 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
|
35 |
else:
|
36 |
+
raise TypeError("Expected 'messages' to be a list of dicts.")
|
37 |
|
38 |
+
prompt = "\n".join(f"{m['role'].capitalize()}: {m['content']}" for m in messages)
|
|
|
39 |
|
40 |
try:
|
41 |
response = self.model.generate_content(prompt)
|
|
|
43 |
except Exception as e:
|
44 |
return f"GENERATION ERROR: {e}"
|
45 |
|
46 |
+
# Agent using Gemini
|
47 |
class MyAgent:
|
48 |
def __init__(self):
|
49 |
self.model = GeminiFlashModel(model_name="gemini-1.5-flash")
|
|
|
52 |
def __call__(self, question: str) -> str:
|
53 |
return self.agent.run(question)
|
54 |
|
|
|
55 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
56 |
space_id = os.getenv("SPACE_ID")
|
57 |
|
|
|
60 |
print(f"User logged in: {username}")
|
61 |
else:
|
62 |
print("User not logged in.")
|
63 |
+
return "Please login to Hugging Face.", None
|
64 |
|
65 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
66 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
|
|
67 |
|
68 |
try:
|
69 |
agent = MyAgent()
|
70 |
except Exception as e:
|
71 |
return f"Error initializing agent: {e}", None
|
72 |
|
|
|
|
|
73 |
try:
|
74 |
response = requests.get(questions_url, timeout=15)
|
75 |
response.raise_for_status()
|
76 |
questions_data = response.json()
|
|
|
|
|
|
|
77 |
except Exception as e:
|
78 |
return f"Error fetching questions: {e}", None
|
79 |
|
80 |
results_log = []
|
81 |
answers_payload = []
|
82 |
+
|
83 |
for item in questions_data:
|
84 |
task_id = item.get("task_id")
|
85 |
question_text = item.get("question")
|
|
|
90 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
91 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
92 |
except Exception as e:
|
93 |
+
results_log.append({
|
94 |
+
"Task ID": task_id,
|
95 |
+
"Question": question_text,
|
96 |
+
"Submitted Answer": f"AGENT ERROR: {e}"
|
97 |
+
})
|
98 |
|
99 |
if not answers_payload:
|
100 |
+
return "Agent did not return any answers.", pd.DataFrame(results_log)
|
101 |
|
102 |
submission_data = {
|
103 |
+
"username": profile.username.strip(),
|
104 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
105 |
"answers": answers_payload
|
106 |
}
|
107 |
|
|
|
112 |
final_status = (
|
113 |
f"Submission Successful!\n"
|
114 |
f"User: {result_data.get('username')}\n"
|
115 |
+
f"Score: {result_data.get('score', 'N/A')}% "
|
116 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
117 |
f"Message: {result_data.get('message', 'No message received.')}"
|
118 |
)
|
119 |
+
return final_status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
except Exception as e:
|
121 |
+
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
122 |
|
123 |
# Gradio UI
|
124 |
with gr.Blocks() as demo:
|
125 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
126 |
gr.Markdown("""
|
127 |
**Instructions:**
|
128 |
+
1. Clone this space and configure your Gemini API key.
|
129 |
+
2. Log in to Hugging Face.
|
130 |
+
3. Run your agent on evaluation tasks and submit answers.
|
131 |
""")
|
132 |
|
133 |
gr.LoginButton()
|
134 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
135 |
+
status_output = gr.Textbox(label="Submission Result", lines=5, interactive=False)
|
136 |
+
results_table = gr.DataFrame(label="Results", wrap=True)
|
|
|
137 |
|
138 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
+
print("🔧 App starting...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
demo.launch(debug=True, share=False)
|
143 |
|
144 |
|