-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Update Transformers agent demo #9885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7c093eb
update code
freddyaboulton 02282ed
Merge branch 'main' into modify-transformers-agent-demo
freddyaboulton fd98395
modify imports
freddyaboulton 275e4cf
Merge branch 'main' into modify-transformers-agent-demo
freddyaboulton 74e65db
fix
freddyaboulton f29ada6
merge
freddyaboulton 1cc6f13
update notebook
freddyaboulton 958c4f4
Merge branch 'main' into modify-transformers-agent-demo
abidlabs 2a4fd72
gr.load_chat: Allow loading any openai-compatible server immediately …
aliabid94 142ca1c
Allow editing chatbot messages (#10203)
aliabid94 c265f6a
Update Guides related to deploying Gradio chatbots to Discord, Slack,…
abidlabs 8a9bb54
Allow editable ChatInterface (#10229)
aliabid94 e98c398
Fix typing for components in `gr.Interface` and docstring in `image.p…
abidlabs 8f47f87
Lite: Capture stdout and stderr from the main thread (#9984)
whitphx ce9ec30
use chat interface
freddyaboulton 3ec6726
Merge branch 'main' into modify-transformers-agent-demo
freddyaboulton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
git+https://github.com/huggingface/transformers.git#egg=transformers[agents] | ||
transformers>=4.47.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
{"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 gradio import ChatMessage\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, messages):\n", " messages.append(ChatMessage(role=\"user\", content=prompt))\n", " yield messages\n", " for msg in stream_to_gradio(agent, prompt):\n", " messages.append(msg)\n", " yield messages\n", " yield messages\n", "\n", "\n", "with gr.Blocks() as demo:\n", " stored_message = gr.State([])\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", " text_input = gr.Textbox(lines=1, label=\"Chat Message\")\n", " text_input.submit(\n", " lambda s: (s, \"\"), [text_input], [stored_message, text_input]\n", " ).then(interact_with_agent, [stored_message, chatbot], [chatbot])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
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 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)) | ||
yield messages | ||
for msg in stream_from_transformers_agent(agent, prompt): | ||
for msg in stream_to_gradio(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")) | ||
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]) | ||
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() |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just fyi there are some discrepancies between the code here and the complete demo, e.g. the llm engine in the demo is
Qwen/Qwen2.5-Coder-32B-Instruct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good point!