tejasashinde commited on
Commit
0497427
·
verified ·
1 Parent(s): d621ddc

Updated gr.Chatbot code

Browse files
Files changed (1) hide show
  1. app.py +165 -38
app.py CHANGED
@@ -1,6 +1,12 @@
1
  import time
2
-
3
  import gradio as gr
 
 
 
 
 
 
 
4
  from gradio.themes.utils.theme_dropdown import create_theme_dropdown
5
 
6
  dropdown, js = create_theme_dropdown()
@@ -10,8 +16,8 @@ with gr.Blocks() as demo:
10
  with gr.Column(scale=10):
11
  gr.Markdown(
12
  """
13
- # Theme preview: `aurora`
14
- To use this theme, set `theme='tejasashinde/aurora'` in the `launch()` method of `gr.Blocks()` or `gr.Interface()`.
15
  You can append an `@` and a semantic version expression, e.g. @>=1.0.0,<2.0.0 to pin to a given version
16
  of this theme.
17
  """
@@ -26,23 +32,50 @@ with gr.Blocks() as demo:
26
  None,
27
  js="""
28
  () => {
29
- document.body.classList.toggle('dark');
 
 
 
 
 
30
  }
31
  """,
32
  )
33
 
34
  name = gr.Textbox(
35
  label="Name",
36
- info="Full name, including middle name. No special characters.",
37
  placeholder="John Doe",
38
  value="John Doe",
39
  interactive=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  )
41
 
42
  with gr.Row():
43
- slider1 = gr.Slider(label="Slider 1")
44
- slider2 = gr.Slider(label="Slider 2")
45
- gr.CheckboxGroup(["A", "B", "C"], label="Checkbox Group")
 
46
 
47
  with gr.Row():
48
  with gr.Column(variant="panel", scale=1):
@@ -50,15 +83,17 @@ with gr.Blocks() as demo:
50
  radio = gr.Radio(
51
  ["A", "B", "C"],
52
  label="Radio",
 
53
  info="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
54
  )
55
- drop = gr.Dropdown(["Option 1", "Option 2", "Option 3"], show_label=False)
56
  drop_2 = gr.Dropdown(
57
  ["Option A", "Option B", "Option C"],
58
  multiselect=True,
59
  value=["Option A"],
60
  label="Dropdown",
61
  interactive=True,
 
62
  )
63
  check = gr.Checkbox(label="Go")
64
  with gr.Column(variant="panel", scale=2):
@@ -88,52 +123,144 @@ with gr.Blocks() as demo:
88
  stop_btn = gr.Button("Stop", size="sm", variant="stop")
89
 
90
  with gr.Row():
91
- gr.Dataframe(value=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], label="Dataframe")
92
  gr.JSON(
93
- value={"a": 1, "b": 2, "c": {"test": "a", "test2": [1, 2, 3]}}, label="JSON"
94
  )
95
  gr.Label(value={"cat": 0.7, "dog": 0.2, "fish": 0.1})
96
- gr.File()
 
97
  with gr.Row():
98
- gr.ColorPicker()
99
- gr.Video("https://gradio-static-files.s3.us-west-2.amazonaws.com/world.mp4")
100
- gr.Gallery(
101
- [
102
- (
103
- "https://gradio-static-files.s3.us-west-2.amazonaws.com/lion.jpg",
104
- "lion",
105
- ),
106
- (
107
- "https://gradio-static-files.s3.us-west-2.amazonaws.com/logo.png",
108
- "logo",
109
- ),
110
- (
111
- "https://gradio-static-files.s3.us-west-2.amazonaws.com/tower.jpg",
112
- "tower",
113
- ),
114
- ],
115
- height=200,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  )
117
 
118
  with gr.Row():
119
  with gr.Column(scale=2):
120
- chatbot = gr.Chatbot([("Hello", "Hi")], label="Chatbot")
 
 
 
 
 
 
 
121
  chat_btn = gr.Button("Add messages")
122
 
 
 
 
 
 
 
 
 
 
 
 
123
  chat_btn.click(
124
- lambda history: (
125
- history + [["How are you?", "I am good."]] + (time.sleep(2) or [])
126
- ),
127
- chatbot,
128
- chatbot,
129
  )
 
130
  with gr.Column(scale=1):
131
  with gr.Accordion("Advanced Settings"):
132
- gr.Markdown("Hello")
133
  gr.Number(label="Chatbot control 1")
134
  gr.Number(label="Chatbot control 2")
135
  gr.Number(label="Chatbot control 3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
 
 
137
 
138
  if __name__ == "__main__":
139
- demo.queue().launch(theme='tejasashinde/aurora')
 
1
  import time
 
2
  import gradio as gr
3
+ from aurora import aurora
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ import pandas as pd
7
+ import random
8
+ from aurora_css import aurora_css
9
+
10
  from gradio.themes.utils.theme_dropdown import create_theme_dropdown
11
 
12
  dropdown, js = create_theme_dropdown()
 
16
  with gr.Column(scale=10):
17
  gr.Markdown(
18
  """
19
+ # Theme preview: `Aurora`
20
+ To use this theme, set `theme='tejasashinde/Aurora'` in the `launch()` method of `gr.Blocks()` or `gr.Interface()`.
21
  You can append an `@` and a semantic version expression, e.g. @>=1.0.0,<2.0.0 to pin to a given version
22
  of this theme.
23
  """
 
32
  None,
33
  js="""
34
  () => {
35
+ const app = document.querySelector(".gradio-app");
36
+ if (app.dataset.theme === "dark") {
37
+ app.dataset.theme = "light";
38
+ } else {
39
+ app.dataset.theme = "dark";
40
+ }
41
  }
42
  """,
43
  )
44
 
45
  name = gr.Textbox(
46
  label="Name",
47
+ info="Full name, including middle name.",
48
  placeholder="John Doe",
49
  value="John Doe",
50
  interactive=True,
51
+ elem_classes="aurora-hover-gradient"
52
+ )
53
+ # JavaScript to toggle dark mode on button click
54
+ toggle_dark.click(
55
+ None,
56
+ js="""
57
+ () => {
58
+ // Toggle the dark class on the body element
59
+ document.body.classList.toggle('dark');
60
+
61
+ // Dynamically check if dark mode is active and update the theme accordingly
62
+ const isDark = document.body.classList.contains('dark');
63
+ if (isDark) {
64
+ // Apply dark theme styles
65
+ gradio.theme = 'dark';
66
+ } else {
67
+ // Apply light theme styles
68
+ gradio.theme = 'light';
69
+ }
70
+ }
71
+ """
72
  )
73
 
74
  with gr.Row():
75
+ slider1 = gr.Slider(label="Slider 1", elem_classes="aurora-hover-gradient")
76
+ gr.ColorPicker(elem_classes="aurora-hover-gradient")
77
+ gr.DateTime(elem_classes="aurora-hover-gradient")
78
+ gr.CheckboxGroup(["A", "B", "C"], label="Checkbox Group", elem_classes="aurora-hover-gradient")
79
 
80
  with gr.Row():
81
  with gr.Column(variant="panel", scale=1):
 
83
  radio = gr.Radio(
84
  ["A", "B", "C"],
85
  label="Radio",
86
+ elem_classes="aurora-hover-gradient",
87
  info="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
88
  )
89
+ drop = gr.Dropdown(["Option 1", "Option 2", "Option 3"], show_label=False, elem_classes="aurora-hover-gradient")
90
  drop_2 = gr.Dropdown(
91
  ["Option A", "Option B", "Option C"],
92
  multiselect=True,
93
  value=["Option A"],
94
  label="Dropdown",
95
  interactive=True,
96
+ elem_classes="aurora-hover-gradient"
97
  )
98
  check = gr.Checkbox(label="Go")
99
  with gr.Column(variant="panel", scale=2):
 
123
  stop_btn = gr.Button("Stop", size="sm", variant="stop")
124
 
125
  with gr.Row():
 
126
  gr.JSON(
127
+ value={"a": 1, "b": 2, "c": {"test": "a", "test2": [1, 2]}}, label="JSON"
128
  )
129
  gr.Label(value={"cat": 0.7, "dog": 0.2, "fish": 0.1})
130
+ gr.File(label="Upload File")
131
+
132
  with gr.Row():
133
+ # Initial data for the DataFrame
134
+ initial_data = [
135
+ ["Alice", 25, "F", "python"],
136
+ ["Bob", 30, "M", "cpp"],
137
+ ["Charlie", 22, "O", "cpp"],
138
+ ["Diana", 28, "F", "python"],
139
+ ["Evan", 35, "M", "python"],
140
+ ["Frank", 40, "M", "java"],
141
+ ["Grace", 27, "F", "python"],
142
+ ["Hank", 32, "O", "cpp"],
143
+ ["Ivy", 24, "F", "java"],
144
+ ["Jack", 29, "M", "python"]
145
+ ]
146
+ # DataFrame input
147
+ df_input = gr.Dataframe(
148
+ value=initial_data,
149
+ headers=["name", "age", "gender", "language"],
150
+ datatype=["str", "number", "str", "str"],
151
+ row_count=10,
152
+ column_count=4,
153
+ label="Dataframe"
154
+ )
155
+
156
+ with gr.Tab("Video"):
157
+ gr.Video("https://gradio-static-files.s3.us-west-2.amazonaws.com/world.mp4")
158
+ with gr.Tab("Gallery"):
159
+ gr.Gallery(
160
+ [
161
+ (
162
+ "https://gradio-static-files.s3.us-west-2.amazonaws.com/lion.jpg",
163
+ "lion",
164
+ ),
165
+ (
166
+ "https://gradio-static-files.s3.us-west-2.amazonaws.com/logo.png",
167
+ "logo",
168
+ ),
169
+ ]
170
+ )
171
+ with gr.Tab("Image Slider"):
172
+ gr.ImageSlider(
173
+ (
174
+ "https://gradio-static-files.s3.us-west-2.amazonaws.com/lion.jpg",
175
+ "https://gradio-static-files.s3.us-west-2.amazonaws.com/tower.jpg"
176
+ ),
177
+ label="Image Slider"
178
  )
179
 
180
  with gr.Row():
181
  with gr.Column(scale=2):
182
+ # Initialize Chatbot with conversation history as dictionaries
183
+ chatbot = gr.Chatbot(
184
+ value=[
185
+ {"role": "user", "content": "Hello!"},
186
+ {"role": "assistant", "content": "Hi there! How can I help?"}
187
+ ],
188
+ label="Chatbot"
189
+ )
190
  chat_btn = gr.Button("Add messages")
191
 
192
+ # Function to add messages to chat history
193
+ def add_message(history):
194
+ time.sleep(1)
195
+ # Append new messages
196
+ history = history + [
197
+ {"role": "user", "content": "How are you?"},
198
+ {"role": "assistant", "content": "I am good."}
199
+ ]
200
+ return history
201
+
202
+ # Connect button to update chatbot
203
  chat_btn.click(
204
+ add_message,
205
+ inputs=chatbot,
206
+ outputs=chatbot
 
 
207
  )
208
+
209
  with gr.Column(scale=1):
210
  with gr.Accordion("Advanced Settings"):
211
+ gr.Markdown("Hello Accordian!")
212
  gr.Number(label="Chatbot control 1")
213
  gr.Number(label="Chatbot control 2")
214
  gr.Number(label="Chatbot control 3")
215
+
216
+ with gr.Row():
217
+ gr.FileExplorer()
218
+ gr.HTML("<h1>This is HTML Heading 1</h1>")
219
+ gr.Code(
220
+ value="""# hello.py\n\n# Prints a message\n\n print('Gradio is awesome!')""",
221
+ language="python",
222
+ label="Code"
223
+ )
224
+
225
+
226
+ with gr.Row():
227
+ with gr.Column(scale=1):
228
+ # Plot
229
+ def make_plot(frequency=5, sample_rate=8000, sample_count=8000):
230
+ x = np.arange(sample_count)
231
+ y = np.sin(2 * np.pi * frequency * x / sample_rate)
232
+ plt.figure(figsize=(6, 3))
233
+ plt.plot(x, y)
234
+ plt.title(f"Sine wave: {frequency} Hz")
235
+ plt.xlabel("Sample index")
236
+ plt.ylabel("Amplitude")
237
+ return plt # returning a matplotlib plot
238
+
239
+ freq = gr.Slider(1, 20, value=5, label="Sine frequency (Hz)")
240
+ plot = gr.Plot(label="SW Plot")
241
+ freq.change(fn=make_plot, inputs=freq, outputs=plot)
242
+
243
+ with gr.Column(scale=2):
244
+ # Plot DataFrame
245
+ df = pd.DataFrame({
246
+ 'height': np.random.randint(50, 70, 25),
247
+ 'weight': np.random.randint(120, 320, 25),
248
+ 'age': np.random.randint(18, 65, 25),
249
+ 'ethnicity': [random.choice(["white", "black", "asian"]) for _ in range(25)]
250
+ })
251
+
252
+ with gr.Row():
253
+ ethnicity = gr.Dropdown(["all", "white", "black", "asian"], value="all", label="Ethnicity")
254
+ max_age = gr.Slider(18, 65, value=50, label="Max Age")
255
+
256
+ with gr.Row():
257
+ def filtered_df(ethnic, age):
258
+ _df = df if ethnic == "all" else df[df["ethnicity"] == ethnic]
259
+ _df = _df[_df["age"] < age]
260
+ return _df
261
 
262
+ gr.ScatterPlot(filtered_df, inputs=[ethnicity, max_age], x="weight", y="height", title="Weight x Height", label="Scatter Plot")
263
+ gr.LinePlot(filtered_df, inputs=[ethnicity, max_age], x="age", y="height", title="Age x Height", label="Line Plot")
264
 
265
  if __name__ == "__main__":
266
+ demo.queue().launch(theme=aurora, css=aurora_css)