File size: 2,175 Bytes
262a4e1
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: agent_chatbot"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio transformers>=4.47.0"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from dataclasses import asdict\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(  # type: ignore\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", "\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))  # type: ignore\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", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}