dlaima commited on
Commit
c3bd339
·
verified ·
1 Parent(s): 9edd6bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -8,7 +8,7 @@ import pandas as pd
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.
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.
@@ -17,16 +17,17 @@ If you're asked for a string, don’t use articles or abbreviations (e.g. for ci
17
 
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
- # Wrapper class for model output
21
  class GenerationResult:
22
- def __init__(self, content, token_usage=None):
23
  self.content = content
24
  self.token_usage = token_usage or {}
 
 
25
 
26
  # Gemini model wrapper
27
  class GeminiFlashModel:
28
  def __init__(self, model_id="gemini-1.5-flash", api_key=None):
29
- import google.generativeai as genai
30
  genai.configure(api_key=api_key or os.getenv("GEMINI_API_KEY"))
31
  self.model = genai.GenerativeModel(model_id)
32
  self.system_prompt = SYSTEM_PROMPT
@@ -46,11 +47,21 @@ class GeminiFlashModel:
46
 
47
  try:
48
  response = self.model.generate_content(prompt)
49
- return GenerationResult(content=response.text.strip(), token_usage={}) # Gemini doesn't expose usage
 
 
 
 
 
50
  except Exception as e:
51
- return GenerationResult(content=f"GENERATION ERROR: {e}", token_usage={})
52
-
53
- # Agent using Gemini
 
 
 
 
 
54
  class MyAgent:
55
  def __init__(self):
56
  self.model = GeminiFlashModel(model_id="gemini-1.5-flash")
@@ -59,6 +70,7 @@ class MyAgent:
59
  def __call__(self, question: str) -> str:
60
  return self.agent.run(question)
61
 
 
62
  def run_and_submit_all(profile: gr.OAuthProfile | None):
63
  space_id = os.getenv("SPACE_ID")
64
 
@@ -127,7 +139,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
127
  except Exception as e:
128
  return f"Submission failed: {e}", pd.DataFrame(results_log)
129
 
130
- # Gradio UI
131
  with gr.Blocks() as demo:
132
  gr.Markdown("# Basic Agent Evaluation Runner")
133
  gr.Markdown("""
 
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.
 
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
24
  self.token_usage = token_usage or {}
25
+ self.input_tokens = input_tokens
26
+ self.output_tokens = output_tokens
27
 
28
  # Gemini model wrapper
29
  class GeminiFlashModel:
30
  def __init__(self, model_id="gemini-1.5-flash", api_key=None):
 
31
  genai.configure(api_key=api_key or os.getenv("GEMINI_API_KEY"))
32
  self.model = genai.GenerativeModel(model_id)
33
  self.system_prompt = SYSTEM_PROMPT
 
47
 
48
  try:
49
  response = self.model.generate_content(prompt)
50
+ return GenerationResult(
51
+ content=response.text.strip(),
52
+ token_usage={},
53
+ input_tokens=0,
54
+ output_tokens=0
55
+ )
56
  except Exception as e:
57
+ return GenerationResult(
58
+ content=f"GENERATION ERROR: {e}",
59
+ token_usage={},
60
+ input_tokens=0,
61
+ output_tokens=0
62
+ )
63
+
64
+ # Agent wrapper
65
  class MyAgent:
66
  def __init__(self):
67
  self.model = GeminiFlashModel(model_id="gemini-1.5-flash")
 
70
  def __call__(self, question: str) -> str:
71
  return self.agent.run(question)
72
 
73
+ # Main evaluation function
74
  def run_and_submit_all(profile: gr.OAuthProfile | None):
75
  space_id = os.getenv("SPACE_ID")
76
 
 
139
  except Exception as e:
140
  return f"Submission failed: {e}", pd.DataFrame(results_log)
141
 
142
+ # Gradio UI setup
143
  with gr.Blocks() as demo:
144
  gr.Markdown("# Basic Agent Evaluation Runner")
145
  gr.Markdown("""