akhaliq HF staff commited on
Commit
4bd9767
·
verified ·
1 Parent(s): 146e4f5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from smolagents import CodeAgent, HfApiModel, Tool
3
+
4
+ # Define a simple tool (e.g., a text generator from a Hugging Face Space)
5
+ image_generation_tool = Tool.from_space(
6
+ "black-forest-labs/FLUX.1-schnell", # Example Space ID
7
+ name="image_generator",
8
+ description="Generate an image from a text prompt."
9
+ )
10
+
11
+ # Initialize the model (using a Hugging Face API model as an example)
12
+ model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct") # Replace with your preferred model
13
+
14
+ # Create the Smol Agent with the tool
15
+ agent = CodeAgent(tools=[image_generation_tool], model=model)
16
+
17
+ # Define the chatbot function
18
+ def chat_with_agent(user_input, history):
19
+ # History is a list of [user, assistant] pairs; we'll append to it
20
+ if history is None:
21
+ history = []
22
+
23
+ # Run the agent with the user's input
24
+ response = agent.run(user_input)
25
+
26
+ # Append the new interaction to the history
27
+ history.append([user_input, response])
28
+ return history, "" # Return updated history and clear input box
29
+
30
+ # Create the Gradio interface
31
+ with gr.Blocks(title="Smol Agent Chatbot") as demo:
32
+ gr.Markdown("# Chat with a Smol Agent")
33
+ gr.Markdown("Ask anything, and the agent will respond—maybe even generate an image!")
34
+
35
+ # Chatbot component to display conversation
36
+ chatbot = gr.Chatbot()
37
+
38
+ # Textbox for user input
39
+ user_input = gr.Textbox(placeholder="Type your message here...", label="Your Message")
40
+
41
+ # Submit button
42
+ submit_btn = gr.Button("Send")
43
+
44
+ # Connect the components: when the button is clicked, run the chat function
45
+ submit_btn.click(
46
+ fn=chat_with_agent,
47
+ inputs=[user_input, chatbot],
48
+ outputs=[chatbot, user_input]
49
+ )
50
+
51
+ # Launch the app
52
+ demo.launch()