Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import ( | |
load_tool, | |
ReactCodeAgent, | |
HfApiEngine, | |
stream_to_gradio, | |
) | |
from transformers.agents.search import DuckDuckGoSearchTool | |
# Import tool from Hub | |
image_generation_tool = load_tool("m-ric/text-to-image") | |
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct") | |
search_tool = DuckDuckGoSearchTool() | |
# Initialize the agent with the image generation tool | |
agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine, additional_authorized_imports=['requests', 'bs4'] , add_base_tools=True) | |
def interact_with_agent(task): | |
messages = [] | |
messages.append(gr.ChatMessage(role="user", content=task)) | |
yield messages | |
for msg in stream_to_gradio(agent, task): | |
messages.append(msg) | |
yield messages + [ | |
gr.ChatMessage(role="assistant", content="⏳ Task not finished yet!") | |
] | |
yield messages | |
with gr.Blocks() as demo: | |
text_input = gr.Textbox(lines=1, label="Chat Message", value="Make me a picture of the Taj Mahal.") | |
submit = gr.Button("Run illustrator agent!") | |
chatbot = gr.Chatbot( | |
label="Agent", | |
type="messages", | |
avatar_images=( | |
None, | |
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png", | |
), | |
) | |
submit.click(interact_with_agent, [text_input], [chatbot]) | |
if __name__ == "__main__": | |
demo.launch() |