You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting Familiar\n",
"\n",
"https://www.gradio.app/guides/agents-and-tool-usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"\n",
"with gr.Blocks() as demo:\n",
" chatbot = gr.Chatbot(\n",
" type=\"messages\",\n",
" value=[\n",
" gr.ChatMessage(role=\"user\", content=\"What is the weather in San Francisco?\"),\n",
" gr.ChatMessage(\n",
" role=\"assistant\", content=\"I need to use the weather API tool?\", metadata={\"title\": \"🧠 Thinking\"}\n",
" ),\n",
" ],\n",
" )\n",
"\n",
"demo.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"from transformers import Tool, ReactCodeAgent # type: ignore\n",
"from transformers.agents import stream_to_gradio, HfApiEngine # type: ignore\n",
"\n",
"# Import tool from Hub\n",
"image_generation_tool = Tool.from_space(\n",
" space_id=\"black-forest-labs/FLUX.1-schnell\",\n",
" name=\"image_generator\",\n",
" description=\"Generates an image following your prompt. Returns a PIL Image.\",\n",
" api_name=\"/infer\",\n",
")\n",
"\n",
"llm_engine = HfApiEngine(\"Qwen/Qwen2.5-Coder-32B-Instruct\")\n",
"# Initialize the agent with both tools and engine\n",
"agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)\n",
"\n",
"def interact_with_agent(prompt, history):\n",
" messages = []\n",
" yield messages\n",
" for msg in stream_to_gradio(agent, prompt):\n",
" messages.append(asdict(msg))\n",
" yield messages\n",
" yield messages\n",
"\n",
"\n",
"demo = gr.ChatInterface(\n",
" interact_with_agent,\n",
" chatbot= gr.Chatbot(\n",
" label=\"Agent\",\n",
" type=\"messages\",\n",
" avatar_images=(\n",
" None,\n",
" \"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png\",\n",
" ),\n",
" ),\n",
" examples=[\n",
" [\"Generate an image of an astronaut riding an alligator\"],\n",
" [\"I am writing a children's book for my daughter. Can you help me with some illustrations?\"],\n",
" ],\n",
" type=\"messages\",\n",
")\n",
"\n",
"demo.launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "deepsearch-py311-2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}