Spaces:
Sleeping
Sleeping
updated ui
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from deep_translator import GoogleTranslator
|
|
| 8 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 9 |
import torch
|
| 10 |
|
| 11 |
-
# ===== TTS
|
| 12 |
|
| 13 |
voice_characters = {
|
| 14 |
"English - US": {
|
|
@@ -17,7 +17,8 @@ voice_characters = {
|
|
| 17 |
"Guy": "en-US-GuyNeural",
|
| 18 |
},
|
| 19 |
"Hindi": {
|
| 20 |
-
"Swara": "hi-IN-SwaraNeural"
|
|
|
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
|
@@ -33,37 +34,29 @@ def tts_wrapper(text, language, character, translation_direction):
|
|
| 33 |
try:
|
| 34 |
original_text = text.strip()
|
| 35 |
if not original_text:
|
| 36 |
-
return "
|
| 37 |
|
| 38 |
if translation_direction == "English to Hindi":
|
| 39 |
text = GoogleTranslator(source='en', target='hi').translate(original_text)
|
| 40 |
elif translation_direction == "Hindi to English":
|
| 41 |
text = GoogleTranslator(source='hi', target='en').translate(original_text)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
if language == "Hindi":
|
| 45 |
-
voice = voice_characters["Hindi"]["Swara"]
|
| 46 |
-
else:
|
| 47 |
-
voice = voice_characters.get(language, {}).get(character)
|
| 48 |
-
|
| 49 |
if not voice:
|
| 50 |
-
return f"
|
| 51 |
|
| 52 |
filename = asyncio.run(generate_tts(text, voice))
|
| 53 |
return text, filename
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"Error: {str(e)}", None
|
| 57 |
|
| 58 |
def get_characters(language):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
chars = list(voice_characters.get(language, {}).keys())
|
| 63 |
-
default_char = chars[0] if chars else None
|
| 64 |
-
return gr.update(choices=chars, value=default_char, visible=True)
|
| 65 |
|
| 66 |
-
# =====
|
| 67 |
|
| 68 |
model_name = "facebook/blenderbot-400M-distill"
|
| 69 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
|
@@ -80,60 +73,69 @@ def chatbot_response(history, user_message):
|
|
| 80 |
if history is None:
|
| 81 |
history = []
|
| 82 |
|
| 83 |
-
history.append(("
|
| 84 |
conversation_text = " ".join([msg for _, msg in history]) + " " + user_message
|
| 85 |
inputs = tokenizer([conversation_text], return_tensors="pt").to(device)
|
| 86 |
|
| 87 |
reply_ids = model.generate(**inputs, max_length=200)
|
| 88 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
chat_str = ""
|
| 93 |
-
for speaker, msg in history:
|
| 94 |
-
chat_str += f"{speaker}: {msg}\n"
|
| 95 |
|
| 96 |
try:
|
| 97 |
audio_path = asyncio.run(generate_bot_tts(response))
|
| 98 |
except Exception as e:
|
| 99 |
audio_path = None
|
| 100 |
-
print(f"TTS
|
| 101 |
|
| 102 |
return history, chat_str, audio_path
|
| 103 |
|
| 104 |
-
# =====
|
| 105 |
|
| 106 |
def create_app():
|
| 107 |
-
with gr.Blocks() as app:
|
| 108 |
-
gr.Markdown("
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
language_dropdown.change(fn=get_characters, inputs=language_dropdown, outputs=character_dropdown)
|
| 120 |
-
|
| 121 |
tts_button.click(fn=tts_wrapper,
|
| 122 |
inputs=[text_input, language_dropdown, character_dropdown, translation_dropdown],
|
| 123 |
outputs=[output_text, output_audio])
|
| 124 |
|
| 125 |
-
with gr.Tab("Chatbot"):
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
user_input.submit(fn=respond, inputs=[user_input, chat_history], outputs=[chat_history, chat_display, audio_output])
|
| 137 |
|
| 138 |
return app
|
| 139 |
|
|
|
|
| 8 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 9 |
import torch
|
| 10 |
|
| 11 |
+
# ===== TTS Setup =====
|
| 12 |
|
| 13 |
voice_characters = {
|
| 14 |
"English - US": {
|
|
|
|
| 17 |
"Guy": "en-US-GuyNeural",
|
| 18 |
},
|
| 19 |
"Hindi": {
|
| 20 |
+
"Swara": "hi-IN-SwaraNeural",
|
| 21 |
+
"Madhur": "hi-IN-MadhurNeural"
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
|
|
|
| 34 |
try:
|
| 35 |
original_text = text.strip()
|
| 36 |
if not original_text:
|
| 37 |
+
return "β οΈ Please enter some text.", None
|
| 38 |
|
| 39 |
if translation_direction == "English to Hindi":
|
| 40 |
text = GoogleTranslator(source='en', target='hi').translate(original_text)
|
| 41 |
elif translation_direction == "Hindi to English":
|
| 42 |
text = GoogleTranslator(source='hi', target='en').translate(original_text)
|
| 43 |
|
| 44 |
+
voice = voice_characters.get(language, {}).get(character)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if not voice:
|
| 46 |
+
return f"β οΈ Voice '{character}' not available for '{language}'.", None
|
| 47 |
|
| 48 |
filename = asyncio.run(generate_tts(text, voice))
|
| 49 |
return text, filename
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"β Error: {str(e)}", None
|
| 53 |
|
| 54 |
def get_characters(language):
|
| 55 |
+
chars = list(voice_characters.get(language, {}).keys())
|
| 56 |
+
default_char = chars[0] if chars else None
|
| 57 |
+
return gr.update(choices=chars, value=default_char)
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# ===== Chatbot Setup =====
|
| 60 |
|
| 61 |
model_name = "facebook/blenderbot-400M-distill"
|
| 62 |
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
|
|
|
| 73 |
if history is None:
|
| 74 |
history = []
|
| 75 |
|
| 76 |
+
history.append(("π§", user_message))
|
| 77 |
conversation_text = " ".join([msg for _, msg in history]) + " " + user_message
|
| 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(("π€", response))
|
| 83 |
|
| 84 |
+
chat_str = "\n".join([f"{speaker}: {msg}" for speaker, msg in history])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
try:
|
| 87 |
audio_path = asyncio.run(generate_bot_tts(response))
|
| 88 |
except Exception as e:
|
| 89 |
audio_path = None
|
| 90 |
+
print(f"TTS failed: {e}")
|
| 91 |
|
| 92 |
return history, chat_str, audio_path
|
| 93 |
|
| 94 |
+
# ===== UI =====
|
| 95 |
|
| 96 |
def create_app():
|
| 97 |
+
with gr.Blocks(css="footer {text-align: center; padding: 10px;}") as app:
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
# π£οΈ SpeakEasy AI
|
| 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():
|
| 105 |
+
with gr.Column(scale=2):
|
| 106 |
+
text_input = gr.Textbox(label="π¬ Your Text", placeholder="Enter text here...", lines=4)
|
| 107 |
+
language_dropdown = gr.Dropdown(choices=list(voice_characters.keys()), value="English - US", label="π Language")
|
| 108 |
+
character_dropdown = gr.Dropdown(choices=list(voice_characters["English - US"].keys()), value="Aria", label="π§βπ€ Voice Character")
|
| 109 |
+
with gr.Accordion("π Translation Options", open=False):
|
| 110 |
+
translation_dropdown = gr.Dropdown(choices=["None", "English to Hindi", "Hindi to English"],
|
| 111 |
+
value="None", label="π Translate Text")
|
| 112 |
+
|
| 113 |
+
tts_button = gr.Button("ποΈ Generate Voice")
|
| 114 |
+
output_text = gr.Textbox(label="π Final Output / Translation")
|
| 115 |
+
with gr.Column(scale=1):
|
| 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 |
tts_button.click(fn=tts_wrapper,
|
| 120 |
inputs=[text_input, language_dropdown, character_dropdown, translation_dropdown],
|
| 121 |
outputs=[output_text, output_audio])
|
| 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 |
+
gr.HTML("<footer>π§ Made by using Gradio, Edge TTS, and Hugging Face π€</footer>")
|
|
|
|
| 139 |
|
| 140 |
return app
|
| 141 |
|