File size: 1,288 Bytes
849178c
 
36b445a
849178c
36b445a
 
 
849178c
 
 
 
36b445a
 
849178c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cb7519
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import gradio as gr
from gradio import ChatMessage
from transformers import load_tool, ReactCodeAgent, HfApiEngine  # type: ignore
from utils import stream_from_transformers_agent
import os 

HF_TOKEN = os.environ.get("HF_TOKEN")

# Import tool from Hub
image_generation_tool = load_tool("m-ric/text-to-image")

llm_engine = HfApiEngine(model="meta-llama/Meta-Llama-3-70B-Instruct", token=HF_TOKEN)

# Initialize the agent with both tools
agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)

def interact_with_agent(prompt, messages):
    messages.append(ChatMessage(role="user", content=prompt))
    yield messages
    for msg in stream_from_transformers_agent(agent, prompt):
        messages.append(msg)
        yield messages
    yield messages

with gr.Blocks() as demo:
    stored_message = gr.State([])
    chatbot = gr.Chatbot(label="Agent",
                         type="messages",
                         avatar_images=(None, "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png"))
    text_input = gr.Textbox(lines=1, label="Chat Message")
    text_input.submit(lambda s: (s, ""), [text_input], [stored_message, text_input]).then(interact_with_agent, [stored_message, chatbot], [chatbot])

if __name__ == "__main__":
    demo.launch()