Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio import ChatMessage
|
| 3 |
+
from transformers import load_tool, ReactCodeAgent, HfEngine
|
| 4 |
+
from utils import stream_from_transformers_agent
|
| 5 |
+
|
| 6 |
+
# Import tool from Hub
|
| 7 |
+
image_generation_tool = load_tool("m-ric/text-to-image")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
llm_engine = HfEngine("meta-llama/Meta-Llama-3-70B-Instruct")
|
| 11 |
+
# Initialize the agent with both tools
|
| 12 |
+
agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def interact_with_agent(prompt, messages):
|
| 16 |
+
messages.append(ChatMessage(role="user", content=prompt))
|
| 17 |
+
yield messages
|
| 18 |
+
for msg in stream_from_transformers_agent(agent, prompt):
|
| 19 |
+
messages.append(msg)
|
| 20 |
+
yield messages
|
| 21 |
+
yield messages
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
chatbot = gr.Chatbot(label="Agent",
|
| 26 |
+
msg_format="messages",
|
| 27 |
+
avatar_images=(None, "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png"))
|
| 28 |
+
text_input = gr.Textbox(lines=1, label="Chat Message")
|
| 29 |
+
text_input.submit(interact_with_agent, [text_input, chatbot], [chatbot])
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|