aliabd HF staff commited on
Commit
6388682
1 Parent(s): da34507

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. run.ipynb +1 -1
  3. run.py +1 -1
  4. utils.py +7 -4
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 4.38.1
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
 
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 4.39.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
run.ipynb CHANGED
@@ -1 +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\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", "\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", "\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", "\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", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
 
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", "\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", "\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", "\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", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from gradio import ChatMessage
3
- from transformers import load_tool, ReactCodeAgent, HfEngine
4
  from utils import stream_from_transformers_agent
5
 
6
  # Import tool from Hub
 
1
  import gradio as gr
2
  from gradio import ChatMessage
3
+ from transformers import load_tool, ReactCodeAgent, HfEngine # type: ignore
4
  from utils import stream_from_transformers_agent
5
 
6
  # Import tool from Hub
utils.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from gradio import ChatMessage
2
  from transformers.agents import ReactCodeAgent, agent_types
3
  from typing import Generator
@@ -37,7 +39,8 @@ def stream_from_transformers_agent(
37
 
38
  class Output:
39
  output: agent_types.AgentType | str = None
40
-
 
41
  for step_log in agent.run(prompt, stream=True):
42
  if isinstance(step_log, dict):
43
  for message in pull_message(step_log):
@@ -48,16 +51,16 @@ def stream_from_transformers_agent(
48
  Output.output = step_log
49
  if isinstance(Output.output, agent_types.AgentText):
50
  yield ChatMessage(
51
- role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```")
52
  elif isinstance(Output.output, agent_types.AgentImage):
53
  yield ChatMessage(
54
  role="assistant",
55
- content={"path": Output.output.to_string(), "mime_type": "image/png"},
56
  )
57
  elif isinstance(Output.output, agent_types.AgentAudio):
58
  yield ChatMessage(
59
  role="assistant",
60
- content={"path": Output.output.to_string(), "mime_type": "audio/wav"},
61
  )
62
  else:
63
  return ChatMessage(role="assistant", content=Output.output)
 
1
+ from __future__ import annotations
2
+
3
  from gradio import ChatMessage
4
  from transformers.agents import ReactCodeAgent, agent_types
5
  from typing import Generator
 
39
 
40
  class Output:
41
  output: agent_types.AgentType | str = None
42
+
43
+ step_log = None
44
  for step_log in agent.run(prompt, stream=True):
45
  if isinstance(step_log, dict):
46
  for message in pull_message(step_log):
 
51
  Output.output = step_log
52
  if isinstance(Output.output, agent_types.AgentText):
53
  yield ChatMessage(
54
+ role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```") # type: ignore
55
  elif isinstance(Output.output, agent_types.AgentImage):
56
  yield ChatMessage(
57
  role="assistant",
58
+ content={"path": Output.output.to_string(), "mime_type": "image/png"}, # type: ignore
59
  )
60
  elif isinstance(Output.output, agent_types.AgentAudio):
61
  yield ChatMessage(
62
  role="assistant",
63
+ content={"path": Output.output.to_string(), "mime_type": "audio/wav"}, # type: ignore
64
  )
65
  else:
66
  return ChatMessage(role="assistant", content=Output.output)