Yogya12 commited on
Commit
8d29d40
Β·
1 Parent(s): f647470

updated ui

Browse files
Files changed (1) hide show
  1. app.py +50 -48
app.py CHANGED
@@ -8,7 +8,7 @@ from deep_translator import GoogleTranslator
8
  from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
9
  import torch
10
 
11
- # ===== TTS PART =====
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" # Only one voice will be used for Hindi
 
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 "Input text is empty", None
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
- # Hindi uses fixed voice "Swara"
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"No voice found for selected language and character", None
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
- if language == "Hindi":
60
- return gr.update(choices=[], value=None, visible=False)
61
- else:
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
- # ===== CHATBOT PART USING BLENDERBOT =====
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(("User", user_message))
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
- history.append(("Bot", response))
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 generation failed: {e}")
101
 
102
  return history, chat_str, audio_path
103
 
104
- # ===== GRADIO UI =====
105
 
106
  def create_app():
107
- with gr.Blocks() as app:
108
- gr.Markdown("## πŸŽ™οΈ Multi-Voice AI TTS with Translation + πŸ€– BlenderBot Chatbot")
109
-
110
- with gr.Tab("TTS"):
111
- text_input = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=4)
112
- language_dropdown = gr.Dropdown(choices=list(voice_characters.keys()), value="English - US", label="Language")
113
- character_dropdown = gr.Dropdown(choices=list(voice_characters["English - US"].keys()), value="Aria", label="Voice Character", visible=True)
114
- translation_dropdown = gr.Dropdown(choices=["None", "English to Hindi", "Hindi to English"], value="None", label="Translation Direction")
115
- tts_button = gr.Button("πŸ”Š Generate Voice")
116
- output_text = gr.Textbox(label="Translated Text or Error")
117
- output_audio = gr.Audio(label="Generated Voice")
 
 
 
 
 
 
 
 
 
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
- chat_history = gr.State([])
127
- user_input = gr.Textbox(label="Enter your message", lines=2, placeholder="Say something...")
128
- chat_display = gr.Textbox(label="Chat History", interactive=False, lines=15)
129
- audio_output = gr.Audio(label="Bot Voice Reply")
130
- send_button = gr.Button("Send")
 
 
131
 
132
- def respond(user_message, history):
133
- return chatbot_response(history, user_message)
 
 
 
134
 
135
- send_button.click(fn=respond, inputs=[user_input, chat_history], outputs=[chat_history, chat_display, audio_output])
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