dlaima commited on
Commit
932b4d5
·
verified ·
1 Parent(s): edbeeac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -57
app.py CHANGED
@@ -4,10 +4,8 @@ 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
- from smolagents.model.base import ModelOutput # import ModelOutput if available
11
 
12
  # System prompt used by the agent
13
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
@@ -18,64 +16,36 @@ 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_id="gemini-1.5-flash", api_key=None):
24
- genai.configure(api_key=api_key or os.getenv("GEMINI_API_KEY"))
25
- self.model = genai.GenerativeModel(model_id)
 
 
 
26
  self.system_prompt = SYSTEM_PROMPT
27
 
28
- def generate(self, messages, stop_sequences=None, **kwargs):
29
- if not isinstance(messages, list) or not all(isinstance(m, dict) for m in messages):
30
- raise TypeError("Expected 'messages' to be a list of dicts")
31
-
32
  if not any(m.get("role") == "system" for m in messages):
33
  messages = [{"role": "system", "content": self.system_prompt}] + messages
34
 
35
- prompt = ""
36
- for m in messages:
37
- role = m["role"].capitalize()
38
- content = m["content"]
39
- prompt += f"{role}: {content}\n"
 
40
 
41
- try:
42
- response = self.model.generate_content(prompt)
43
- return ModelOutput(
44
- content=response.text.strip(),
45
- input_tokens=0,
46
- output_tokens=0,
47
- token_usage={}
48
- )
49
- except Exception as e:
50
- return ModelOutput(
51
- content=f"GENERATION ERROR: {e}",
52
- input_tokens=0,
53
- output_tokens=0,
54
- token_usage={}
55
- )
56
-
57
- # Agent wrapper
58
  class MyAgent:
59
  def __init__(self):
60
- self.model = GeminiFlashModel(model_id="gemini-1.5-flash")
61
  self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
62
 
63
  def __call__(self, question: str) -> str:
64
- result = self.agent.run(question)
65
- print(f"[DEBUG] Agent run result type: {type(result)}; value: {result}")
66
-
67
- # Return string content only
68
- if hasattr(result, "content"):
69
- return result.content
70
- elif isinstance(result, dict):
71
- return result.get("content", str(result))
72
- else:
73
- return str(result)
74
-
75
- # Main evaluation function
76
- def run_and_submit_all(profile: gr.OAuthProfile | None):
77
- print("Starting run_and_submit_all...")
78
 
 
79
  space_id = os.getenv("SPACE_ID")
80
 
81
  if profile:
@@ -108,20 +78,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
108
  question_text = item.get("question")
109
  if not task_id or question_text is None:
110
  continue
111
-
112
- print(f"Running agent on question: {question_text}")
113
  try:
114
  submitted_answer = agent(question_text)
115
- print(f"Agent answer: {submitted_answer} (type: {type(submitted_answer)})")
116
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
117
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
118
  except Exception as e:
119
- error_msg = f"AGENT ERROR: {e}"
120
- print(error_msg)
121
  results_log.append({
122
  "Task ID": task_id,
123
  "Question": question_text,
124
- "Submitted Answer": error_msg
125
  })
126
 
127
  if not answers_payload:
@@ -148,7 +113,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
148
  except Exception as e:
149
  return f"Submission failed: {e}", pd.DataFrame(results_log)
150
 
151
- # Gradio UI setup
152
  with gr.Blocks() as demo:
153
  gr.Markdown("# Basic Agent Evaluation Runner")
154
  gr.Markdown("""
@@ -173,5 +137,3 @@ if __name__ == "__main__":
173
 
174
 
175
 
176
-
177
-
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
+ from openai import OpenAI
 
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool
 
9
 
10
  # System prompt used by the agent
11
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
 
16
 
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
 
19
  class GeminiFlashModel:
20
+ def __init__(self, model_id="gemini-1.5-flash"):
21
+ self.client = OpenAI(
22
+ api_key=os.getenv("GEMINI_API_KEY"),
23
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
24
+ )
25
+ self.model_id = model_id
26
  self.system_prompt = SYSTEM_PROMPT
27
 
28
+ def generate(self, messages):
29
+ # Ensure system prompt is present
 
 
30
  if not any(m.get("role") == "system" for m in messages):
31
  messages = [{"role": "system", "content": self.system_prompt}] + messages
32
 
33
+ response = self.client.chat.completions.create(
34
+ model=self.model_id,
35
+ messages=messages
36
+ )
37
+ # Return the generated content string directly
38
+ return response.choices[0].message.content
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  class MyAgent:
41
  def __init__(self):
42
+ self.model = GeminiFlashModel()
43
  self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
44
 
45
  def __call__(self, question: str) -> str:
46
+ return self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
49
  space_id = os.getenv("SPACE_ID")
50
 
51
  if profile:
 
78
  question_text = item.get("question")
79
  if not task_id or question_text is None:
80
  continue
 
 
81
  try:
82
  submitted_answer = agent(question_text)
 
83
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
84
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
85
  except Exception as e:
 
 
86
  results_log.append({
87
  "Task ID": task_id,
88
  "Question": question_text,
89
+ "Submitted Answer": f"AGENT ERROR: {e}"
90
  })
91
 
92
  if not answers_payload:
 
113
  except Exception as e:
114
  return f"Submission failed: {e}", pd.DataFrame(results_log)
115
 
 
116
  with gr.Blocks() as demo:
117
  gr.Markdown("# Basic Agent Evaluation Runner")
118
  gr.Markdown("""
 
137
 
138
 
139