pmeyhoefer commited on
Commit
6a7f9e3
ยท
verified ยท
1 Parent(s): e2b28ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -27
app.py CHANGED
@@ -4,19 +4,15 @@ import traceback
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
- from openai import OpenAI
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
9
- from smolagents.models import OpenAIServerModel
10
 
11
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
12
  logger = logging.getLogger(__name__)
13
 
14
  SUBMISSION_URL = "https://agents-course-unit4-scoring.hf.space"
15
- GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
16
- if not GITHUB_TOKEN:
17
- raise ValueError("CRITICAL: GITHUB_TOKEN environment variable not set.")
18
- GITHUB_ENDPOINT = "https://models.github.ai/inference"
19
- MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-4o-mini")
20
 
21
  try:
22
  search_tool_instance = DuckDuckGoSearchTool()
@@ -29,7 +25,6 @@ except Exception as e:
29
  def web_search(query: str) -> str:
30
  """
31
  Performs a web search using DuckDuckGo. Use this for general questions or current info.
32
-
33
  Args:
34
  query (str): The search query string.
35
  """
@@ -49,7 +44,6 @@ def web_search(query: str) -> str:
49
  def wikipedia_lookup(page_title: str) -> str:
50
  """
51
  Fetches the summary intro text of an English Wikipedia page. Use exact titles.
52
-
53
  Args:
54
  page_title (str): The exact title of the Wikipedia page (e.g., 'Albert Einstein').
55
  """
@@ -72,8 +66,8 @@ def wikipedia_lookup(page_title: str) -> str:
72
  logger.warning(f"Wikipedia page '{title}' is disambiguation.")
73
  return f"Wikipedia Error: '{title}' is a disambiguation page. Try a more specific title."
74
  else:
75
- logger.warning(f"Wikipedia page '{title}' found but has no summary.")
76
- return f"Wikipedia Error: Page '{title}' found but has no summary."
77
  except requests.exceptions.HTTPError as e:
78
  if e.response.status_code == 404:
79
  logger.warning(f"Wikipedia page not found: {page_safe}")
@@ -86,11 +80,9 @@ def wikipedia_lookup(page_title: str) -> str:
86
  return f"Wikipedia Error: Unexpected error: {e}"
87
 
88
  REACT_INSTRUCTION_PROMPT = """You are a helpful assistant using tools to answer questions.
89
-
90
  Available Tools:
91
  - web_search(query: str): Searches the web. Use for general info or current events.
92
  - wikipedia_lookup(page_title: str): Looks up a specific English Wikipedia page. Use exact titles (e.g., 'Berlin').
93
-
94
  Follow these steps:
95
  1. Thought: Plan which tool to use and why.
96
  2. Action: Call ONE tool (e.g., web_search(query="...") or wikipedia_lookup(page_title="...")).
@@ -99,28 +91,58 @@ Follow these steps:
99
  5. Repeat Action/Observation/Thought until answered or determined impossible.
100
  6. Thought: Summarize findings based ONLY on observations.
101
  7. Final Answer: Provide the answer starting exactly with "FINAL ANSWER: " using the required format (number, short string, or comma-separated list).
102
-
103
  Formatting Rules for FINAL ANSWER:
104
  - Numbers: Just the number (e.g., `42`).
105
  - Strings: Minimal words, no articles. Digits as words (e.g., `seven`).
106
  - Lists: Comma-separated (e.g., `paris,london,three`).
107
-
108
  Let's begin!
109
  """
110
 
111
- logger.info(f"Initializing LLM connection: {MODEL_ID} @ {GITHUB_ENDPOINT}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  try:
113
- logger.info("Attempting to configure OpenAIServerModel with 'api_base' (and no request_timeout)...")
114
- llm_model = OpenAIServerModel(
115
  model_id=MODEL_ID,
116
- api_key=GITHUB_TOKEN,
117
- api_base=GITHUB_ENDPOINT
118
- # Removed request_timeout=60
119
  )
120
- logger.info("LLM connection configured using 'api_base'.")
121
  except Exception as e:
122
- logger.exception("CRITICAL: Failed to configure OpenAIServerModel (tried with api_base)")
123
- raise RuntimeError(f"Could not configure SmolAgents model using api_base: {e}") from e
124
 
125
  logger.info("Initializing CodeAgent...")
126
  try:
@@ -175,7 +197,7 @@ def evaluate_and_submit():
175
  logger.info(f"Processing Q {i+1}/{len(questions)} (ID: {task_id})...")
176
  raw_agent_output = run_agent_on_question(question_text)
177
  final_answer = "AGENT_ERROR: No 'FINAL ANSWER:' marker."
178
- marker = "FINAL ANSWER:";
179
  if marker in raw_agent_output: final_answer = raw_agent_output.split(marker, 1)[1].strip()
180
  elif "AGENT_ERROR:" in raw_agent_output: final_answer = raw_agent_output
181
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer, "Full Output": raw_agent_output})
@@ -202,7 +224,7 @@ def evaluate_and_submit():
202
 
203
  logger.info("Setting up Gradio interface...")
204
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
205
- gr.Markdown("# ๐Ÿš€ Agent Evaluation Runner ๐Ÿš€\nEnsure `GITHUB_TOKEN` secret is set. Click Run to start.")
206
  run_button = gr.Button("โ–ถ๏ธ Run Evaluation & Submit All Answers", variant="primary")
207
  status_textbox = gr.Textbox(label="๐Ÿ“Š Status", lines=4, interactive=False)
208
  results_df_display = gr.DataFrame(label="๐Ÿ“‹ Detailed Log", headers=["Task ID", "Question", "Submitted Answer", "Full Output"], wrap=True, column_widths=["10%", "25%", "20%", "45%"])
@@ -213,4 +235,3 @@ if __name__ == "__main__":
213
  logger.info("Launching Gradio application...")
214
  demo.launch(debug=True, share=False)
215
  logger.info("Gradio application launched.")
216
-
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
 
7
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
8
+ from smolagents.models import BaseModel
9
 
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
11
  logger = logging.getLogger(__name__)
12
 
13
  SUBMISSION_URL = "https://agents-course-unit4-scoring.hf.space"
14
+ POLLINATIONS_API = "https://text.pollinations.ai/"
15
+ MODEL_ID = os.getenv("MODEL_ID", "openai-large")
 
 
 
16
 
17
  try:
18
  search_tool_instance = DuckDuckGoSearchTool()
 
25
  def web_search(query: str) -> str:
26
  """
27
  Performs a web search using DuckDuckGo. Use this for general questions or current info.
 
28
  Args:
29
  query (str): The search query string.
30
  """
 
44
  def wikipedia_lookup(page_title: str) -> str:
45
  """
46
  Fetches the summary intro text of an English Wikipedia page. Use exact titles.
 
47
  Args:
48
  page_title (str): The exact title of the Wikipedia page (e.g., 'Albert Einstein').
49
  """
 
66
  logger.warning(f"Wikipedia page '{title}' is disambiguation.")
67
  return f"Wikipedia Error: '{title}' is a disambiguation page. Try a more specific title."
68
  else:
69
+ logger.warning(f"Wikipedia page '{title}' found but has no summary.")
70
+ return f"Wikipedia Error: Page '{title}' found but has no summary."
71
  except requests.exceptions.HTTPError as e:
72
  if e.response.status_code == 404:
73
  logger.warning(f"Wikipedia page not found: {page_safe}")
 
80
  return f"Wikipedia Error: Unexpected error: {e}"
81
 
82
  REACT_INSTRUCTION_PROMPT = """You are a helpful assistant using tools to answer questions.
 
83
  Available Tools:
84
  - web_search(query: str): Searches the web. Use for general info or current events.
85
  - wikipedia_lookup(page_title: str): Looks up a specific English Wikipedia page. Use exact titles (e.g., 'Berlin').
 
86
  Follow these steps:
87
  1. Thought: Plan which tool to use and why.
88
  2. Action: Call ONE tool (e.g., web_search(query="...") or wikipedia_lookup(page_title="...")).
 
91
  5. Repeat Action/Observation/Thought until answered or determined impossible.
92
  6. Thought: Summarize findings based ONLY on observations.
93
  7. Final Answer: Provide the answer starting exactly with "FINAL ANSWER: " using the required format (number, short string, or comma-separated list).
 
94
  Formatting Rules for FINAL ANSWER:
95
  - Numbers: Just the number (e.g., `42`).
96
  - Strings: Minimal words, no articles. Digits as words (e.g., `seven`).
97
  - Lists: Comma-separated (e.g., `paris,london,three`).
 
98
  Let's begin!
99
  """
100
 
101
+ # Benutzerdefiniertes Modell fรผr Pollinations API
102
+ class PollinationsModel(BaseModel):
103
+ def __init__(self, model_id="openai-large", max_tokens=8196, seed=42):
104
+ self.model_id = model_id
105
+ self.max_tokens = max_tokens
106
+ self.seed = seed
107
+ self.api_url = POLLINATIONS_API
108
+ logger.info(f"Initialized PollinationsModel with model_id={model_id}")
109
+
110
+ def generate(self, prompt, **kwargs):
111
+ logger.info(f"Generating with PollinationsModel. Prompt length: {len(prompt)}")
112
+ try:
113
+ payload = {
114
+ "messages": [
115
+ {"role": "user", "content": prompt}
116
+ ],
117
+ "model": self.model_id,
118
+ "max_tokens": self.max_tokens,
119
+ "seed": self.seed,
120
+ "jsonMode": False,
121
+ "private": True
122
+ }
123
+ response = requests.post(self.api_url, json=payload, timeout=120)
124
+ response.raise_for_status()
125
+ result = response.json()
126
+ if "content" in result:
127
+ return result["content"]
128
+ else:
129
+ logger.error(f"Unexpected response structure: {result}")
130
+ return "Error: Unexpected API response format"
131
+ except Exception as e:
132
+ logger.exception(f"PollinationsModel generate failed: {e}")
133
+ return f"Error generating response: {str(e)}"
134
+
135
+ logger.info(f"Initializing Pollinations LLM connection: {MODEL_ID}")
136
  try:
137
+ llm_model = PollinationsModel(
 
138
  model_id=MODEL_ID,
139
+ max_tokens=8196,
140
+ seed=42
 
141
  )
142
+ logger.info("LLM connection configured using Pollinations API.")
143
  except Exception as e:
144
+ logger.exception("CRITICAL: Failed to configure PollinationsModel")
145
+ raise RuntimeError(f"Could not configure PollinationsModel: {e}") from e
146
 
147
  logger.info("Initializing CodeAgent...")
148
  try:
 
197
  logger.info(f"Processing Q {i+1}/{len(questions)} (ID: {task_id})...")
198
  raw_agent_output = run_agent_on_question(question_text)
199
  final_answer = "AGENT_ERROR: No 'FINAL ANSWER:' marker."
200
+ marker = "FINAL ANSWER:"
201
  if marker in raw_agent_output: final_answer = raw_agent_output.split(marker, 1)[1].strip()
202
  elif "AGENT_ERROR:" in raw_agent_output: final_answer = raw_agent_output
203
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer, "Full Output": raw_agent_output})
 
224
 
225
  logger.info("Setting up Gradio interface...")
226
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
227
+ gr.Markdown("# ๐Ÿš€ Agent Evaluation Runner ๐Ÿš€\nEnsure all configurations are set. Click Run to start.")
228
  run_button = gr.Button("โ–ถ๏ธ Run Evaluation & Submit All Answers", variant="primary")
229
  status_textbox = gr.Textbox(label="๐Ÿ“Š Status", lines=4, interactive=False)
230
  results_df_display = gr.DataFrame(label="๐Ÿ“‹ Detailed Log", headers=["Task ID", "Question", "Submitted Answer", "Full Output"], wrap=True, column_widths=["10%", "25%", "20%", "45%"])
 
235
  logger.info("Launching Gradio application...")
236
  demo.launch(debug=True, share=False)
237
  logger.info("Gradio application launched.")