Spaces:
Running
Running
upgraded ui!
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
import threading
|
| 2 |
import asyncio
|
| 3 |
import uuid
|
| 4 |
import gradio as gr
|
| 5 |
import edge_tts
|
| 6 |
from deep_translator import GoogleTranslator
|
| 7 |
-
|
| 8 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 9 |
import torch
|
| 10 |
|
| 11 |
-
# =====
|
| 12 |
|
| 13 |
voice_characters = {
|
| 14 |
"English - US": {
|
|
@@ -56,7 +54,7 @@ def get_characters(language):
|
|
| 56 |
default_char = chars[0] if chars else None
|
| 57 |
return gr.update(choices=chars, value=default_char)
|
| 58 |
|
| 59 |
-
# ===== Chatbot
|
| 60 |
|
| 61 |
model_name = "facebook/blenderbot-400M-distill"
|
| 62 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
|
@@ -73,13 +71,13 @@ def chatbot_response(history, user_message):
|
|
| 73 |
if history is None:
|
| 74 |
history = []
|
| 75 |
|
| 76 |
-
history.append(("
|
| 77 |
-
conversation_text = " ".join([msg for _, msg in history])
|
| 78 |
inputs = tokenizer([conversation_text], return_tensors="pt").to(device)
|
| 79 |
|
| 80 |
reply_ids = model.generate(**inputs, max_length=200)
|
| 81 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 82 |
-
history.append(("
|
| 83 |
|
| 84 |
chat_str = "\n".join([f"{speaker}: {msg}" for speaker, msg in history])
|
| 85 |
|
|
@@ -91,14 +89,119 @@ def chatbot_response(history, user_message):
|
|
| 91 |
|
| 92 |
return history, chat_str, audio_path
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# ===== UI =====
|
| 95 |
|
| 96 |
def create_app():
|
| 97 |
-
with gr.Blocks(css=
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
A simple and fun **Text-to-Speech + Translator + Chatbot** app!
|
| 101 |
-
""")
|
| 102 |
|
| 103 |
with gr.Tab("π§ Text to Speech + Translator"):
|
| 104 |
with gr.Row():
|
|
@@ -116,26 +219,45 @@ def create_app():
|
|
| 116 |
output_audio = gr.Audio(label="π Listen Here", autoplay=True)
|
| 117 |
|
| 118 |
language_dropdown.change(fn=get_characters, inputs=language_dropdown, outputs=character_dropdown)
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
with gr.Tab("π€ Chatbot"):
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Column(scale=2):
|
| 126 |
user_input = gr.Textbox(label="π¬ Ask Anything", lines=2, placeholder="Try: What's your name?")
|
| 127 |
-
chat_display = gr.Textbox(label="π Conversation", interactive=False, lines=15)
|
| 128 |
send_button = gr.Button("π© Send")
|
| 129 |
with gr.Column(scale=1):
|
| 130 |
audio_output = gr.Audio(label="π Bot's Voice Reply", autoplay=True)
|
| 131 |
|
| 132 |
chat_history = gr.State([])
|
| 133 |
-
send_button.click(fn=chatbot_response, inputs=[chat_history, user_input],
|
| 134 |
-
outputs=[chat_history, chat_display, audio_output])
|
| 135 |
-
user_input.submit(fn=chatbot_response, inputs=[chat_history, user_input],
|
| 136 |
-
outputs=[chat_history, chat_display, audio_output])
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
return app
|
| 141 |
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import uuid
|
| 3 |
import gradio as gr
|
| 4 |
import edge_tts
|
| 5 |
from deep_translator import GoogleTranslator
|
|
|
|
| 6 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 7 |
import torch
|
| 8 |
|
| 9 |
+
# ===== Voice setup =====
|
| 10 |
|
| 11 |
voice_characters = {
|
| 12 |
"English - US": {
|
|
|
|
| 54 |
default_char = chars[0] if chars else None
|
| 55 |
return gr.update(choices=chars, value=default_char)
|
| 56 |
|
| 57 |
+
# ===== Chatbot setup =====
|
| 58 |
|
| 59 |
model_name = "facebook/blenderbot-400M-distill"
|
| 60 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
|
|
|
| 71 |
if history is None:
|
| 72 |
history = []
|
| 73 |
|
| 74 |
+
history.append(("You", user_message))
|
| 75 |
+
conversation_text = " ".join([msg for _, msg in history])
|
| 76 |
inputs = tokenizer([conversation_text], return_tensors="pt").to(device)
|
| 77 |
|
| 78 |
reply_ids = model.generate(**inputs, max_length=200)
|
| 79 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 80 |
+
history.append(("Bot", response))
|
| 81 |
|
| 82 |
chat_str = "\n".join([f"{speaker}: {msg}" for speaker, msg in history])
|
| 83 |
|
|
|
|
| 89 |
|
| 90 |
return history, chat_str, audio_path
|
| 91 |
|
| 92 |
+
# ===== CSS with animations and glitch =====
|
| 93 |
+
|
| 94 |
+
custom_css = """
|
| 95 |
+
body {
|
| 96 |
+
background: linear-gradient(-45deg, #0f0c29, #302b63, #24243e, #000000);
|
| 97 |
+
background-size: 600% 600%;
|
| 98 |
+
animation: gradientBG 15s ease infinite;
|
| 99 |
+
font-family: 'Courier New', monospace;
|
| 100 |
+
color: #00FF00;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
.gradio-container {
|
| 104 |
+
border: 2px solid #00FF00;
|
| 105 |
+
padding: 20px;
|
| 106 |
+
box-shadow: 0 0 10px #00FF00;
|
| 107 |
+
border-radius: 10px;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
h1 {
|
| 111 |
+
position: relative;
|
| 112 |
+
font-family: 'Courier New', monospace;
|
| 113 |
+
color: #00FF00;
|
| 114 |
+
border-right: 2px solid #00FF00;
|
| 115 |
+
white-space: nowrap;
|
| 116 |
+
overflow: hidden;
|
| 117 |
+
width: fit-content;
|
| 118 |
+
animation: typewriter 3s steps(40, end), blinkCaret 0.8s step-end infinite;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
h1::after {
|
| 122 |
+
content: attr(data-text);
|
| 123 |
+
position: absolute;
|
| 124 |
+
top: 0;
|
| 125 |
+
left: 0;
|
| 126 |
+
width: 100%;
|
| 127 |
+
color: #ff00c8;
|
| 128 |
+
clip: rect(0, 900px, 0, 0);
|
| 129 |
+
animation: glitch 2s infinite linear alternate-reverse;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
@keyframes glitch {
|
| 133 |
+
0% { clip: rect(42px, 9999px, 44px, 0); transform: skew(0.5deg); }
|
| 134 |
+
5% { clip: rect(12px, 9999px, 15px, 0); transform: skew(0.3deg); }
|
| 135 |
+
10% { clip: rect(85px, 9999px, 88px, 0); transform: skew(0.2deg); }
|
| 136 |
+
15% { clip: rect(24px, 9999px, 27px, 0); transform: skew(0.4deg); }
|
| 137 |
+
20% { clip: rect(61px, 9999px, 65px, 0); transform: skew(0.2deg); }
|
| 138 |
+
25% { clip: rect(10px, 9999px, 14px, 0); transform: skew(0.3deg); }
|
| 139 |
+
30% { clip: rect(50px, 9999px, 53px, 0); transform: skew(0.1deg); }
|
| 140 |
+
35% { clip: rect(20px, 9999px, 24px, 0); transform: skew(0.5deg); }
|
| 141 |
+
40% { clip: rect(65px, 9999px, 69px, 0); transform: skew(0.3deg); }
|
| 142 |
+
100% { clip: rect(0, 9999px, 0, 0); transform: skew(0deg); }
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.gr-button {
|
| 146 |
+
background: black;
|
| 147 |
+
color: #00FF00 !important;
|
| 148 |
+
font-weight: bold;
|
| 149 |
+
border: 1px solid #00FF00;
|
| 150 |
+
box-shadow: 0 0 5px #00FF00;
|
| 151 |
+
animation: bounce 2s infinite;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.gr-textbox, .gr-dropdown {
|
| 155 |
+
background: black;
|
| 156 |
+
color: #00FF00;
|
| 157 |
+
border: 1px solid #00FF00;
|
| 158 |
+
box-shadow: inset 0 0 5px #00FF00;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.gr-audio {
|
| 162 |
+
box-shadow: 0 0 10px #00FF00;
|
| 163 |
+
border-radius: 10px;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
#chat_display {
|
| 167 |
+
background: black;
|
| 168 |
+
color: #00FF00;
|
| 169 |
+
padding: 10px;
|
| 170 |
+
border: 1px solid #00FF00;
|
| 171 |
+
box-shadow: inset 0 0 10px #00FF00;
|
| 172 |
+
font-family: 'Courier New', monospace;
|
| 173 |
+
white-space: pre-wrap;
|
| 174 |
+
animation: typewriter 3s steps(100, end);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
@keyframes typewriter {
|
| 178 |
+
from { width: 0 }
|
| 179 |
+
to { width: 100% }
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
@keyframes blinkCaret {
|
| 183 |
+
0%, 100% { border-color: transparent }
|
| 184 |
+
50% { border-color: #00FF00; }
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
@keyframes bounce {
|
| 188 |
+
0%, 100% { transform: translateY(0); }
|
| 189 |
+
50% { transform: translateY(-6px); }
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
@keyframes gradientBG {
|
| 193 |
+
0% {background-position: 0% 50%;}
|
| 194 |
+
50% {background-position: 100% 50%;}
|
| 195 |
+
100% {background-position: 0% 50%;}
|
| 196 |
+
}
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
# ===== UI =====
|
| 200 |
|
| 201 |
def create_app():
|
| 202 |
+
with gr.Blocks(css=custom_css) as app:
|
| 203 |
+
|
| 204 |
+
gr.HTML("<h1 data-text='π£οΈ SpeakEasy AI'>π£οΈ SpeakEasy AI</h1>")
|
|
|
|
|
|
|
| 205 |
|
| 206 |
with gr.Tab("π§ Text to Speech + Translator"):
|
| 207 |
with gr.Row():
|
|
|
|
| 219 |
output_audio = gr.Audio(label="π Listen Here", autoplay=True)
|
| 220 |
|
| 221 |
language_dropdown.change(fn=get_characters, inputs=language_dropdown, outputs=character_dropdown)
|
| 222 |
+
|
| 223 |
+
def tts_with_loading(*args):
|
| 224 |
+
yield "Processing...", None
|
| 225 |
+
yield tts_wrapper(*args)
|
| 226 |
+
|
| 227 |
+
tts_button.click(
|
| 228 |
+
fn=tts_with_loading,
|
| 229 |
+
inputs=[text_input, language_dropdown, character_dropdown, translation_dropdown],
|
| 230 |
+
outputs=[output_text, output_audio]
|
| 231 |
+
)
|
| 232 |
|
| 233 |
with gr.Tab("π€ Chatbot"):
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=2):
|
| 236 |
user_input = gr.Textbox(label="π¬ Ask Anything", lines=2, placeholder="Try: What's your name?")
|
| 237 |
+
chat_display = gr.Textbox(label="π Conversation", interactive=False, lines=15, elem_id="chat_display")
|
| 238 |
send_button = gr.Button("π© Send")
|
| 239 |
with gr.Column(scale=1):
|
| 240 |
audio_output = gr.Audio(label="π Bot's Voice Reply", autoplay=True)
|
| 241 |
|
| 242 |
chat_history = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
def chatbot_with_typing(history, user_message):
|
| 245 |
+
yield history, "Bot is typing...", None
|
| 246 |
+
yield chatbot_response(history, user_message)
|
| 247 |
+
|
| 248 |
+
send_button.click(
|
| 249 |
+
fn=chatbot_with_typing,
|
| 250 |
+
inputs=[chat_history, user_input],
|
| 251 |
+
outputs=[chat_history, chat_display, audio_output]
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
user_input.submit(
|
| 255 |
+
fn=chatbot_with_typing,
|
| 256 |
+
inputs=[chat_history, user_input],
|
| 257 |
+
outputs=[chat_history, chat_display, audio_output]
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
gr.HTML("<footer style='text-align:center;padding:10px;color:#00FF00;'>π§ Made with β€οΈ by Yogya using Gradio, Edge TTS, and Hugging Face π€</footer>")
|
| 261 |
|
| 262 |
return app
|
| 263 |
|