myopera9 commited on
Commit
6405660
·
verified ·
1 Parent(s): a3cd3ac

Create run.py

Browse files
Files changed (1) hide show
  1. run.py +46 -0
run.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from dataclasses import asdict
3
+ from transformers import Tool, ReactCodeAgent # type: ignore
4
+ from transformers.agents import stream_to_gradio, HfApiEngine # type: ignore
5
+
6
+ # Import tool from Hub
7
+ image_generation_tool = Tool.from_space( # type: ignore
8
+ space_id="black-forest-labs/FLUX.1-schnell",
9
+ name="image_generator",
10
+ description="Generates an image following your prompt. Returns a PIL Image.",
11
+ api_name="/infer",
12
+ )
13
+
14
+ llm_engine = HfApiEngine("Qwen/Qwen2.5-Coder-32B-Instruct")
15
+ # Initialize the agent with both tools and engine
16
+ agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)
17
+
18
+
19
+ def interact_with_agent(prompt, history):
20
+ messages = []
21
+ yield messages
22
+ for msg in stream_to_gradio(agent, prompt):
23
+ messages.append(asdict(msg)) # type: ignore
24
+ yield messages
25
+ yield messages
26
+
27
+
28
+ demo = gr.ChatInterface(
29
+ interact_with_agent,
30
+ chatbot= gr.Chatbot(
31
+ label="Agent",
32
+ type="messages",
33
+ avatar_images=(
34
+ None,
35
+ "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
36
+ ),
37
+ ),
38
+ examples=[
39
+ ["Generate an image of an astronaut riding an alligator"],
40
+ ["I am writing a children's book for my daughter. Can you help me with some illustrations?"],
41
+ ],
42
+ type="messages",
43
+ )
44
+
45
+ if __name__ == "__main__":
46
+ demo.launch()