Spaces:
Running
Running
fixed code
Browse files
app.py
CHANGED
@@ -51,11 +51,11 @@ def tts_wrapper(text, language, character, translation_direction):
|
|
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.
|
58 |
-
|
59 |
|
60 |
# ===== CHATBOT PART USING BLENDERBOT =====
|
61 |
|
@@ -65,9 +65,7 @@ model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
|
65 |
device = torch.device("cpu")
|
66 |
model.to(device)
|
67 |
|
68 |
-
# We add async TTS call for bot replies here
|
69 |
async def generate_bot_tts(text):
|
70 |
-
# You can choose voice here for bot TTS, for example English Aria
|
71 |
voice = voice_characters["English - US"]["Aria"]
|
72 |
filename = await generate_tts(text, voice)
|
73 |
return filename
|
@@ -76,25 +74,19 @@ def chatbot_response(history, user_message):
|
|
76 |
if history is None:
|
77 |
history = []
|
78 |
|
79 |
-
# Append new user message
|
80 |
history.append(("User", user_message))
|
81 |
-
|
82 |
-
# Prepare conversation text for BlenderBot
|
83 |
conversation_text = " ".join([msg for _, msg in history]) + " " + user_message
|
84 |
inputs = tokenizer([conversation_text], return_tensors="pt").to(device)
|
85 |
|
86 |
reply_ids = model.generate(**inputs, max_length=200)
|
87 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
88 |
|
89 |
-
# Append bot reply
|
90 |
history.append(("Bot", response))
|
91 |
|
92 |
-
# Prepare chat display string
|
93 |
chat_str = ""
|
94 |
for speaker, msg in history:
|
95 |
chat_str += f"{speaker}: {msg}\n"
|
96 |
|
97 |
-
# Generate bot TTS audio file
|
98 |
try:
|
99 |
audio_path = asyncio.run(generate_bot_tts(response))
|
100 |
except Exception as e:
|
@@ -103,12 +95,11 @@ def chatbot_response(history, user_message):
|
|
103 |
|
104 |
return history, chat_str, audio_path
|
105 |
|
106 |
-
|
107 |
# ===== GRADIO UI =====
|
108 |
|
109 |
def create_app():
|
110 |
with gr.Blocks() as app:
|
111 |
-
gr.Markdown("## 🎙️ Multi-Voice AI TTS with Translation + 🤖Chatbot")
|
112 |
|
113 |
with gr.Tab("TTS"):
|
114 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=4)
|
@@ -119,7 +110,9 @@ def create_app():
|
|
119 |
output_text = gr.Textbox(label="Translated Text or Error")
|
120 |
output_audio = gr.Audio(label="Generated Voice")
|
121 |
|
|
|
122 |
language_dropdown.change(fn=get_characters, inputs=language_dropdown, outputs=character_dropdown)
|
|
|
123 |
tts_button.click(fn=tts_wrapper,
|
124 |
inputs=[text_input, language_dropdown, character_dropdown, translation_dropdown],
|
125 |
outputs=[output_text, output_audio])
|
@@ -139,7 +132,6 @@ def create_app():
|
|
139 |
|
140 |
return app
|
141 |
|
142 |
-
|
143 |
if __name__ == "__main__":
|
144 |
app = create_app()
|
145 |
app.launch(share=True)
|
|
|
51 |
except Exception as e:
|
52 |
return f"Error: {str(e)}", None
|
53 |
|
54 |
+
# ✅ FIXED: Voice character list updates properly now
|
55 |
def get_characters(language):
|
56 |
chars = list(voice_characters.get(language, {}).keys())
|
57 |
default_char = chars[0] if chars else None
|
58 |
+
return gr.update(choices=chars, value=default_char)
|
|
|
59 |
|
60 |
# ===== CHATBOT PART USING BLENDERBOT =====
|
61 |
|
|
|
65 |
device = torch.device("cpu")
|
66 |
model.to(device)
|
67 |
|
|
|
68 |
async def generate_bot_tts(text):
|
|
|
69 |
voice = voice_characters["English - US"]["Aria"]
|
70 |
filename = await generate_tts(text, voice)
|
71 |
return filename
|
|
|
74 |
if history is None:
|
75 |
history = []
|
76 |
|
|
|
77 |
history.append(("User", user_message))
|
|
|
|
|
78 |
conversation_text = " ".join([msg for _, msg in history]) + " " + user_message
|
79 |
inputs = tokenizer([conversation_text], return_tensors="pt").to(device)
|
80 |
|
81 |
reply_ids = model.generate(**inputs, max_length=200)
|
82 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
83 |
|
|
|
84 |
history.append(("Bot", response))
|
85 |
|
|
|
86 |
chat_str = ""
|
87 |
for speaker, msg in history:
|
88 |
chat_str += f"{speaker}: {msg}\n"
|
89 |
|
|
|
90 |
try:
|
91 |
audio_path = asyncio.run(generate_bot_tts(response))
|
92 |
except Exception as e:
|
|
|
95 |
|
96 |
return history, chat_str, audio_path
|
97 |
|
|
|
98 |
# ===== GRADIO UI =====
|
99 |
|
100 |
def create_app():
|
101 |
with gr.Blocks() as app:
|
102 |
+
gr.Markdown("## 🎙️ Multi-Voice AI TTS with Translation + 🤖 BlenderBot Chatbot")
|
103 |
|
104 |
with gr.Tab("TTS"):
|
105 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=4)
|
|
|
110 |
output_text = gr.Textbox(label="Translated Text or Error")
|
111 |
output_audio = gr.Audio(label="Generated Voice")
|
112 |
|
113 |
+
# ✅ This updates the character dropdown dynamically based on selected language
|
114 |
language_dropdown.change(fn=get_characters, inputs=language_dropdown, outputs=character_dropdown)
|
115 |
+
|
116 |
tts_button.click(fn=tts_wrapper,
|
117 |
inputs=[text_input, language_dropdown, character_dropdown, translation_dropdown],
|
118 |
outputs=[output_text, output_audio])
|
|
|
132 |
|
133 |
return app
|
134 |
|
|
|
135 |
if __name__ == "__main__":
|
136 |
app = create_app()
|
137 |
app.launch(share=True)
|