Spaces:
Sleeping
Sleeping
add three new ‘Slangify’ chat styles and remove the previous TA’s functions.
Browse filesadd
Pirate Speak → Nautical Marauder
Shakespeare (Old English) → Elizabethan Prose
1337 Speak → Cyber Elite
Normal → Standard Conversational
app.py
CHANGED
|
@@ -10,15 +10,93 @@ pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dty
|
|
| 10 |
# Global flag to handle cancellation
|
| 11 |
stop_inference = False
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
global stop_inference
|
| 23 |
stop_inference = False # Reset cancellation flag
|
| 24 |
|
|
@@ -26,137 +104,55 @@ def respond(
|
|
| 26 |
if history is None:
|
| 27 |
history = []
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
if stop_inference:
|
| 48 |
-
response = "Inference cancelled."
|
| 49 |
-
yield history + [(message, response)]
|
| 50 |
-
return
|
| 51 |
-
token = output['generated_text'][-1]['content']
|
| 52 |
-
response += token
|
| 53 |
-
yield history + [(message, response)] # Yield history + new response
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
# API-based inference
|
| 57 |
-
messages = [{"role": "system", "content": system_message}]
|
| 58 |
-
for val in history:
|
| 59 |
-
if val[0]:
|
| 60 |
-
messages.append({"role": "user", "content": val[0]})
|
| 61 |
-
if val[1]:
|
| 62 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 63 |
-
messages.append({"role": "user", "content": message})
|
| 64 |
-
|
| 65 |
-
response = ""
|
| 66 |
-
for message_chunk in client.chat_completion(
|
| 67 |
-
messages,
|
| 68 |
-
max_tokens=max_tokens,
|
| 69 |
-
stream=True,
|
| 70 |
-
temperature=temperature,
|
| 71 |
-
top_p=top_p,
|
| 72 |
-
):
|
| 73 |
-
if stop_inference:
|
| 74 |
-
response = "Inference cancelled."
|
| 75 |
-
yield history + [(message, response)]
|
| 76 |
-
return
|
| 77 |
-
if stop_inference:
|
| 78 |
-
response = "Inference cancelled."
|
| 79 |
-
break
|
| 80 |
-
token = message_chunk.choices[0].delta.content
|
| 81 |
-
response += token
|
| 82 |
-
yield history + [(message, response)] # Yield history + new response
|
| 83 |
|
| 84 |
|
| 85 |
def cancel_inference():
|
| 86 |
global stop_inference
|
| 87 |
stop_inference = True
|
| 88 |
|
| 89 |
-
# Custom CSS for a fancy look
|
| 90 |
-
custom_css = """
|
| 91 |
-
#main-container {
|
| 92 |
-
background-color: #f0f0f0;
|
| 93 |
-
font-family: 'Arial', sans-serif;
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
.gradio-container {
|
| 97 |
-
max-width: 700px;
|
| 98 |
-
margin: 0 auto;
|
| 99 |
-
padding: 20px;
|
| 100 |
-
background: white;
|
| 101 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 102 |
-
border-radius: 10px;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
.gr-button {
|
| 106 |
-
background-color: #4CAF50;
|
| 107 |
-
color: white;
|
| 108 |
-
border: none;
|
| 109 |
-
border-radius: 5px;
|
| 110 |
-
padding: 10px 20px;
|
| 111 |
-
cursor: pointer;
|
| 112 |
-
transition: background-color 0.3s ease;
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
.gr-button:hover {
|
| 116 |
-
background-color: #45a049;
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
.gr-slider input {
|
| 120 |
-
color: #4CAF50;
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
.gr-chat {
|
| 124 |
-
font-size: 16px;
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
#title {
|
| 128 |
-
text-align: center;
|
| 129 |
-
font-size: 2em;
|
| 130 |
-
margin-bottom: 20px;
|
| 131 |
-
color: #333;
|
| 132 |
-
}
|
| 133 |
-
"""
|
| 134 |
-
|
| 135 |
# Define the interface
|
| 136 |
-
with gr.Blocks(
|
| 137 |
gr.Markdown("<h1 style='text-align: center;'>🌟 Fancy AI Chatbot 🌟</h1>")
|
| 138 |
gr.Markdown("Interact with the AI chatbot using customizable settings below.")
|
| 139 |
|
| 140 |
-
with gr.Row():
|
| 141 |
-
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message", interactive=True)
|
| 142 |
-
use_local_model = gr.Checkbox(label="Use Local Model", value=False)
|
| 143 |
-
|
| 144 |
-
with gr.Row():
|
| 145 |
-
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 146 |
-
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 147 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 148 |
-
|
| 149 |
chat_history = gr.Chatbot(label="Chat")
|
| 150 |
|
| 151 |
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
| 152 |
|
| 153 |
cancel_button = gr.Button("Cancel Inference", variant="danger")
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
|
|
|
|
|
|
|
|
|
| 158 |
cancel_button.click(cancel_inference)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch(share=False) # Remove share=True because it's not supported on HF Spaces
|
| 162 |
-
|
|
|
|
| 10 |
# Global flag to handle cancellation
|
| 11 |
stop_inference = False
|
| 12 |
|
| 13 |
+
def style_response(style, response):
|
| 14 |
+
"""Modify response style based on the selected style."""
|
| 15 |
+
if style == "Nautical Marauder":
|
| 16 |
+
response = response.replace("you", "ye").replace("hello", "ahoy").replace("friend", "matey")
|
| 17 |
+
response = response.replace("is", "be").replace("my", "me").replace("the", "th'").replace("am", "be")
|
| 18 |
+
elif style == "Elizabethan Prose":
|
| 19 |
+
response = response.replace("you", "thou").replace("are", "art").replace("is", "be").replace("my", "mine")
|
| 20 |
+
response = response.replace("your", "thy").replace("the", "thee").replace("has", "hath").replace("do", "doth")
|
| 21 |
+
elif style == "Cyber Elite":
|
| 22 |
+
response = response.replace("e", "3").replace("a", "4").replace("t", "7").replace("o", "0").replace("i", "1")
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
def get_css(style):
|
| 26 |
+
"""Return corresponding CSS based on the selected style."""
|
| 27 |
+
if style == "Nautical Marauder":
|
| 28 |
+
return """
|
| 29 |
+
body {
|
| 30 |
+
background-color: #2b2b2b;
|
| 31 |
+
font-family: 'Trebuchet MS', sans-serif;
|
| 32 |
+
color: #f4e9c9;
|
| 33 |
+
background-image: url('https://www.transparenttextures.com/patterns/old-map.png');
|
| 34 |
+
}
|
| 35 |
+
.gradio-container {
|
| 36 |
+
background: rgba(0, 0, 0, 0.7);
|
| 37 |
+
border: 2px solid #d4af37;
|
| 38 |
+
box-shadow: 0 4px 8px rgba(255, 255, 255, 0.1);
|
| 39 |
+
}
|
| 40 |
+
.gr-chat {
|
| 41 |
+
font-size: 16px;
|
| 42 |
+
color: #f4e9c9;
|
| 43 |
+
}
|
| 44 |
+
"""
|
| 45 |
+
elif style == "Elizabethan Prose":
|
| 46 |
+
return """
|
| 47 |
+
body {
|
| 48 |
+
background-color: #f5f0e1;
|
| 49 |
+
font-family: 'Dancing Script', cursive;
|
| 50 |
+
color: #5c4033;
|
| 51 |
+
background-image: url('https://www.transparenttextures.com/patterns/old-paper.png');
|
| 52 |
+
}
|
| 53 |
+
.gradio-container {
|
| 54 |
+
background: rgba(255, 255, 255, 0.9);
|
| 55 |
+
border: 2px solid #a0522d;
|
| 56 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 57 |
+
}
|
| 58 |
+
.gr-chat {
|
| 59 |
+
font-size: 18px;
|
| 60 |
+
color: #5c4033;
|
| 61 |
+
}
|
| 62 |
+
"""
|
| 63 |
+
elif style == "Cyber Elite":
|
| 64 |
+
return """
|
| 65 |
+
body {
|
| 66 |
+
background-color: #000000;
|
| 67 |
+
font-family: 'Courier New', Courier, monospace;
|
| 68 |
+
color: #00ff00;
|
| 69 |
+
}
|
| 70 |
+
.gradio-container {
|
| 71 |
+
background: #1a1a1a;
|
| 72 |
+
border: 2px solid #00ff00;
|
| 73 |
+
box-shadow: 0 4px 8px rgba(0, 255, 0, 0.3);
|
| 74 |
+
}
|
| 75 |
+
.gr-chat {
|
| 76 |
+
font-size: 16px;
|
| 77 |
+
color: #00ff00;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
else:
|
| 81 |
+
# Default style
|
| 82 |
+
return """
|
| 83 |
+
body {
|
| 84 |
+
background-color: #f0f0f0;
|
| 85 |
+
font-family: 'Arial', sans-serif;
|
| 86 |
+
color: #333;
|
| 87 |
+
}
|
| 88 |
+
.gradio-container {
|
| 89 |
+
background: white;
|
| 90 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 91 |
+
border-radius: 10px;
|
| 92 |
+
}
|
| 93 |
+
.gr-chat {
|
| 94 |
+
font-size: 16px;
|
| 95 |
+
color: #333;
|
| 96 |
+
}
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
def respond(message, history: list[tuple[str, str]], style="Standard Conversational"):
|
| 100 |
global stop_inference
|
| 101 |
stop_inference = False # Reset cancellation flag
|
| 102 |
|
|
|
|
| 104 |
if history is None:
|
| 105 |
history = []
|
| 106 |
|
| 107 |
+
# API-based inference
|
| 108 |
+
messages = [{"role": "user", "content": message}]
|
| 109 |
+
|
| 110 |
+
response = ""
|
| 111 |
+
for message_chunk in client.chat_completion(
|
| 112 |
+
messages,
|
| 113 |
+
max_tokens=512, # Default max tokens for response
|
| 114 |
+
stream=True,
|
| 115 |
+
temperature=0.7, # Default temperature
|
| 116 |
+
top_p=0.95, # Default top-p
|
| 117 |
+
):
|
| 118 |
+
if stop_inference:
|
| 119 |
+
response = "Inference cancelled."
|
| 120 |
+
yield history + [(message, response)]
|
| 121 |
+
return
|
| 122 |
+
token = message_chunk.choices[0].delta.content
|
| 123 |
+
response += token
|
| 124 |
+
yield history + [(message, style_response(style, response))] # Apply selected style to the response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
def cancel_inference():
|
| 128 |
global stop_inference
|
| 129 |
stop_inference = True
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# Define the interface
|
| 132 |
+
with gr.Blocks() as demo:
|
| 133 |
gr.Markdown("<h1 style='text-align: center;'>🌟 Fancy AI Chatbot 🌟</h1>")
|
| 134 |
gr.Markdown("Interact with the AI chatbot using customizable settings below.")
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
chat_history = gr.Chatbot(label="Chat")
|
| 137 |
|
| 138 |
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
| 139 |
|
| 140 |
cancel_button = gr.Button("Cancel Inference", variant="danger")
|
| 141 |
|
| 142 |
+
# New feature: Style selection with more formal names
|
| 143 |
+
style_selection = gr.Dropdown(
|
| 144 |
+
label="Response Style",
|
| 145 |
+
choices=["Standard Conversational", "Nautical Marauder", "Elizabethan Prose", "Cyber Elite"],
|
| 146 |
+
value="Standard Conversational"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
def apply_css(style):
|
| 150 |
+
return get_css(style)
|
| 151 |
|
| 152 |
+
# Adjusted to ensure history is maintained and passed correctly
|
| 153 |
+
user_input.submit(respond, [user_input, chat_history, style_selection], chat_history)
|
| 154 |
+
style_selection.change(apply_css, style_selection, gr.CSS())
|
| 155 |
cancel_button.click(cancel_inference)
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
demo.launch(share=False) # Remove share=True because it's not supported on HF Spaces
|
|
|