Spaces:
Runtime error
Runtime error
Alysha Creelman
commited on
Commit
Β·
f567464
1
Parent(s):
21ebf11
Putting in button code again
Browse files
app.py
CHANGED
|
@@ -2,9 +2,11 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
|
| 6 |
-
# Inference client setup
|
| 7 |
-
|
|
|
|
| 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,7 +95,6 @@ custom_css = """
|
|
| 93 |
background: #cdebc5;
|
| 94 |
font-family: 'Comic Neue', sans-serif;
|
| 95 |
}
|
| 96 |
-
|
| 97 |
.gradio-container {
|
| 98 |
max-width: 700px;
|
| 99 |
margin: 0 auto;
|
|
@@ -102,7 +103,6 @@ custom_css = """
|
|
| 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,45 +112,60 @@ custom_css = """
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
with gr.Blocks(css=custom_css) as demo:
|
| 139 |
gr.Markdown("<h2 style='text-align: center;'>πβοΈ School AI Chatbot βοΈπ</h2>")
|
| 140 |
-
gr.
|
| 141 |
-
gr.Markdown("Interact with Wormington Scholar π by selecting the appropriate level below.")
|
| 142 |
-
|
| 143 |
|
| 144 |
-
|
| 145 |
with gr.Row():
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
|
@@ -163,16 +178,16 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 163 |
|
| 164 |
chat_history = gr.Chatbot(label="Chat")
|
| 165 |
|
| 166 |
-
user_input = gr.Textbox(show_label=False, placeholder="
|
| 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,
|
| 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
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Inference client setup with token from environment
|
| 8 |
+
token = os.getenv('HF_TOKEN')
|
| 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 |
background: #cdebc5;
|
| 96 |
font-family: 'Comic Neue', sans-serif;
|
| 97 |
}
|
|
|
|
| 98 |
.gradio-container {
|
| 99 |
max-width: 700px;
|
| 100 |
margin: 0 auto;
|
|
|
|
| 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 |
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 |
+
# Define system messages for each level
|
| 139 |
+
def update_system_message(level):
|
| 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.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")
|
| 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 |
+
# Update the system message when a button is clicked
|
| 165 |
+
elementary_button.click(fn=lambda: update_system_message("Elementary School"), inputs=None, outputs=system_message_display)
|
| 166 |
+
middle_button.click(fn=lambda: update_system_message("Middle School"), inputs=None, outputs=system_message_display)
|
| 167 |
+
high_button.click(fn=lambda: update_system_message("High School"), inputs=None, outputs=system_message_display)
|
| 168 |
+
college_button.click(fn=lambda: update_system_message("College"), inputs=None, outputs=system_message_display)
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
|
|
|
| 178 |
|
| 179 |
chat_history = gr.Chatbot(label="Chat")
|
| 180 |
|
| 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) # Remove share=True because it's not supported on HF Spaces
|