dlaima commited on
Commit
886a81f
·
verified ·
1 Parent(s): c8ff0c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -4,20 +4,19 @@ import os
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
-
8
  import google.generativeai as genai
9
  from smolagents import CodeAgent, DuckDuckGoSearchTool
10
 
11
- # System prompt used by the agent
12
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
13
- Report your thoughts, and finish your answer with just the answer — no prefixes like "FINAL ANSWER:".
14
  Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
15
  If you're asked for a number, don’t use commas or units like $ or %, unless specified.
16
  If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
17
 
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
- # Generation result wrapper to match smolagents expectations
21
  class GenerationResult:
22
  def __init__(self, content, token_usage=None, input_tokens=0, output_tokens=0):
23
  self.content = content
@@ -47,32 +46,31 @@ class GeminiFlashModel:
47
 
48
  try:
49
  response = self.model.generate_content(prompt)
50
- text_output = response.text.strip() if hasattr(response, "text") else str(response)
51
-
52
  return GenerationResult(
53
- content=text_output,
54
- token_usage={"input_tokens": 0, "output_tokens": 0},
55
  input_tokens=0,
56
  output_tokens=0
57
  )
58
  except Exception as e:
59
  return GenerationResult(
60
  content=f"GENERATION ERROR: {e}",
61
- token_usage={"input_tokens": 0, "output_tokens": 0},
62
  input_tokens=0,
63
  output_tokens=0
64
  )
65
 
66
- # Agent wrapper
67
  class MyAgent:
68
  def __init__(self):
69
- self.model = GeminiFlashModel(model_id="gemini-1.5-flash")
70
  self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
71
 
72
- def __call__(self, question: str) -> str:
73
  return self.agent.run(question)
74
 
75
- # Main evaluation function
 
76
  def run_and_submit_all(profile: gr.OAuthProfile | None):
77
  space_id = os.getenv("SPACE_ID")
78
 
@@ -106,10 +104,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
106
  question_text = item.get("question")
107
  if not task_id or question_text is None:
108
  continue
 
109
  try:
110
- submitted_answer = agent(question_text)
111
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
112
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  except Exception as e:
114
  results_log.append({
115
  "Task ID": task_id,
@@ -141,7 +157,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
141
  except Exception as e:
142
  return f"Submission failed: {e}", pd.DataFrame(results_log)
143
 
144
- # Gradio UI setup
145
  with gr.Blocks() as demo:
146
  gr.Markdown("# Basic Agent Evaluation Runner")
147
  gr.Markdown("""
@@ -159,7 +175,7 @@ with gr.Blocks() as demo:
159
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
160
 
161
  if __name__ == "__main__":
162
- print("🔧 App starting...")
163
  demo.launch(debug=True, share=False)
164
 
165
 
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
 
7
  import google.generativeai as genai
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool
9
 
10
+ # System prompt for Gemini
11
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
12
+ Report your thoughts, and finish your answer with just the answer — no prefixes like \"FINAL ANSWER:\".
13
  Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
14
  If you're asked for a number, don’t use commas or units like $ or %, unless specified.
15
  If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
16
 
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
19
+ # Generation result wrapper
20
  class GenerationResult:
21
  def __init__(self, content, token_usage=None, input_tokens=0, output_tokens=0):
22
  self.content = content
 
46
 
47
  try:
48
  response = self.model.generate_content(prompt)
 
 
49
  return GenerationResult(
50
+ content=response.text.strip(),
51
+ token_usage={},
52
  input_tokens=0,
53
  output_tokens=0
54
  )
55
  except Exception as e:
56
  return GenerationResult(
57
  content=f"GENERATION ERROR: {e}",
58
+ token_usage={},
59
  input_tokens=0,
60
  output_tokens=0
61
  )
62
 
63
+ # Agent class
64
  class MyAgent:
65
  def __init__(self):
66
+ self.model = GeminiFlashModel()
67
  self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
68
 
69
+ def __call__(self, question: str):
70
  return self.agent.run(question)
71
 
72
+ # Evaluation function
73
+
74
  def run_and_submit_all(profile: gr.OAuthProfile | None):
75
  space_id = os.getenv("SPACE_ID")
76
 
 
104
  question_text = item.get("question")
105
  if not task_id or question_text is None:
106
  continue
107
+
108
  try:
109
+ result = agent(question_text)
110
+
111
+ if hasattr(result, "content"):
112
+ submitted_answer = result.content
113
+ elif isinstance(result, dict) and "content" in result:
114
+ submitted_answer = result["content"]
115
+ else:
116
+ submitted_answer = str(result)
117
+
118
+ answers_payload.append({
119
+ "task_id": task_id,
120
+ "submitted_answer": submitted_answer
121
+ })
122
+
123
+ results_log.append({
124
+ "Task ID": task_id,
125
+ "Question": question_text,
126
+ "Submitted Answer": submitted_answer
127
+ })
128
+
129
  except Exception as e:
130
  results_log.append({
131
  "Task ID": task_id,
 
157
  except Exception as e:
158
  return f"Submission failed: {e}", pd.DataFrame(results_log)
159
 
160
+ # UI setup
161
  with gr.Blocks() as demo:
162
  gr.Markdown("# Basic Agent Evaluation Runner")
163
  gr.Markdown("""
 
175
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
176
 
177
  if __name__ == "__main__":
178
+ print("\ud83d\udd27 App starting...")
179
  demo.launch(debug=True, share=False)
180
 
181