dlaima commited on
Commit
bd35e0b
·
verified ·
1 Parent(s): 3a4977e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -67,36 +67,30 @@ with gr.Blocks() as demo:
67
  gr.Markdown("Powered by **smolagents**")
68
 
69
  with gr.Column(scale=3):
70
- output = gr.Chatbot(label="Agent", type="messages" ) # default format (list of tuples)
71
-
72
- #def run_query(prompt, file):
73
- #global agent_instance
74
- #agent_instance = build_agent(file_path=file)
75
- #result = agent_instance.run(prompt)
76
-
77
- # Ensure result is string for chatbot format
78
- #if isinstance(result, dict):
79
- # Convert dict to readable string if agent returns JSON-like structure
80
- # result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
81
-
82
- #return [("User", prompt), ("Agent", result)]
83
 
84
  def run_query(prompt, file):
85
  global agent_instance
86
  agent_instance = build_agent(file_path=file)
87
  result = agent_instance.run(prompt)
88
-
89
- # If result is a dict, convert to readable string (optional)
90
  if isinstance(result, dict):
91
  result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
92
-
93
- # Return the conversation in the new format for gr.Chatbot (type="messages")
 
 
 
 
 
 
 
94
  return [
95
  {"role": "user", "content": prompt},
96
  {"role": "assistant", "content": result}
97
  ]
98
 
99
-
100
  submit.click(fn=run_query, inputs=[prompt, guest_file], outputs=output)
101
 
102
  if __name__ == "__main__":
 
67
  gr.Markdown("Powered by **smolagents**")
68
 
69
  with gr.Column(scale=3):
70
+ output = gr.Chatbot(label="Agent", type="messages")
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  def run_query(prompt, file):
73
  global agent_instance
74
  agent_instance = build_agent(file_path=file)
75
  result = agent_instance.run(prompt)
76
+
77
+ # Handle different result types to convert to string for chatbot output
78
  if isinstance(result, dict):
79
  result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
80
+ elif isinstance(result, list):
81
+ if all(isinstance(item, dict) and "name" in item and "starter" in item for item in result):
82
+ result = "\n\n".join(f"{item['name']}: {item['starter']}" for item in result)
83
+ else:
84
+ result = str(result)
85
+ else:
86
+ result = str(result)
87
+
88
+ # Return as list of message dicts for Gradio chatbot type="messages"
89
  return [
90
  {"role": "user", "content": prompt},
91
  {"role": "assistant", "content": result}
92
  ]
93
 
 
94
  submit.click(fn=run_query, inputs=[prompt, guest_file], outputs=output)
95
 
96
  if __name__ == "__main__":