luke9705 commited on
Commit
5215882
·
1 Parent(s): 7b2bd1f

Refactor check_format function to disallow nested lists and update agent model to 'Qwen/Qwen3-32B'

Browse files
Files changed (2) hide show
  1. app.py +14 -7
  2. system_prompt.txt +8 -10
app.py CHANGED
@@ -49,19 +49,20 @@ def load_file(path: str) -> list | dict:
49
 
50
  def check_format(answer: str | list, *args, **kwargs) -> list:
51
  """Check if the answer is a list and not a nested list."""
52
- print("Checking format of the answer:", answer)
53
  if isinstance(answer, list):
54
  for item in answer:
55
  if isinstance(item, list):
56
- print("list detected")
57
  raise TypeError("Nested lists are not allowed in the final answer.")
 
 
58
  elif isinstance(answer, str):
59
  return [answer]
60
  elif isinstance(answer, dict):
61
- raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format. Error: {e}")
62
 
63
 
64
-
65
  ## tools definition
66
  @tool
67
  def download_images(image_urls: str) -> list:
@@ -156,7 +157,7 @@ def generate_audio(prompt: str) -> object:
156
  ## agent definition
157
  class Agent:
158
  def __init__(self, ):
159
- client = HfApiModel("google/gemma-3-27b-it", provider="nebius", api_key=os.getenv("NEBIUS_API_KEY"))
160
  self.agent = CodeAgent(
161
  model=client,
162
  tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
@@ -188,15 +189,21 @@ def respond(message: str, history : dict, web_search: bool = False):
188
  if not message.get("files") and not web_search: # no files uploaded
189
  print("No files received.")
190
  message = agent(text + "\nADDITIONAL CONTRAINT: Don't use web search", conversation_history=history) # conversation_history is a dict with the history of the conversation
191
- elif not message.get("files") and web_search==True: # no files uploaded
192
  print("No files received + web search enabled.")
193
  message = agent(text, conversation_history=history)
194
  else:
195
  files = message.get("files", [])
196
  print(f"files received: {files}")
197
- if is_image_extension(files[0]):
198
  image = load_file(files[0]) # assuming only one file is uploaded at a time (gradio default behavior)
 
 
 
199
  message = agent(text, images=image, conversation_history=history)
 
 
 
200
  else:
201
  file = load_file(files[0])
202
  message = agent(text, files=file, conversation_history=history)
 
49
 
50
  def check_format(answer: str | list, *args, **kwargs) -> list:
51
  """Check if the answer is a list and not a nested list."""
52
+ #print("Checking format of the answer:", answer)
53
  if isinstance(answer, list):
54
  for item in answer:
55
  if isinstance(item, list):
56
+ print("Nested list detected")
57
  raise TypeError("Nested lists are not allowed in the final answer.")
58
+ print("Final answer is a list:", answer)
59
+ return answer
60
  elif isinstance(answer, str):
61
  return [answer]
62
  elif isinstance(answer, dict):
63
+ raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format.")
64
 
65
 
 
66
  ## tools definition
67
  @tool
68
  def download_images(image_urls: str) -> list:
 
157
  ## agent definition
158
  class Agent:
159
  def __init__(self, ):
160
+ client = HfApiModel("Qwen/Qwen3-32B", provider="nebius", api_key=os.getenv("NEBIUS_API_KEY"))
161
  self.agent = CodeAgent(
162
  model=client,
163
  tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
 
189
  if not message.get("files") and not web_search: # no files uploaded
190
  print("No files received.")
191
  message = agent(text + "\nADDITIONAL CONTRAINT: Don't use web search", conversation_history=history) # conversation_history is a dict with the history of the conversation
192
+ elif not message.get("files") and web_search: # no files uploaded
193
  print("No files received + web search enabled.")
194
  message = agent(text, conversation_history=history)
195
  else:
196
  files = message.get("files", [])
197
  print(f"files received: {files}")
198
+ if is_image_extension(files[0]) and not web_search:
199
  image = load_file(files[0]) # assuming only one file is uploaded at a time (gradio default behavior)
200
+ message = agent(text + "\nADDITIONAL CONTRAINT: Don't use web search", images=image, conversation_history=history)
201
+ elif is_image_extension(files[0]) and web_search:
202
+ image = load_file(files[0])
203
  message = agent(text, images=image, conversation_history=history)
204
+ elif not web_search:
205
+ file = load_file(files[0])
206
+ message = agent(text + "\nADDITIONAL CONTRAINT: Don't use web search", files=file, conversation_history=history)
207
  else:
208
  file = load_file(files[0])
209
  message = agent(text, files=file, conversation_history=history)
system_prompt.txt CHANGED
@@ -312,19 +312,17 @@ Additional domain-specific behaviors:
312
  - This includes flagging problematic language, stereotyped depictions, or systemic imbalance in representation.
313
  - Where applicable, the assistant must provide annotated examples and suggest possible reframing or source guidance (e.g., UNESCO media diversity principles).
314
  ----
315
-
316
 
317
  It is MANDATORY to use these rules for the 'final_answer' tool:
318
  1. Always return a Python list. Do not return a dictionary or any other type.
319
- 2. The first element of the list must be a Gradio component (for example, gr.Image(…), gr.File(…), etc.).
320
- 3. Any text or explanation must come after the component, as string elements in the same list.
321
- 4. If there is no component to return, return a list whose only element is the text.
322
- 5. Examples of valid returns:
323
- [gr.Image(value=“output.png”), “Here is the chart.”]
324
- [gr.File(value=“report.pdf”), “Download the report.”]
325
- 7. Any deviation (returning a dict, tuple, raw PIL image, etc.) is invalid.
326
- 8. Nested list are forbidden.
327
- 9. Always put generated images in the list, do NOT create external link.
328
 
329
  Now Begin!
330
 
 
312
  - This includes flagging problematic language, stereotyped depictions, or systemic imbalance in representation.
313
  - Where applicable, the assistant must provide annotated examples and suggest possible reframing or source guidance (e.g., UNESCO media diversity principles).
314
  ----
 
315
 
316
  It is MANDATORY to use these rules for the 'final_answer' tool:
317
  1. Always return a Python list. Do not return a dictionary or any other type.
318
+ 2. Any text or explanation must come after the component, as string elements in the same list.
319
+ 3. If there is no component to return, return a list whose only element is the text.
320
+ 4. Examples of valid returns:
321
+ - [image, “Here is the first image”, image2, "Here is the second image"]
322
+ - [file, “Download the report.”]
323
+ 5. Any deviation (returning a dict, tuple, raw PIL image, etc.) is invalid.
324
+ 6. Nested list are forbidden.
325
+ 7. Always put generated images in the list, do NOT create external link.
 
326
 
327
  Now Begin!
328