ahmer64 commited on
Commit
86bfc1d
·
verified ·
1 Parent(s): 7ca8465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -34
app.py CHANGED
@@ -1,57 +1,72 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
-
7
- # (Keep Constants as is)
8
- # --- Constants ---
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
-
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
14
  import os
15
 
16
  class BasicAgent:
17
  def __init__(self):
18
- print("Final Agent initialized.")
19
-
20
- hf_token = os.getenv("HF_TOKEN")
21
- print("TOKEN EXISTS:", hf_token is not None)
22
-
23
- self.model = InferenceClientModel(
24
- model_id="HuggingFaceH4/zephyr-7b-beta",
25
- provider="hf-inference",
26
- token=hf_token
27
- )
28
 
29
- self.agent = CodeAgent(
30
- tools=[DuckDuckGoSearchTool()],
31
- model=self.model,
32
- add_base_tools=True,
33
- max_steps=6
34
- )
 
 
 
 
 
35
 
36
  def clean(self, ans):
37
  ans = str(ans).strip()
38
  for x in ["FINAL ANSWER:", "Final Answer:", "Answer:"]:
39
  ans = ans.replace(x, "")
 
40
  return ans.split("\n")[0].strip()
41
 
42
  def __call__(self, question: str) -> str:
 
 
 
 
43
  prompt = f"""
44
- Return ONLY the final answer.
45
- No explanation. No extra words.
 
 
46
 
47
  Question:
48
  {question}
 
 
 
49
  """
 
50
  try:
51
- ans = self.agent.run(prompt)
52
- return self.clean(ans)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  except Exception as e:
54
- print("ERROR:", e)
55
  return "unknown"
56
 
57
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
1
+ from openai import OpenAI
2
+ from ddgs import DDGS
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
 
5
  class BasicAgent:
6
  def __init__(self):
7
+ print("OpenAI Agent initialized.")
8
+ self.client = OpenAI(api_key=os.getenv("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"))
 
 
 
 
 
 
 
 
9
 
10
+ def web_search(self, query, max_results=5):
11
+ try:
12
+ with DDGS() as ddgs:
13
+ results = list(ddgs.text(query, max_results=max_results))
14
+ return "\n".join([
15
+ f"{r.get('title')}: {r.get('body')}"
16
+ for r in results
17
+ ])
18
+ except Exception as e:
19
+ print("SEARCH ERROR:", e)
20
+ return ""
21
 
22
  def clean(self, ans):
23
  ans = str(ans).strip()
24
  for x in ["FINAL ANSWER:", "Final Answer:", "Answer:"]:
25
  ans = ans.replace(x, "")
26
+ ans = ans.strip()
27
  return ans.split("\n")[0].strip()
28
 
29
  def __call__(self, question: str) -> str:
30
+ print("Question:", question[:100])
31
+
32
+ context = self.web_search(question)
33
+
34
  prompt = f"""
35
+ Return ONLY the exact final answer.
36
+ No explanation.
37
+ No extra words.
38
+ Do not write FINAL ANSWER.
39
 
40
  Question:
41
  {question}
42
+
43
+ Search context:
44
+ {context}
45
  """
46
+
47
  try:
48
+ response = self.client.chat.completions.create(
49
+ model="gpt-4o-mini",
50
+ messages=[
51
+ {
52
+ "role": "system",
53
+ "content": "You solve exact-match benchmark questions. Return only the final answer."
54
+ },
55
+ {
56
+ "role": "user",
57
+ "content": prompt
58
+ }
59
+ ],
60
+ temperature=0
61
+ )
62
+
63
+ answer = response.choices[0].message.content
64
+ final = self.clean(answer)
65
+ print("Submitted:", final)
66
+ return final
67
+
68
  except Exception as e:
69
+ print("AGENT ERROR:", e)
70
  return "unknown"
71
 
72
  def run_and_submit_all( profile: gr.OAuthProfile | None):