diff --git a/.changeset/long-months-wave.md b/.changeset/long-months-wave.md new file mode 100644 index 0000000000000..adfcda1fe6066 --- /dev/null +++ b/.changeset/long-months-wave.md @@ -0,0 +1,6 @@ +--- +"@gradio/chatbot": minor +"gradio": minor +--- + +feat:Dataframe support in Chatbot diff --git a/demo/agent_chatbot/requirements.txt b/demo/agent_chatbot/requirements.txt index 646219d1bb0ae..9ed70ee033a18 100644 --- a/demo/agent_chatbot/requirements.txt +++ b/demo/agent_chatbot/requirements.txt @@ -1 +1 @@ -git+https://github.com/huggingface/transformers.git#egg=transformers[agents] \ No newline at end of file +transformers>=4.47.0 \ No newline at end of file diff --git a/demo/agent_chatbot/run.ipynb b/demo/agent_chatbot/run.ipynb index edfb71b31c84e..ccf9c2fe27edc 100644 --- a/demo/agent_chatbot/run.ipynb +++ b/demo/agent_chatbot/run.ipynb @@ -1 +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 git+https://github.com/huggingface/transformers.git#egg=transformers[agents]"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/agent_chatbot/utils.py"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from gradio import ChatMessage\n", "from transformers import load_tool, ReactCodeAgent, HfEngine # type: ignore\n", "from utils import stream_from_transformers_agent\n", "\n", "# Import tool from Hub\n", "image_generation_tool = load_tool(\"m-ric/text-to-image\")\n", "\n", "llm_engine = HfEngine(\"meta-llama/Meta-Llama-3-70B-Instruct\")\n", "# Initialize the agent with both tools\n", "agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)\n", "\n", "def interact_with_agent(prompt, messages):\n", " messages.append(ChatMessage(role=\"user\", content=prompt))\n", " yield messages\n", " for msg in stream_from_transformers_agent(agent, prompt):\n", " messages.append(msg)\n", " yield messages\n", " yield messages\n", "\n", "with gr.Blocks() as demo:\n", " stored_message = gr.State([])\n", " chatbot = gr.Chatbot(label=\"Agent\",\n", " type=\"messages\",\n", " avatar_images=(None, \"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png\"))\n", " text_input = gr.Textbox(lines=1, label=\"Chat Message\")\n", " text_input.submit(lambda s: (s, \"\"), [text_input], [stored_message, text_input]).then(interact_with_agent, [stored_message, chatbot], [chatbot])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file +{"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(\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))\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} \ No newline at end of file diff --git a/demo/agent_chatbot/run.py b/demo/agent_chatbot/run.py index c240d8d81460b..4e6cf36214536 100644 --- a/demo/agent_chatbot/run.py +++ b/demo/agent_chatbot/run.py @@ -1,30 +1,46 @@ import gradio as gr -from gradio import ChatMessage -from transformers import load_tool, ReactCodeAgent, HfEngine # type: ignore -from utils import stream_from_transformers_agent +from dataclasses import asdict +from transformers import Tool, ReactCodeAgent # type: ignore +from transformers.agents import stream_to_gradio, HfApiEngine # type: ignore # Import tool from Hub -image_generation_tool = load_tool("m-ric/text-to-image") +image_generation_tool = Tool.from_space( + space_id="black-forest-labs/FLUX.1-schnell", + name="image_generator", + description="Generates an image following your prompt. Returns a PIL Image.", + api_name="/infer", +) -llm_engine = HfEngine("meta-llama/Meta-Llama-3-70B-Instruct") -# Initialize the agent with both tools +llm_engine = HfApiEngine("Qwen/Qwen2.5-Coder-32B-Instruct") +# Initialize the agent with both tools and engine agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine) -def interact_with_agent(prompt, messages): - messages.append(ChatMessage(role="user", content=prompt)) + +def interact_with_agent(prompt, history): + messages = [] yield messages - for msg in stream_from_transformers_agent(agent, prompt): - messages.append(msg) + for msg in stream_to_gradio(agent, prompt): + messages.append(asdict(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]) + +demo = gr.ChatInterface( + interact_with_agent, + chatbot= gr.Chatbot( + label="Agent", + type="messages", + avatar_images=( + None, + "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png", + ), + ), + examples=[ + ["Generate an image of an astronaut riding an alligator"], + ["I am writing a children's book for my daughter. Can you help me with some illustrations?"], + ], + type="messages", +) if __name__ == "__main__": demo.launch() diff --git a/demo/agent_chatbot/utils.py b/demo/agent_chatbot/utils.py deleted file mode 100644 index 6d4d5493488b7..0000000000000 --- a/demo/agent_chatbot/utils.py +++ /dev/null @@ -1,64 +0,0 @@ -# type: ignore -from __future__ import annotations - -from gradio import ChatMessage -from transformers.agents import ReactCodeAgent, agent_types -from typing import Generator - -def pull_message(step_log: dict): - if step_log.get("rationale"): - yield ChatMessage( - role="assistant", content=step_log["rationale"] - ) - if step_log.get("tool_call"): - used_code = step_log["tool_call"]["tool_name"] == "code interpreter" - content = step_log["tool_call"]["tool_arguments"] - if used_code: - content = f"```py\n{content}\n```" - yield ChatMessage( - role="assistant", - metadata={"title": f"🛠️ Used tool {step_log['tool_call']['tool_name']}"}, - content=content, - ) - if step_log.get("observation"): - yield ChatMessage( - role="assistant", content=f"```\n{step_log['observation']}\n```" - ) - if step_log.get("error"): - yield ChatMessage( - role="assistant", - content=str(step_log["error"]), - metadata={"title": "💥 Error"}, - ) - -def stream_from_transformers_agent( - agent: ReactCodeAgent, prompt: str -) -> Generator[ChatMessage, None, ChatMessage | None]: - """Runs an agent with the given prompt and streams the messages from the agent as ChatMessages.""" - - class Output: - output: agent_types.AgentType | str = None - - step_log = None - for step_log in agent.run(prompt, stream=True): - if isinstance(step_log, dict): - for message in pull_message(step_log): - print("message", message) - yield message - - Output.output = step_log - if isinstance(Output.output, agent_types.AgentText): - yield ChatMessage( - role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```") # type: ignore - elif isinstance(Output.output, agent_types.AgentImage): - yield ChatMessage( - role="assistant", - content={"path": Output.output.to_string(), "mime_type": "image/png"}, # type: ignore - ) - elif isinstance(Output.output, agent_types.AgentAudio): - yield ChatMessage( - role="assistant", - content={"path": Output.output.to_string(), "mime_type": "audio/wav"}, # type: ignore - ) - else: - return ChatMessage(role="assistant", content=Output.output) diff --git a/demo/chatbot_core_components/run.ipynb b/demo/chatbot_core_components/run.ipynb index 9d07bae9c2a74..5abdfb0cbf1ee 100644 --- a/demo/chatbot_core_components/run.ipynb +++ b/demo/chatbot_core_components/run.ipynb @@ -1 +1 @@ -{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_core_components"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio plotly numpy pandas matplotlib "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/audio.wav https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/audio.wav\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/avatar.png\n", "!wget -q -O files/sample.txt https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/sample.txt\n", "!wget -q -O files/world.mp4 https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/world.mp4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["# type: ignore\n", "import gradio as gr\n", "import os\n", "import plotly.express as px\n", "import random\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "txt = \"\"\"\n", "Absolutely! The mycorrhizal network, often referred to as the \"Wood Wide Web,\" is a symbiotic association between fungi and the roots of most plant species. Here\u2019s a deeper dive into how it works and its implications:\n", "\n", "### How It Works\n", "\n", "1. **Symbiosis**: Mycorrhizal fungi attach to plant roots, extending far into the soil. The plant provides the fungi with carbohydrates produced via photosynthesis. In return, the fungi help the plant absorb water and essential nutrients like phosphorus and nitrogen from the soil.\n", "\n", "2. **Network Formation**: The fungal hyphae (thread-like structures) connect individual plants, creating an extensive underground network. This network can link many plants together, sometimes spanning entire forests.\n", "\n", "3. **Communication**: Trees and plants use this network to communicate and share resources. For example, a tree under attack by pests can send chemical signals through the mycorrhizal network to warn neighboring trees. These trees can then produce defensive chemicals to prepare for the impending threat.\n", "\n", "### Benefits and Functions\n", "\n", "1. **Resource Sharing**: The network allows for the redistribution of resources among plants. For instance, a large, established tree might share excess nutrients and water with smaller, younger trees, promoting overall forest health.\n", "\n", "2. **Defense Mechanism**: The ability to share information about pests and diseases enhances the resilience of plant communities. This early warning system helps plants activate their defenses before they are directly affected.\n", "\n", "3. **Support for Seedlings**: Young seedlings, which have limited root systems, benefit immensely from the mycorrhizal network. They receive nutrients and water from larger plants, increasing their chances of survival and growth.\n", "\n", "### Ecological Impact\n", "\n", "1. **Biodiversity**: The mycorrhizal network supports biodiversity by fostering a cooperative environment. Plants of different species can coexist and thrive because of the shared resources and information.\n", "\n", "2. **Forest Health**: The network enhances the overall health of forests. By enabling efficient nutrient cycling and supporting plant defenses, it contributes to the stability and longevity of forest ecosystems.\n", "\n", "3. **Climate Change Mitigation**: Healthy forests act as significant carbon sinks, absorbing carbon dioxide from the atmosphere. The mycorrhizal network plays a critical role in maintaining forest health and, consequently, in mitigating climate change.\n", "\n", "### Research and Discoveries\n", "\n", "1. **Suzanne Simard's Work**: Ecologist Suzanne Simard\u2019s research has been pivotal in uncovering the complexities of the mycorrhizal network. She demonstrated that trees of different species can share resources and that \"mother trees\" (large, older trees) play a crucial role in nurturing younger plants.\n", "\n", "2. **Implications for Conservation**: Understanding the mycorrhizal network has significant implications for conservation efforts. It highlights the importance of preserving not just individual trees but entire ecosystems, including the fungal networks that sustain them.\n", "\n", "### Practical Applications\n", "\n", "1. **Agriculture**: Farmers and horticulturists are exploring the use of mycorrhizal fungi to improve crop yields and soil health. By incorporating these fungi into agricultural practices, they can reduce the need for chemical fertilizers and enhance plant resilience.\n", "\n", "2. **Reforestation**: In reforestation projects, introducing mycorrhizal fungi can accelerate the recovery of degraded lands. The fungi help establish healthy plant communities, ensuring the success of newly planted trees.\n", "\n", "The \"Wood Wide Web\" exemplifies the intricate and often hidden connections that sustain life on Earth. It\u2019s a reminder of the profound interdependence within natural systems and the importance of preserving these delicate relationships.\n", "\"\"\"\n", "\n", "def random_plot():\n", " df = px.data.iris()\n", " fig = px.scatter(\n", " df,\n", " x=\"sepal_width\",\n", " y=\"sepal_length\",\n", " color=\"species\",\n", " size=\"petal_length\",\n", " hover_data=[\"petal_width\"],\n", " )\n", " return fig\n", "\n", "color_map = {\n", " \"harmful\": \"crimson\",\n", " \"neutral\": \"gray\",\n", " \"beneficial\": \"green\",\n", "}\n", "\n", "def html_src(harm_level):\n", " return f\"\"\"\n", "
\n", "
\n", " {harm_level}\n", "
\n", "
\n", "\"\"\"\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def random_bokeh_plot():\n", " from bokeh.models import ColumnDataSource, Whisker\n", " from bokeh.plotting import figure\n", " from bokeh.sampledata.autompg2 import autompg2 as df\n", " from bokeh.transform import factor_cmap, jitter\n", "\n", " classes = sorted(df[\"class\"].unique())\n", "\n", " p = figure(\n", " height=400,\n", " x_range=classes,\n", " background_fill_color=\"#efefef\",\n", " title=\"Car class vs HWY mpg with quintile ranges\",\n", " )\n", " p.xgrid.grid_line_color = None\n", "\n", " g = df.groupby(\"class\")\n", " upper = g.hwy.quantile(0.80)\n", " lower = g.hwy.quantile(0.20)\n", " source = ColumnDataSource(data=dict(base=classes, upper=upper, lower=lower))\n", "\n", " error = Whisker(\n", " base=\"base\",\n", " upper=\"upper\",\n", " lower=\"lower\",\n", " source=source,\n", " level=\"annotation\",\n", " line_width=2,\n", " )\n", " error.upper_head.size = 20\n", " error.lower_head.size = 20\n", " p.add_layout(error)\n", "\n", " p.circle(\n", " jitter(\"class\", 0.3, range=p.x_range),\n", " \"hwy\",\n", " source=df,\n", " alpha=0.5,\n", " size=13,\n", " line_color=\"white\",\n", " color=factor_cmap(\"class\", \"Light6\", classes),\n", " )\n", " return p\n", "\n", "def random_matplotlib_plot():\n", " import numpy as np\n", " import pandas as pd\n", " import matplotlib.pyplot as plt\n", "\n", " countries = [\"USA\", \"Canada\", \"Mexico\", \"UK\"]\n", " months = [\"January\", \"February\", \"March\", \"April\", \"May\"]\n", " m = months.index(\"January\")\n", " r = 3.2\n", " start_day = 30 * m\n", " final_day = 30 * (m + 1)\n", " x = np.arange(start_day, final_day + 1)\n", " pop_count = {\"USA\": 350, \"Canada\": 40, \"Mexico\": 300, \"UK\": 120}\n", " df = pd.DataFrame({\"day\": x})\n", " for country in countries:\n", " df[country] = x ** (r) * (pop_count[country] + 1)\n", "\n", " fig = plt.figure()\n", " plt.plot(df[\"day\"], df[countries].to_numpy())\n", " plt.title(\"Outbreak in \" + \"January\")\n", " plt.ylabel(\"Cases\")\n", " plt.xlabel(\"Days since Day 0\")\n", " plt.legend(countries)\n", " return fig\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append({\"role\": \"user\", \"content\": {\"path\": x}})\n", " if message[\"text\"] is not None:\n", " history.append({\"role\": \"user\", \"content\": message[\"text\"]})\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def bot(history, response_type):\n", " msg = {\"role\": \"assistant\", \"content\": \"\"}\n", " if response_type == \"plot\":\n", " content = gr.Plot(random_plot())\n", " elif response_type == \"bokeh_plot\":\n", " content = gr.Plot(random_bokeh_plot())\n", " elif response_type == \"matplotlib_plot\":\n", " content = gr.Plot(random_matplotlib_plot())\n", " elif response_type == \"gallery\":\n", " content = gr.Gallery(\n", " [os.path.join(\"files\", \"avatar.png\"), os.path.join(\"files\", \"avatar.png\")]\n", " )\n", " elif response_type == \"image\":\n", " content = gr.Image(os.path.join(\"files\", \"avatar.png\"))\n", " elif response_type == \"video\":\n", " content = gr.Video(os.path.join(\"files\", \"world.mp4\"))\n", " elif response_type == \"audio\":\n", " content = gr.Audio(os.path.join(\"files\", \"audio.wav\"))\n", " elif response_type == \"audio_file\":\n", " content = {\"path\": os.path.join(\"files\", \"audio.wav\"), \"alt_text\": \"description\"}\n", " elif response_type == \"image_file\":\n", " content = {\"path\": os.path.join(\"files\", \"avatar.png\"), \"alt_text\": \"description\"}\n", " elif response_type == \"video_file\":\n", " content = {\"path\": os.path.join(\"files\", \"world.mp4\"), \"alt_text\": \"description\"}\n", " elif response_type == \"txt_file\":\n", " content = {\"path\": os.path.join(\"files\", \"sample.txt\"), \"alt_text\": \"description\"}\n", " elif response_type == \"html\":\n", " content = gr.HTML(\n", " html_src(random.choice([\"harmful\", \"neutral\", \"beneficial\"]))\n", " )\n", " else:\n", " content = txt\n", " msg[\"content\"] = content # type: ignore\n", " history.append(msg)\n", " return history\n", "\n", "fig = random_plot()\n", "\n", "with gr.Blocks(fill_height=True) as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " type=\"messages\",\n", " bubble_full_width=False,\n", " scale=1,\n", " show_copy_button=True,\n", " avatar_images=(\n", " None, # os.path.join(\"files\", \"avatar.png\"),\n", " os.path.join(\"files\", \"avatar.png\"),\n", " ),\n", " )\n", " response_type = gr.Radio(\n", " [\n", " \"audio_file\",\n", " \"image_file\",\n", " \"video_file\",\n", " \"txt_file\",\n", " \"plot\",\n", " \"matplotlib_plot\",\n", " \"bokeh_plot\",\n", " \"image\",\n", " \"text\",\n", " \"gallery\",\n", " \"video\",\n", " \"audio\",\n", " \"html\",\n", " ],\n", " value=\"text\",\n", " label=\"Response Type\",\n", " )\n", "\n", " chat_input = gr.MultimodalTextbox(\n", " interactive=True,\n", " placeholder=\"Enter message or upload file...\",\n", " show_label=False,\n", " )\n", "\n", " chat_msg = chat_input.submit(\n", " add_message, [chatbot, chat_input], [chatbot, chat_input]\n", " )\n", " bot_msg = chat_msg.then(\n", " bot, [chatbot, response_type], chatbot, api_name=\"bot_response\"\n", " )\n", " bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])\n", "\n", " chatbot.like(print_like_dislike, None, None)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file +{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_core_components"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio plotly numpy pandas matplotlib "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/audio.wav https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/audio.wav\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/avatar.png\n", "!wget -q -O files/sample.txt https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/sample.txt\n", "!wget -q -O files/world.mp4 https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components/files/world.mp4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["# type: ignore\n", "import gradio as gr\n", "import os\n", "import plotly.express as px\n", "import random\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "txt = \"\"\"\n", "Absolutely! The mycorrhizal network, often referred to as the \"Wood Wide Web,\" is a symbiotic association between fungi and the roots of most plant species. Here\u2019s a deeper dive into how it works and its implications:\n", "\n", "### How It Works\n", "\n", "1. **Symbiosis**: Mycorrhizal fungi attach to plant roots, extending far into the soil. The plant provides the fungi with carbohydrates produced via photosynthesis. In return, the fungi help the plant absorb water and essential nutrients like phosphorus and nitrogen from the soil.\n", "\n", "2. **Network Formation**: The fungal hyphae (thread-like structures) connect individual plants, creating an extensive underground network. This network can link many plants together, sometimes spanning entire forests.\n", "\n", "3. **Communication**: Trees and plants use this network to communicate and share resources. For example, a tree under attack by pests can send chemical signals through the mycorrhizal network to warn neighboring trees. These trees can then produce defensive chemicals to prepare for the impending threat.\n", "\n", "### Benefits and Functions\n", "\n", "1. **Resource Sharing**: The network allows for the redistribution of resources among plants. For instance, a large, established tree might share excess nutrients and water with smaller, younger trees, promoting overall forest health.\n", "\n", "2. **Defense Mechanism**: The ability to share information about pests and diseases enhances the resilience of plant communities. This early warning system helps plants activate their defenses before they are directly affected.\n", "\n", "3. **Support for Seedlings**: Young seedlings, which have limited root systems, benefit immensely from the mycorrhizal network. They receive nutrients and water from larger plants, increasing their chances of survival and growth.\n", "\n", "### Ecological Impact\n", "\n", "1. **Biodiversity**: The mycorrhizal network supports biodiversity by fostering a cooperative environment. Plants of different species can coexist and thrive because of the shared resources and information.\n", "\n", "2. **Forest Health**: The network enhances the overall health of forests. By enabling efficient nutrient cycling and supporting plant defenses, it contributes to the stability and longevity of forest ecosystems.\n", "\n", "3. **Climate Change Mitigation**: Healthy forests act as significant carbon sinks, absorbing carbon dioxide from the atmosphere. The mycorrhizal network plays a critical role in maintaining forest health and, consequently, in mitigating climate change.\n", "\n", "### Research and Discoveries\n", "\n", "1. **Suzanne Simard's Work**: Ecologist Suzanne Simard\u2019s research has been pivotal in uncovering the complexities of the mycorrhizal network. She demonstrated that trees of different species can share resources and that \"mother trees\" (large, older trees) play a crucial role in nurturing younger plants.\n", "\n", "2. **Implications for Conservation**: Understanding the mycorrhizal network has significant implications for conservation efforts. It highlights the importance of preserving not just individual trees but entire ecosystems, including the fungal networks that sustain them.\n", "\n", "### Practical Applications\n", "\n", "1. **Agriculture**: Farmers and horticulturists are exploring the use of mycorrhizal fungi to improve crop yields and soil health. By incorporating these fungi into agricultural practices, they can reduce the need for chemical fertilizers and enhance plant resilience.\n", "\n", "2. **Reforestation**: In reforestation projects, introducing mycorrhizal fungi can accelerate the recovery of degraded lands. The fungi help establish healthy plant communities, ensuring the success of newly planted trees.\n", "\n", "The \"Wood Wide Web\" exemplifies the intricate and often hidden connections that sustain life on Earth. It\u2019s a reminder of the profound interdependence within natural systems and the importance of preserving these delicate relationships.\n", "\"\"\"\n", "\n", "def random_plot():\n", " df = px.data.iris()\n", " fig = px.scatter(\n", " df,\n", " x=\"sepal_width\",\n", " y=\"sepal_length\",\n", " color=\"species\",\n", " size=\"petal_length\",\n", " hover_data=[\"petal_width\"],\n", " )\n", " return fig\n", "\n", "color_map = {\n", " \"harmful\": \"crimson\",\n", " \"neutral\": \"gray\",\n", " \"beneficial\": \"green\",\n", "}\n", "\n", "def html_src(harm_level):\n", " return f\"\"\"\n", "
\n", "
\n", " {harm_level}\n", "
\n", "
\n", "\"\"\"\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def random_bokeh_plot():\n", " from bokeh.models import ColumnDataSource, Whisker\n", " from bokeh.plotting import figure\n", " from bokeh.sampledata.autompg2 import autompg2 as df\n", " from bokeh.transform import factor_cmap, jitter\n", "\n", " classes = sorted(df[\"class\"].unique())\n", "\n", " p = figure(\n", " height=400,\n", " x_range=classes,\n", " background_fill_color=\"#efefef\",\n", " title=\"Car class vs HWY mpg with quintile ranges\",\n", " )\n", " p.xgrid.grid_line_color = None\n", "\n", " g = df.groupby(\"class\")\n", " upper = g.hwy.quantile(0.80)\n", " lower = g.hwy.quantile(0.20)\n", " source = ColumnDataSource(data=dict(base=classes, upper=upper, lower=lower))\n", "\n", " error = Whisker(\n", " base=\"base\",\n", " upper=\"upper\",\n", " lower=\"lower\",\n", " source=source,\n", " level=\"annotation\",\n", " line_width=2,\n", " )\n", " error.upper_head.size = 20\n", " error.lower_head.size = 20\n", " p.add_layout(error)\n", "\n", " p.circle(\n", " jitter(\"class\", 0.3, range=p.x_range),\n", " \"hwy\",\n", " source=df,\n", " alpha=0.5,\n", " size=13,\n", " line_color=\"white\",\n", " color=factor_cmap(\"class\", \"Light6\", classes),\n", " )\n", " return p\n", "\n", "def random_matplotlib_plot():\n", " import numpy as np\n", " import pandas as pd\n", " import matplotlib.pyplot as plt\n", "\n", " countries = [\"USA\", \"Canada\", \"Mexico\", \"UK\"]\n", " months = [\"January\", \"February\", \"March\", \"April\", \"May\"]\n", " m = months.index(\"January\")\n", " r = 3.2\n", " start_day = 30 * m\n", " final_day = 30 * (m + 1)\n", " x = np.arange(start_day, final_day + 1)\n", " pop_count = {\"USA\": 350, \"Canada\": 40, \"Mexico\": 300, \"UK\": 120}\n", " df = pd.DataFrame({\"day\": x})\n", " for country in countries:\n", " df[country] = x ** (r) * (pop_count[country] + 1)\n", "\n", " fig = plt.figure()\n", " plt.plot(df[\"day\"], df[countries].to_numpy())\n", " plt.title(\"Outbreak in \" + \"January\")\n", " plt.ylabel(\"Cases\")\n", " plt.xlabel(\"Days since Day 0\")\n", " plt.legend(countries)\n", " return fig\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append({\"role\": \"user\", \"content\": {\"path\": x}})\n", " if message[\"text\"] is not None:\n", " history.append({\"role\": \"user\", \"content\": message[\"text\"]})\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def bot(history, response_type):\n", " msg = {\"role\": \"assistant\", \"content\": \"\"}\n", " if response_type == \"plot\":\n", " content = gr.Plot(random_plot())\n", " elif response_type == \"bokeh_plot\":\n", " content = gr.Plot(random_bokeh_plot())\n", " elif response_type == \"matplotlib_plot\":\n", " content = gr.Plot(random_matplotlib_plot())\n", " elif response_type == \"gallery\":\n", " content = gr.Gallery(\n", " [os.path.join(\"files\", \"avatar.png\"), os.path.join(\"files\", \"avatar.png\")]\n", " )\n", " elif response_type == \"dataframe\":\n", " content = gr.Dataframe(\n", " interactive=True,\n", " headers=[\"One\", \"Two\", \"Three\"],\n", " col_count=(3, \"fixed\"),\n", " row_count=(3, \"fixed\"),\n", " value=[[1, 2, 3], [4, 5, 6], [7, 8, 9]],\n", " label=\"Dataframe\",\n", " )\n", " elif response_type == \"image\":\n", " content = gr.Image(os.path.join(\"files\", \"avatar.png\"))\n", " elif response_type == \"video\":\n", " content = gr.Video(os.path.join(\"files\", \"world.mp4\"))\n", " elif response_type == \"audio\":\n", " content = gr.Audio(os.path.join(\"files\", \"audio.wav\"))\n", " elif response_type == \"audio_file\":\n", " content = {\"path\": os.path.join(\"files\", \"audio.wav\"), \"alt_text\": \"description\"}\n", " elif response_type == \"image_file\":\n", " content = {\"path\": os.path.join(\"files\", \"avatar.png\"), \"alt_text\": \"description\"}\n", " elif response_type == \"video_file\":\n", " content = {\"path\": os.path.join(\"files\", \"world.mp4\"), \"alt_text\": \"description\"}\n", " elif response_type == \"txt_file\":\n", " content = {\"path\": os.path.join(\"files\", \"sample.txt\"), \"alt_text\": \"description\"}\n", " elif response_type == \"html\":\n", " content = gr.HTML(\n", " html_src(random.choice([\"harmful\", \"neutral\", \"beneficial\"]))\n", " )\n", " else:\n", " content = txt\n", " msg[\"content\"] = content # type: ignore\n", " history.append(msg)\n", " return history\n", "\n", "fig = random_plot()\n", "\n", "with gr.Blocks(fill_height=True) as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " type=\"messages\",\n", " bubble_full_width=False,\n", " scale=1,\n", " show_copy_button=True,\n", " avatar_images=(\n", " None, # os.path.join(\"files\", \"avatar.png\"),\n", " os.path.join(\"files\", \"avatar.png\"),\n", " ),\n", " )\n", " response_type = gr.Radio(\n", " [\n", " \"audio_file\",\n", " \"image_file\",\n", " \"video_file\",\n", " \"txt_file\",\n", " \"plot\",\n", " \"matplotlib_plot\",\n", " \"bokeh_plot\",\n", " \"image\",\n", " \"text\",\n", " \"gallery\",\n", " \"dataframe\",\n", " \"video\",\n", " \"audio\",\n", " \"html\",\n", " ],\n", " value=\"text\",\n", " label=\"Response Type\",\n", " )\n", "\n", " chat_input = gr.MultimodalTextbox(\n", " interactive=True,\n", " placeholder=\"Enter message or upload file...\",\n", " show_label=False,\n", " )\n", "\n", " chat_msg = chat_input.submit(\n", " add_message, [chatbot, chat_input], [chatbot, chat_input]\n", " )\n", " bot_msg = chat_msg.then(\n", " bot, [chatbot, response_type], chatbot, api_name=\"bot_response\"\n", " )\n", " bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])\n", "\n", " chatbot.like(print_like_dislike, None, None)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file diff --git a/demo/chatbot_core_components/run.py b/demo/chatbot_core_components/run.py index 57f1282caa341..0ef50965f0235 100644 --- a/demo/chatbot_core_components/run.py +++ b/demo/chatbot_core_components/run.py @@ -166,6 +166,15 @@ def bot(history, response_type): content = gr.Gallery( [os.path.join("files", "avatar.png"), os.path.join("files", "avatar.png")] ) + elif response_type == "dataframe": + content = gr.Dataframe( + interactive=True, + headers=["One", "Two", "Three"], + col_count=(3, "fixed"), + row_count=(3, "fixed"), + value=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], + label="Dataframe", + ) elif response_type == "image": content = gr.Image(os.path.join("files", "avatar.png")) elif response_type == "video": @@ -216,6 +225,7 @@ def bot(history, response_type): "image", "text", "gallery", + "dataframe", "video", "audio", "html", diff --git a/guides/05_chatbots/03_agents-and-tool-usage.md b/guides/05_chatbots/03_agents-and-tool-usage.md index fa3be443b2317..66b2caee756a7 100644 --- a/guides/05_chatbots/03_agents-and-tool-usage.md +++ b/guides/05_chatbots/03_agents-and-tool-usage.md @@ -35,37 +35,50 @@ We'll start by importing the necessary classes from transformers and gradio. ```python import gradio as gr from gradio import ChatMessage -from transformers import load_tool, ReactCodeAgent, HfEngine -from utils import stream_from_transformers_agent +from transformers import Tool, ReactCodeAgent # type: ignore +from transformers.agents import stream_to_gradio, HfApiEngine # type: ignore # Import tool from Hub -image_generation_tool = load_tool("m-ric/text-to-image") - +image_generation_tool = Tool.from_space( + space_id="black-forest-labs/FLUX.1-schnell", + name="image_generator", + description="Generates an image following your prompt. Returns a PIL Image.", + api_name="/infer", +) -llm_engine = HfEngine("meta-llama/Meta-Llama-3-70B-Instruct") -# Initialize the agent with both tools +llm_engine = HfApiEngine("Qwen/Qwen2.5-Coder-32B-Instruct") +# Initialize the agent with both tools and engine agent = ReactCodeAgent(tools=[image_generation_tool], llm_engine=llm_engine) ``` -Then we'll build the UI. The bulk of the logic is handled by `stream_from_transformers_agent`. We won't cover it in this guide because it will soon be merged to transformers but you can see its source code [here](https://huggingface.co/spaces/gradio/agent_chatbot/blob/main/utils.py). +Then we'll build the UI: ```python -def interact_with_agent(prompt, messages): - messages.append(ChatMessage(role="user", content=prompt)) +def interact_with_agent(prompt, history): + messages = [] yield messages - for msg in stream_from_transformers_agent(agent, prompt): - messages.append(msg) + for msg in stream_to_gradio(agent, prompt): + messages.append(asdict(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]) +demo = gr.ChatInterface( + interact_with_agent, + chatbot= gr.Chatbot( + label="Agent", + type="messages", + avatar_images=( + None, + "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png", + ), + ), + examples=[ + ["Generate an image of an astronaut riding an alligator"], + ["I am writing a children's book for my daughter. Can you help me with some illustrations?"], + ], + type="messages", +) ``` You can see the full demo code [here](https://huggingface.co/spaces/gradio/agent_chatbot/blob/main/app.py). diff --git a/js/chatbot/shared/ChatBot.svelte b/js/chatbot/shared/ChatBot.svelte index dcb87dcd1b7e8..29a002ec7559f 100644 --- a/js/chatbot/shared/ChatBot.svelte +++ b/js/chatbot/shared/ChatBot.svelte @@ -405,10 +405,10 @@ } /* table styles */ - .message-wrap :global(.bot table), - .message-wrap :global(.bot tr), - .message-wrap :global(.bot td), - .message-wrap :global(.bot th) { + .message-wrap :global(.bot:not(:has(.table-wrap)) table), + .message-wrap :global(.bot:not(:has(.table-wrap)) tr), + .message-wrap :global(.bot:not(:has(.table-wrap)) td), + .message-wrap :global(.bot:not(:has(.table-wrap)) th) { border: 1px solid var(--border-color-primary); } diff --git a/js/chatbot/shared/Component.svelte b/js/chatbot/shared/Component.svelte index 12c1f48d1950f..f13f616ee6140 100644 --- a/js/chatbot/shared/Component.svelte +++ b/js/chatbot/shared/Component.svelte @@ -1,5 +1,12 @@