darrenphodgson76 commited on
Commit
65adf02
·
verified ·
1 Parent(s): 436a549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -4,6 +4,7 @@ from openai import OpenAI
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
 
7
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
8
 
9
  # --- Constants ---
@@ -14,25 +15,9 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
14
  if not openai_api_key:
15
  raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets or env!")
16
  openai.api_key = openai_api_key
17
- client = OpenAI() # official OpenAI client
18
- OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
19
-
20
- # --- Model Wrapper with __call__ ---
21
- class OpenAIModelWrapper:
22
- """
23
- Wraps the new OpenAI client.responses.create API so that
24
- CodeAgent can call it directly.
25
- """
26
- def __init__(self, model_id: str, client: OpenAI):
27
- self.model_id = model_id
28
- self.client = client
29
 
30
- def __call__(self, prompt: str) -> str:
31
- resp = self.client.responses.create(
32
- model=self.model_id,
33
- input=prompt
34
- )
35
- return resp.output_text
36
 
37
  # --- Tool Definitions ---
38
 
@@ -67,7 +52,7 @@ search_tool = DuckDuckGoSearchTool()
67
  wiki_tool = wikipedia_search
68
  summarize_tool = summarize_query
69
 
70
- # --- ReACT + Scratchpad + Auto-Retry Prompt ---
71
 
72
  instruction_prompt = """
73
  You are a ReACT agent with three tools:
@@ -93,16 +78,31 @@ Rules:
93
  - Strings: no filler words.
94
  """
95
 
96
- # --- Build the CodeAgent with the callable wrapper ---
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
99
 
 
 
100
  smart_agent = CodeAgent(
101
  tools=[search_tool, wiki_tool, summarize_tool],
102
  model=llm_wrapper
103
  )
104
 
105
- # --- Wrap in BasicAgent for Gradio ---
106
 
107
  class BasicAgent:
108
  def __init__(self):
@@ -116,19 +116,19 @@ class BasicAgent:
116
  except Exception as e:
117
  return f"AGENT ERROR: {e}"
118
 
119
- # --- Gradio & Submission Logic ---
120
 
121
  def run_and_submit_all(profile: gr.OAuthProfile | None):
122
  if not profile:
123
  return "Please log in to Hugging Face.", None
124
- username = profile.username
125
- space_id = os.getenv("SPACE_ID", "")
126
- agent = BasicAgent()
127
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
128
 
129
  # 1. Fetch questions
130
  try:
131
- resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
132
  resp.raise_for_status()
133
  questions = resp.json() or []
134
  except Exception as e:
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
+
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
9
 
10
  # --- Constants ---
 
15
  if not openai_api_key:
16
  raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets or env!")
17
  openai.api_key = openai_api_key
18
+ client = OpenAI()
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
 
 
 
 
 
21
 
22
  # --- Tool Definitions ---
23
 
 
52
  wiki_tool = wikipedia_search
53
  summarize_tool = summarize_query
54
 
55
+ # --- ReACT + Scratchpad + Auto-Retry Instruction Prompt ---
56
 
57
  instruction_prompt = """
58
  You are a ReACT agent with three tools:
 
78
  - Strings: no filler words.
79
  """
80
 
81
+ # --- Model wrapper to satisfy CodeAgent's expected interface ---
82
+
83
+ class OpenAIModelWrapper:
84
+ def __init__(self, model_id: str, client: OpenAI):
85
+ self.model_id = model_id
86
+ self.client = client
87
+
88
+ def __call__(self, prompt: str, **kwargs) -> str:
89
+ # ignore kwargs like stop_sequences, temperature, etc.
90
+ resp = self.client.responses.create(
91
+ model=self.model_id,
92
+ input=prompt
93
+ )
94
+ return resp.output_text
95
 
96
  llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
97
 
98
+ # --- Build the CodeAgent ---
99
+
100
  smart_agent = CodeAgent(
101
  tools=[search_tool, wiki_tool, summarize_tool],
102
  model=llm_wrapper
103
  )
104
 
105
+ # --- BasicAgent wrapper for Gradio ---
106
 
107
  class BasicAgent:
108
  def __init__(self):
 
116
  except Exception as e:
117
  return f"AGENT ERROR: {e}"
118
 
119
+ # --- Gradio / Submission Logic ---
120
 
121
  def run_and_submit_all(profile: gr.OAuthProfile | None):
122
  if not profile:
123
  return "Please log in to Hugging Face.", None
124
+ username = profile.username
125
+ space_id = os.getenv("SPACE_ID", "")
126
+ agent = BasicAgent()
127
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
128
 
129
  # 1. Fetch questions
130
  try:
131
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
132
  resp.raise_for_status()
133
  questions = resp.json() or []
134
  except Exception as e: