File size: 9,225 Bytes
f389e2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26750af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f389e2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a05d6e8
f389e2f
 
 
 
 
 
a05d6e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f389e2f
 
 
 
 
 
 
a05d6e8
 
 
 
 
f389e2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a05d6e8
 
 
 
 
f389e2f
 
 
 
 
 
a05d6e8
f389e2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ec2fef2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Prototyping a Basic Chatbot UI\n",
    "* **Created by:** Eric Martinez\n",
    "* **For:** Software Engineering 2\n",
    "* **At:** University of Texas Rio-Grande Valley"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e989bbe3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Tools and Concepts"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f764b43",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Jupyter Notebook\n",
    "Jupyter is a great tool for writing and interacting with code. It lets you embed code and text to write and document code exploration.\n",
    "\n",
    "It is a great tool for prototyping and rapid development on top of being a great way to distribute understandable code to others.\n",
    "\n",
    "In many areas, such as machine learning and data science, it is extremely common for developers to spend most of their time in a notebook environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ed3e93b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "!pip install jupyter-notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "368cda1a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Gradio\n",
    "\n",
    "Gradio is an open-source Python library that allows developers to create and prototype user interfaces (UIs) and APIs for machine learning applications and LLMs quickly and easily. Three reasons it is a great tool are:\n",
    "\n",
    "* Simple and intuitive: Gradio makes it easy to create UIs with minimal code, allowing developers to focus on their models.\n",
    "* Versatile: Gradio supports a wide range of input and output types, making it suitable for various machine learning tasks.\n",
    "* Shareable: Gradio interfaces can be shared with others, enabling collaboration and easy access to your models."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2888a24",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Chatbots\n",
    "\n",
    "Chatbots are AI-powered conversational agents designed to interact with users through natural language. LLMs have the potential to revolutionize chatbots by providing more human-like, accurate, and context-aware responses, leading to more engaging and useful interactions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f24bac1",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Example: Gradio Interface\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ecdb50a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "!pip -q install --upgrade gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0b28a4e7",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7882\n",
      "Running on public URL: https://d807b00726b10425a4.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://d807b00726b10425a4.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import gradio as gr\n",
    " \n",
    "def do_something_cool(first_name, last_name):\n",
    "    return f\"{first_name} {last_name} is so cool\"\n",
    "    \n",
    "with gr.Blocks() as app:\n",
    "    first_name_box = gr.Textbox(label=\"First Name\")\n",
    "    last_name_box = gr.Textbox(label=\"Last Name\")\n",
    "    output_box = gr.Textbox(label=\"Output\", interactive=False)\n",
    "    btn = gr.Button(value =\"Send\")\n",
    "    btn.click(do_something_cool, inputs = [first_name_box, last_name_box], outputs = [output_box])\n",
    "    app.launch(share=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b1b69e",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Example: Basic Gradio Chatbot Interface / Just the look"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea2878bb",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Version with minimal comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6f1cca3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "\n",
    "def chat(message, history):\n",
    "    history = history or []\n",
    "    # Set a simple AI reply (replace this with a call to an LLM for a more sophisticated response)\n",
    "    ai_reply = \"hello\"      \n",
    "    history.append((message, ai_reply))\n",
    "    return None, history, history\n",
    "    \n",
    "with gr.Blocks() as app:\n",
    "    with gr.Tab(\"Conversation\"):\n",
    "        with gr.Row():\n",
    "            with gr.Column():\n",
    "                chatbot = gr.Chatbot(label=\"Conversation\")\n",
    "                message = gr.Textbox(label=\"Message\")\n",
    "                history_state = gr.State()\n",
    "                btn = gr.Button(value =\"Send\")\n",
    "            btn.click(chat, inputs = [message, history_state], outputs = [message, chatbot, history_state])\n",
    "    app.launch(share=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc72efb",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Version with more comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44debca6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "\n",
    "# Define the chat function that processes user input and generates an AI response\n",
    "def chat(message, history):\n",
    "    # If history is empty, initialize it as an empty list\n",
    "    history = history or []\n",
    "    # Set a simple AI reply (replace this with a call to an LLM for a more sophisticated response)\n",
    "    ai_reply = \"hello\"\n",
    "    # Append the user message and AI reply to the conversation history\n",
    "    history.append((message, ai_reply))\n",
    "    # Return the updated history and display it in the chatbot interface\n",
    "    return None, history, history\n",
    "\n",
    "# Create a Gradio Blocks interface\n",
    "with gr.Blocks() as app:\n",
    "    # Create a tab for the conversation\n",
    "    with gr.Tab(\"Conversation\"):\n",
    "        # Create a row for the input components\n",
    "        with gr.Row():\n",
    "            # Create a column for the input components\n",
    "            with gr.Column():\n",
    "                # Create a chatbot component to display the conversation\n",
    "                chatbot = gr.Chatbot(label=\"Conversation\")\n",
    "                # Create a textbox for user input\n",
    "                message = gr.Textbox(label=\"Message\")\n",
    "                # Create a state variable to store the conversation history\n",
    "                history_state = gr.State()\n",
    "                # Create a button to send the user's message\n",
    "                btn = gr.Button(value=\"Send\")\n",
    "            # Connect the button click event to the chat function, passing in the input components and updating the output components\n",
    "            btn.click(chat, inputs=[message, history_state], outputs=[message, chatbot, history_state])\n",
    "    # Launch the Gradio interface and make it shareable\n",
    "    app.launch(share=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b660170",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Sweet! That wasn't too bad, not much code to make a cool little UI!"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}