aliabd commited on
Commit
cf42464
·
verified ·
1 Parent(s): e67bc8c

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. messages_testcase.py +101 -0
  3. run.ipynb +1 -1
  4. run.py +18 -6
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 4.37.2
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.38.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
messages_testcase.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
5
+
6
+
7
+ color_map = {
8
+ "harmful": "crimson",
9
+ "neutral": "gray",
10
+ "beneficial": "green",
11
+ }
12
+
13
+ def html_src(harm_level):
14
+ return f"""
15
+ <div style="display: flex; gap: 5px;padding: 2px 4px;margin-top: -40px">
16
+ <div style="background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;">
17
+ {harm_level}
18
+ </div>
19
+ </div>
20
+ """
21
+
22
+ def print_like_dislike(x: gr.LikeData):
23
+ print(x.index, x.value, x.liked)
24
+
25
+ def add_message(history, message):
26
+ for x in message["files"]:
27
+ history.append({"role": "user", "content": {"path": x}})
28
+ if message["text"] is not None:
29
+ history.append({"role": "user", "content": message['text']})
30
+ return history, gr.MultimodalTextbox(value=None, interactive=False)
31
+
32
+ def bot(history, response_type):
33
+ if response_type == "gallery":
34
+ msg = {"role": "assistant", "content": gr.Gallery(
35
+ ["https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png",
36
+ "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"]
37
+ )
38
+ }
39
+ elif response_type == "image":
40
+ msg = {"role": "assistant",
41
+ "content": gr.Image("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png")
42
+ }
43
+ elif response_type == "video":
44
+ msg = {"role": "assistant",
45
+ "content": gr.Video("https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4")
46
+ }
47
+ elif response_type == "audio":
48
+ msg = {"role": "assistant",
49
+ "content": gr.Audio("https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav")
50
+ }
51
+ elif response_type == "html":
52
+ msg = {"role": "assistant",
53
+ "content": gr.HTML(
54
+ html_src(random.choice(["harmful", "neutral", "beneficial"]))
55
+ )
56
+ }
57
+ else:
58
+ msg = {"role": "assistant", "content": "Cool!"}
59
+ history.append(msg)
60
+ return history
61
+
62
+
63
+ with gr.Blocks(fill_height=True) as demo:
64
+ chatbot = gr.Chatbot(
65
+ elem_id="chatbot",
66
+ bubble_full_width=False,
67
+ scale=1,
68
+ type="messages"
69
+ )
70
+ response_type = gr.Radio(
71
+ [
72
+ "image",
73
+ "text",
74
+ "gallery",
75
+ "video",
76
+ "audio",
77
+ "html",
78
+ ],
79
+ value="text",
80
+ label="Response Type",
81
+ )
82
+
83
+ chat_input = gr.MultimodalTextbox(
84
+ interactive=True,
85
+ placeholder="Enter message or upload file...",
86
+ show_label=False,
87
+ )
88
+
89
+ chat_msg = chat_input.submit(
90
+ add_message, [chatbot, chat_input], [chatbot, chat_input]
91
+ )
92
+ bot_msg = chat_msg.then(
93
+ bot, [chatbot, response_type], chatbot, api_name="bot_response"
94
+ )
95
+ bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
96
+
97
+ chatbot.like(print_like_dislike, None, None)
98
+
99
+ demo.queue()
100
+ if __name__ == "__main__":
101
+ demo.launch()
run.ipynb CHANGED
@@ -1 +1 @@
1
- {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_core_components_simple"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"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_simple/files/audio.wav\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/avatar.png\n", "!wget -q -O files/sample.txt https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/sample.txt\n", "!wget -q -O files/world.mp4 https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/world.mp4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\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", "\n", "color_map = {\n", " \"harmful\": \"crimson\",\n", " \"neutral\": \"gray\",\n", " \"beneficial\": \"green\",\n", "}\n", "\n", "def html_src(harm_level):\n", " return f\"\"\"\n", "<div style=\"display: flex; gap: 5px;padding: 2px 4px;margin-top: -40px\">\n", " <div style=\"background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;\">\n", " {harm_level}\n", " </div>\n", "</div>\n", "\"\"\"\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append(((x,), None))\n", " if message[\"text\"] is not None:\n", " history.append((message[\"text\"], None))\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "def bot(history, response_type):\n", " if response_type == \"gallery\":\n", " history[-1][1] = gr.Gallery(\n", " [\"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\",\n", " \"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\"]\n", " )\n", " elif response_type == \"image\":\n", " history[-1][1] = gr.Image(\"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\")\n", " elif response_type == \"video\":\n", " history[-1][1] = gr.Video(\"https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4\")\n", " elif response_type == \"audio\":\n", " history[-1][1] = gr.Audio(\"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav\")\n", " elif response_type == \"html\":\n", " history[-1][1] = gr.HTML(\n", " html_src(random.choice([\"harmful\", \"neutral\", \"beneficial\"]))\n", " )\n", " else:\n", " history[-1][1] = \"Cool!\"\n", " return history\n", "\n", "\n", "with gr.Blocks(fill_height=True) as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " bubble_full_width=False,\n", " scale=1,\n", " )\n", " response_type = gr.Radio(\n", " [\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", "demo.queue()\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_core_components_simple"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"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_simple/files/audio.wav\n", "!wget -q -O files/avatar.png https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/avatar.png\n", "!wget -q -O files/sample.txt https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/sample.txt\n", "!wget -q -O files/world.mp4 https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/files/world.mp4\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/chatbot_core_components_simple/messages_testcase.py"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import os\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", "\n", "color_map = {\n", " \"harmful\": \"crimson\",\n", " \"neutral\": \"gray\",\n", " \"beneficial\": \"green\",\n", "}\n", "\n", "\n", "def html_src(harm_level):\n", " return f\"\"\"\n", "<div style=\"display: flex; gap: 5px;\">\n", " <div style=\"background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;\">\n", " {harm_level}\n", " </div>\n", "</div>\n", "\"\"\"\n", "\n", "\n", "def print_like_dislike(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "\n", "def add_message(history, message):\n", " for x in message[\"files\"]:\n", " history.append(((x,), None))\n", " if message[\"text\"] is not None:\n", " history.append((message[\"text\"], None))\n", " return history, gr.MultimodalTextbox(value=None, interactive=False)\n", "\n", "\n", "def bot(history, response_type):\n", " if response_type == \"gallery\":\n", " history[-1][1] = gr.Gallery(\n", " [\n", " \"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\",\n", " \"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\",\n", " ]\n", " )\n", " elif response_type == \"image\":\n", " history[-1][1] = gr.Image(\n", " \"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png\"\n", " )\n", " elif response_type == \"video\":\n", " history[-1][1] = gr.Video(\n", " \"https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4\"\n", " )\n", " elif response_type == \"audio\":\n", " history[-1][1] = gr.Audio(\n", " \"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav\"\n", " )\n", " elif response_type == \"html\":\n", " history[-1][1] = gr.HTML(\n", " html_src(random.choice([\"harmful\", \"neutral\", \"beneficial\"]))\n", " )\n", " else:\n", " history[-1][1] = \"Cool!\"\n", " return history\n", "\n", "\n", "with gr.Blocks(fill_height=True) as demo:\n", " chatbot = gr.Chatbot(\n", " elem_id=\"chatbot\",\n", " bubble_full_width=False,\n", " scale=1,\n", " )\n", " response_type = gr.Radio(\n", " [\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", "demo.queue()\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py CHANGED
@@ -11,18 +11,21 @@ color_map = {
11
  "beneficial": "green",
12
  }
13
 
 
14
  def html_src(harm_level):
15
  return f"""
16
- <div style="display: flex; gap: 5px;padding: 2px 4px;margin-top: -40px">
17
  <div style="background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;">
18
  {harm_level}
19
  </div>
20
  </div>
21
  """
22
 
 
23
  def print_like_dislike(x: gr.LikeData):
24
  print(x.index, x.value, x.liked)
25
 
 
26
  def add_message(history, message):
27
  for x in message["files"]:
28
  history.append(((x,), None))
@@ -30,18 +33,27 @@ def add_message(history, message):
30
  history.append((message["text"], None))
31
  return history, gr.MultimodalTextbox(value=None, interactive=False)
32
 
 
33
  def bot(history, response_type):
34
  if response_type == "gallery":
35
  history[-1][1] = gr.Gallery(
36
- ["https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png",
37
- "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"]
 
 
38
  )
39
  elif response_type == "image":
40
- history[-1][1] = gr.Image("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png")
 
 
41
  elif response_type == "video":
42
- history[-1][1] = gr.Video("https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4")
 
 
43
  elif response_type == "audio":
44
- history[-1][1] = gr.Audio("https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav")
 
 
45
  elif response_type == "html":
46
  history[-1][1] = gr.HTML(
47
  html_src(random.choice(["harmful", "neutral", "beneficial"]))
 
11
  "beneficial": "green",
12
  }
13
 
14
+
15
  def html_src(harm_level):
16
  return f"""
17
+ <div style="display: flex; gap: 5px;">
18
  <div style="background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;">
19
  {harm_level}
20
  </div>
21
  </div>
22
  """
23
 
24
+
25
  def print_like_dislike(x: gr.LikeData):
26
  print(x.index, x.value, x.liked)
27
 
28
+
29
  def add_message(history, message):
30
  for x in message["files"]:
31
  history.append(((x,), None))
 
33
  history.append((message["text"], None))
34
  return history, gr.MultimodalTextbox(value=None, interactive=False)
35
 
36
+
37
  def bot(history, response_type):
38
  if response_type == "gallery":
39
  history[-1][1] = gr.Gallery(
40
+ [
41
+ "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png",
42
+ "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png",
43
+ ]
44
  )
45
  elif response_type == "image":
46
+ history[-1][1] = gr.Image(
47
+ "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
48
+ )
49
  elif response_type == "video":
50
+ history[-1][1] = gr.Video(
51
+ "https://github.com/gradio-app/gradio/raw/main/demo/video_component/files/world.mp4"
52
+ )
53
  elif response_type == "audio":
54
+ history[-1][1] = gr.Audio(
55
+ "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav"
56
+ )
57
  elif response_type == "html":
58
  history[-1][1] = gr.HTML(
59
  html_src(random.choice(["harmful", "neutral", "beneficial"]))