Alysha Creelman
commited on
Commit
Β·
31b8d70
1
Parent(s):
f567464
Fixed buttons by making it so they cant be clicked multiple times before reset
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import torch
|
4 |
from transformers import pipeline
|
@@ -12,7 +12,6 @@ pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dty
|
|
12 |
# Global flag to handle cancellation
|
13 |
stop_inference = False
|
14 |
|
15 |
-
|
16 |
def respond(
|
17 |
message,
|
18 |
history: list[tuple[str, str]],
|
@@ -77,9 +76,6 @@ def respond(
|
|
77 |
response = "Inference cancelled."
|
78 |
yield history + [(message, response)]
|
79 |
return
|
80 |
-
if stop_inference:
|
81 |
-
response = "Inference cancelled."
|
82 |
-
break
|
83 |
token = message_chunk.choices[0].delta.content
|
84 |
response += token
|
85 |
yield history + [(message, response)] # Yield history + new response
|
@@ -89,7 +85,7 @@ def cancel_inference():
|
|
89 |
global stop_inference
|
90 |
stop_inference = True
|
91 |
|
92 |
-
# Custom CSS
|
93 |
custom_css = """
|
94 |
#main-container {
|
95 |
background: #cdebc5;
|
@@ -112,20 +108,9 @@ custom_css = """
|
|
112 |
cursor: pointer;
|
113 |
transition: background-color 0.3s ease;
|
114 |
}
|
115 |
-
.gr-button:
|
116 |
-
background-color:
|
117 |
-
|
118 |
-
.gr-slider input {
|
119 |
-
color: #4CAF50;
|
120 |
-
}
|
121 |
-
.gr-chat {
|
122 |
-
font-size: 16px;
|
123 |
-
}
|
124 |
-
#title {
|
125 |
-
text-align: center;
|
126 |
-
font-size: 2em;
|
127 |
-
margin-bottom: 20px;
|
128 |
-
color: #a7e0fd;
|
129 |
}
|
130 |
#school_ai_image {
|
131 |
width: 150px;
|
@@ -146,11 +131,22 @@ def update_system_message(level):
|
|
146 |
elif level == "College":
|
147 |
return "Your name is Wormington. You are a friendly Chatbot that can help answer questions from college students. Please respond using very advanced, college-level vocabulary."
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
# Define interface
|
150 |
with gr.Blocks(css=custom_css) as demo:
|
151 |
gr.Markdown("<h2 style='text-align: center;'>πβοΈ School AI Chatbot βοΈπ</h2>")
|
152 |
gr.Image("wormington_headshot.jpg", elem_id="school_ai_image", show_label=False, interactive=False)
|
153 |
-
gr.Markdown("<h1 style= 'text-align: center;'>Interact with Wormington Scholar π by selecting the appropriate level below
|
154 |
|
155 |
with gr.Row():
|
156 |
elementary_button = gr.Button("Elementary School", elem_id="elementary", variant="primary")
|
@@ -161,15 +157,25 @@ with gr.Blocks(css=custom_css) as demo:
|
|
161 |
# Display area for the selected system message
|
162 |
system_message_display = gr.Textbox(label="System Message", value="", interactive=False)
|
163 |
|
164 |
-
#
|
165 |
-
elementary_button.click(fn=lambda:
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
with gr.Row():
|
171 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
172 |
-
|
173 |
|
174 |
with gr.Row():
|
175 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
@@ -181,13 +187,17 @@ with gr.Blocks(css=custom_css) as demo:
|
|
181 |
user_input = gr.Textbox(show_label=False, placeholder="Wormington would love to answer your questions. Type them here:")
|
182 |
|
183 |
cancel_button = gr.Button("Cancel Inference", variant="danger")
|
|
|
184 |
|
185 |
# Adjusted to ensure history is maintained and passed correctly
|
186 |
user_input.submit(respond, [user_input, chat_history, system_message_display, max_tokens, temperature, top_p, use_local_model], chat_history)
|
187 |
|
188 |
cancel_button.click(cancel_inference)
|
189 |
|
190 |
-
|
|
|
|
|
|
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
-
demo.launch(share=False)
|
|
|
1 |
+
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import torch
|
4 |
from transformers import pipeline
|
|
|
12 |
# Global flag to handle cancellation
|
13 |
stop_inference = False
|
14 |
|
|
|
15 |
def respond(
|
16 |
message,
|
17 |
history: list[tuple[str, str]],
|
|
|
76 |
response = "Inference cancelled."
|
77 |
yield history + [(message, response)]
|
78 |
return
|
|
|
|
|
|
|
79 |
token = message_chunk.choices[0].delta.content
|
80 |
response += token
|
81 |
yield history + [(message, response)] # Yield history + new response
|
|
|
85 |
global stop_inference
|
86 |
stop_inference = True
|
87 |
|
88 |
+
# Custom CSS to disable buttons visually
|
89 |
custom_css = """
|
90 |
#main-container {
|
91 |
background: #cdebc5;
|
|
|
108 |
cursor: pointer;
|
109 |
transition: background-color 0.3s ease;
|
110 |
}
|
111 |
+
.gr-button:disabled {
|
112 |
+
background-color: grey;
|
113 |
+
cursor: not-allowed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
#school_ai_image {
|
116 |
width: 150px;
|
|
|
131 |
elif level == "College":
|
132 |
return "Your name is Wormington. You are a friendly Chatbot that can help answer questions from college students. Please respond using very advanced, college-level vocabulary."
|
133 |
|
134 |
+
# Disable all buttons after one is clicked
|
135 |
+
def disable_buttons_and_update_message(level):
|
136 |
+
system_message = update_system_message(level)
|
137 |
+
# Update button states to disabled
|
138 |
+
return system_message, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
139 |
+
|
140 |
+
# Restart function to refresh the app
|
141 |
+
def restart_chatbot():
|
142 |
+
# Reset buttons and clear system message display
|
143 |
+
return gr.update(value="", interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
|
144 |
+
|
145 |
# Define interface
|
146 |
with gr.Blocks(css=custom_css) as demo:
|
147 |
gr.Markdown("<h2 style='text-align: center;'>πβοΈ School AI Chatbot βοΈπ</h2>")
|
148 |
gr.Image("wormington_headshot.jpg", elem_id="school_ai_image", show_label=False, interactive=False)
|
149 |
+
gr.Markdown("<h1 style= 'text-align: center;'>Interact with Wormington Scholar π by selecting the appropriate level below.</h1>")
|
150 |
|
151 |
with gr.Row():
|
152 |
elementary_button = gr.Button("Elementary School", elem_id="elementary", variant="primary")
|
|
|
157 |
# Display area for the selected system message
|
158 |
system_message_display = gr.Textbox(label="System Message", value="", interactive=False)
|
159 |
|
160 |
+
# Disable buttons and update the system message when a button is clicked
|
161 |
+
elementary_button.click(fn=lambda: disable_buttons_and_update_message("Elementary School"),
|
162 |
+
inputs=None,
|
163 |
+
outputs=[system_message_display, elementary_button, middle_button, high_button, college_button])
|
164 |
+
|
165 |
+
middle_button.click(fn=lambda: disable_buttons_and_update_message("Middle School"),
|
166 |
+
inputs=None,
|
167 |
+
outputs=[system_message_display, elementary_button, middle_button, high_button, college_button])
|
168 |
+
|
169 |
+
high_button.click(fn=lambda: disable_buttons_and_update_message("High School"),
|
170 |
+
inputs=None,
|
171 |
+
outputs=[system_message_display, elementary_button, middle_button, high_button, college_button])
|
172 |
+
|
173 |
+
college_button.click(fn=lambda: disable_buttons_and_update_message("College"),
|
174 |
+
inputs=None,
|
175 |
+
outputs=[system_message_display, elementary_button, middle_button, high_button, college_button])
|
176 |
|
177 |
with gr.Row():
|
178 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
|
|
179 |
|
180 |
with gr.Row():
|
181 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
|
|
187 |
user_input = gr.Textbox(show_label=False, placeholder="Wormington would love to answer your questions. Type them here:")
|
188 |
|
189 |
cancel_button = gr.Button("Cancel Inference", variant="danger")
|
190 |
+
restart_button = gr.Button("Restart Chatbot", variant="secondary")
|
191 |
|
192 |
# Adjusted to ensure history is maintained and passed correctly
|
193 |
user_input.submit(respond, [user_input, chat_history, system_message_display, max_tokens, temperature, top_p, use_local_model], chat_history)
|
194 |
|
195 |
cancel_button.click(cancel_inference)
|
196 |
|
197 |
+
# Reset the buttons when the "Restart Chatbot" button is clicked
|
198 |
+
restart_button.click(fn=restart_chatbot,
|
199 |
+
inputs=None,
|
200 |
+
outputs=[system_message_display, elementary_button, middle_button, high_button, college_button])
|
201 |
|
202 |
if __name__ == "__main__":
|
203 |
+
demo.launch(share=False)
|