Firoj112 commited on
Commit
897173a
·
verified ·
1 Parent(s): 86fccb2

Update GRADIO_UI.py

Browse files
Files changed (1) hide show
  1. GRADIO_UI.py +7 -9
GRADIO_UI.py CHANGED
@@ -132,14 +132,13 @@ def pull_messages_from_step(step_log: MemoryStep):
132
  metadata={"title": "📝 Scraped Text"}
133
  )
134
 
135
- def stream_to_gradio(agent, task: str, api_key: str = None, reset_agent_memory: bool = False, additional_args: Optional[dict] = None):
136
  if not _is_package_available("gradio"):
137
  raise ModuleNotFoundError("Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`")
138
  import gradio as gr
139
 
140
- if api_key:
141
- os.environ["GOOGLE_API_KEY"] = api_key
142
- logger.debug("Using user-provided GOOGLE_API_KEY")
143
 
144
  total_input_tokens = 0
145
  total_output_tokens = 0
@@ -172,16 +171,16 @@ def stream_to_gradio(agent, task: str, api_key: str = None, reset_agent_memory:
172
  yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
173
 
174
  class GradioUI:
175
- def __init__(self, agent: MultiStepAgent):
176
  if not _is_package_available("gradio"):
177
  raise ModuleNotFoundError("Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`")
178
- self.agent = agent
179
 
180
  def interact_with_agent(self, prompt, api_key, messages):
181
  import gradio as gr
182
  messages.append(gr.ChatMessage(role="user", content=prompt))
183
  yield messages
184
- for msg in stream_to_gradio(self.agent, task=prompt, api_key=api_key, reset_agent_memory=False):
185
  messages.append(msg)
186
  yield messages
187
  yield messages
@@ -207,7 +206,6 @@ class GradioUI:
207
  """
208
 
209
  with gr.Blocks(fill_height=True, css=css) as demo:
210
- stored_messages = gr.State([])
211
  gr.Markdown("**Note**: Please provide your own Gemini API key below. The default key may run out of quota.")
212
  api_key_input = gr.Textbox(
213
  lines=1, label="Gemini API Key (optional)", placeholder="Enter your Gemini API key here", type="password"
@@ -217,7 +215,7 @@ class GradioUI:
217
  avatar_images=(None, "./icon.png"), scale=1, height=600
218
  )
219
  text_input = gr.Textbox(
220
- lines=1, label="Enter URL and request (e.g., navigate to https://en.wikipedia.org/wiki/Nvidia, scrape the infobox table, fill a search form)"
221
  )
222
  text_input.submit(self.interact_with_agent, [text_input, api_key_input, stored_messages], [chatbot])
223
 
 
132
  metadata={"title": "📝 Scraped Text"}
133
  )
134
 
135
+ def stream_to_gradio(initialize_agent, task: str, api_key: str = None, reset_agent_memory: bool = False, additional_args: Optional[dict] = None):
136
  if not _is_package_available("gradio"):
137
  raise ModuleNotFoundError("Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`")
138
  import gradio as gr
139
 
140
+ logger.debug(f"Received api_key: {'****' if api_key else 'None'}")
141
+ agent = initialize_agent(api_key)
 
142
 
143
  total_input_tokens = 0
144
  total_output_tokens = 0
 
171
  yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
172
 
173
  class GradioUI:
174
+ def __init__(self, initialize_agent):
175
  if not _is_package_available("gradio"):
176
  raise ModuleNotFoundError("Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`")
177
+ self.initialize_agent = initialize_agent
178
 
179
  def interact_with_agent(self, prompt, api_key, messages):
180
  import gradio as gr
181
  messages.append(gr.ChatMessage(role="user", content=prompt))
182
  yield messages
183
+ for msg in stream_to_gradio(self.initialize_agent, task=prompt, api_key=api_key, reset_agent_memory=False):
184
  messages.append(msg)
185
  yield messages
186
  yield messages
 
206
  """
207
 
208
  with gr.Blocks(fill_height=True, css=css) as demo:
 
209
  gr.Markdown("**Note**: Please provide your own Gemini API key below. The default key may run out of quota.")
210
  api_key_input = gr.Textbox(
211
  lines=1, label="Gemini API Key (optional)", placeholder="Enter your Gemini API key here", type="password"
 
215
  avatar_images=(None, "./icon.png"), scale=1, height=600
216
  )
217
  text_input = gr.Textbox(
218
+ lines=1, label="Enter URL and request (e.g., navigate to https://en.wikipedia.org/wiki/Nvidia, scrape the infobox table)"
219
  )
220
  text_input.submit(self.interact_with_agent, [text_input, api_key_input, stored_messages], [chatbot])
221