Alysha Creelman
commited on
Commit
Β·
21ebf11
1
Parent(s):
918a9fb
Going back to the dropdown
Browse files
app.py
CHANGED
|
@@ -2,11 +2,9 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# Inference client setup
|
| 8 |
-
|
| 9 |
-
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=token)
|
| 10 |
pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dtype=torch.bfloat16, device_map="auto")
|
| 11 |
|
| 12 |
# Global flag to handle cancellation
|
|
@@ -95,6 +93,7 @@ custom_css = """
|
|
| 95 |
background: #cdebc5;
|
| 96 |
font-family: 'Comic Neue', sans-serif;
|
| 97 |
}
|
|
|
|
| 98 |
.gradio-container {
|
| 99 |
max-width: 700px;
|
| 100 |
margin: 0 auto;
|
|
@@ -103,6 +102,7 @@ custom_css = """
|
|
| 103 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 104 |
border-radius: 10px;
|
| 105 |
}
|
|
|
|
| 106 |
.gr-button {
|
| 107 |
background-color: #a7e0fd;
|
| 108 |
color: light blue;
|
|
@@ -112,60 +112,45 @@ custom_css = """
|
|
| 112 |
cursor: pointer;
|
| 113 |
transition: background-color 0.3s ease;
|
| 114 |
}
|
|
|
|
| 115 |
.gr-button:hover {
|
| 116 |
background-color: #45a049;
|
| 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;
|
| 132 |
-
height: auto;
|
| 133 |
-
display: block;
|
| 134 |
-
margin: 0 auto;
|
| 135 |
-
}
|
| 136 |
"""
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
if level == "Elementary School":
|
| 141 |
-
return "Your name is Wormington. You are a friendly Chatbot that can help answer questions from elementary school students. Please respond with the vocabulary that a seven-year-old can understand."
|
| 142 |
-
elif level == "Middle School":
|
| 143 |
-
return "Your name is Wormington. You are a friendly Chatbot that can help answer questions from middle school students. Please respond at a level that middle schoolers can understand."
|
| 144 |
-
elif level == "High School":
|
| 145 |
-
return "Your name is Wormington. You are a friendly Chatbot that can help answer questions from high school students. Please respond at a level that a high schooler can understand."
|
| 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.
|
| 153 |
-
gr.Markdown("
|
| 154 |
-
|
| 155 |
-
with gr.Row():
|
| 156 |
-
elementary_button = gr.Button("Elementary School", elem_id="elementary", variant="primary")
|
| 157 |
-
middle_button = gr.Button("Middle School", elem_id="middle", variant="primary")
|
| 158 |
-
high_button = gr.Button("High School", elem_id="high", variant="primary")
|
| 159 |
-
college_button = gr.Button("College", elem_id="college", variant="primary")
|
| 160 |
|
| 161 |
-
# Display area for the selected system message
|
| 162 |
-
system_message_display = gr.Textbox(label="System Message", value="", interactive=False)
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
|
@@ -178,16 +163,16 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 178 |
|
| 179 |
chat_history = gr.Chatbot(label="Chat")
|
| 180 |
|
| 181 |
-
user_input = gr.Textbox(show_label=False, placeholder="
|
| 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,
|
| 187 |
|
| 188 |
cancel_button.click(cancel_inference)
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
-
demo.launch(share=False) # Remove share=True because it's not supported on HF Spaces
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
|
| 6 |
+
# Inference client setup
|
| 7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
| 8 |
pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dtype=torch.bfloat16, device_map="auto")
|
| 9 |
|
| 10 |
# Global flag to handle cancellation
|
|
|
|
| 93 |
background: #cdebc5;
|
| 94 |
font-family: 'Comic Neue', sans-serif;
|
| 95 |
}
|
| 96 |
+
|
| 97 |
.gradio-container {
|
| 98 |
max-width: 700px;
|
| 99 |
margin: 0 auto;
|
|
|
|
| 102 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 103 |
border-radius: 10px;
|
| 104 |
}
|
| 105 |
+
|
| 106 |
.gr-button {
|
| 107 |
background-color: #a7e0fd;
|
| 108 |
color: light blue;
|
|
|
|
| 112 |
cursor: pointer;
|
| 113 |
transition: background-color 0.3s ease;
|
| 114 |
}
|
| 115 |
+
|
| 116 |
.gr-button:hover {
|
| 117 |
background-color: #45a049;
|
| 118 |
}
|
| 119 |
+
|
| 120 |
.gr-slider input {
|
| 121 |
color: #4CAF50;
|
| 122 |
}
|
| 123 |
+
|
| 124 |
.gr-chat {
|
| 125 |
font-size: 16px;
|
| 126 |
}
|
| 127 |
+
|
| 128 |
#title {
|
| 129 |
text-align: center;
|
| 130 |
font-size: 2em;
|
| 131 |
margin-bottom: 20px;
|
| 132 |
color: #a7e0fd;
|
| 133 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
"""
|
| 135 |
|
| 136 |
+
|
| 137 |
+
# Define the interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
with gr.Blocks(css=custom_css) as demo:
|
| 139 |
gr.Markdown("<h2 style='text-align: center;'>πβοΈ School AI Chatbot βοΈπ</h2>")
|
| 140 |
+
gr.Markdown("<h1 style='text-align: center;'>π</h1>")
|
| 141 |
+
gr.Markdown("Interact with Wormington Scholar π by selecting the appropriate level below.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
|
| 145 |
+
with gr.Row():
|
| 146 |
+
system_message = gr.Dropdown(
|
| 147 |
+
choices=["You are a friendly Chatbot that responds with the vocabulary of the seven year old.",
|
| 148 |
+
"You are a friendly Chatbot. Please respond at a level that middle schoolers can understand",
|
| 149 |
+
"You are a friendly high school Chatbot who responds at a level the average person can understand.",
|
| 150 |
+
"You are a friendly Chatbot that uses a very advanced, college-level vocabulary in your responses."],
|
| 151 |
+
label="System message",
|
| 152 |
+
interactive=True
|
| 153 |
+
)
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
|
|
|
| 163 |
|
| 164 |
chat_history = gr.Chatbot(label="Chat")
|
| 165 |
|
| 166 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
| 167 |
|
| 168 |
cancel_button = gr.Button("Cancel Inference", variant="danger")
|
| 169 |
|
| 170 |
# Adjusted to ensure history is maintained and passed correctly
|
| 171 |
+
user_input.submit(respond, [user_input, chat_history, system_message, max_tokens, temperature, top_p, use_local_model], chat_history)
|
| 172 |
|
| 173 |
cancel_button.click(cancel_inference)
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
demo.launch(share=False) # Remove share=True because it's not supported on HF Spaces
|