geethareddy commited on
Commit
fcc4622
·
verified ·
1 Parent(s): 5b56e0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -71
app.py CHANGED
@@ -1,8 +1,4 @@
1
- import gradio as gr
2
- import torch
3
  import subprocess
4
- import speech_recognition as sr
5
- from TTS.api import TTS
6
 
7
  # Run the setup.py install command
8
  try:
@@ -11,88 +7,28 @@ try:
11
  except subprocess.CalledProcessError as e:
12
  print(f"Installation failed with error: {e}")
13
 
 
 
 
 
14
  # Get device
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
 
17
  # Init TTS
18
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
19
 
20
- # Menu Data (You can modify this as per your actual menu)
21
- def load_menu():
22
- menu = {
23
- "Breads": [
24
- {"name": "Roti", "price": 1.50, "description": "Indian flatbread"},
25
- {"name": "Naan", "price": 2.00, "description": "Soft leavened flatbread"}
26
- ],
27
- "Curries": [
28
- {"name": "Butter Chicken", "price": 7.99, "description": "Creamy and rich chicken curry"},
29
- {"name": "Paneer Tikka Masala", "price": 6.99, "description": "Cottage cheese in a spiced gravy"}
30
- ],
31
- "Biryanis": [
32
- {"name": "Chicken Biryani", "price": 8.99, "description": "Fragrant rice with spiced chicken"},
33
- {"name": "Vegetable Biryani", "price": 7.50, "description": "Fragrant rice with mixed vegetables"}
34
- ]
35
- }
36
- return menu
37
-
38
- # Function to process commands and get menu details
39
- def process_command(command):
40
- menu = load_menu()
41
-
42
- if 'menu' in command.lower():
43
- return "Here's our menu: \n" + "\n".join([f"{category}: {', '.join([item['name'] for item in items])}" for category, items in menu.items()])
44
-
45
- elif 'breads' in command.lower():
46
- return "Our breads: " + ", ".join([item['name'] for item in menu["Breads"]])
47
-
48
- elif 'curries' in command.lower():
49
- return "Our curries: " + ", ".join([item['name'] for item in menu["Curries"]])
50
-
51
- elif 'biryani' in command.lower():
52
- return "Our biryanis: " + ", ".join([item['name'] for item in menu["Biryanis"]])
53
-
54
- return "I'm sorry, I didn't understand that command."
55
-
56
- # Function to recognize speech input
57
- def recognize_speech_from_audio(audio_file):
58
- recognizer = sr.Recognizer()
59
- with sr.AudioFile(audio_file) as source:
60
- audio_data = recognizer.record(source)
61
- try:
62
- # Using Google Web Speech API for recognition
63
- text = recognizer.recognize_google(audio_data)
64
- print(f"Recognized text: {text}")
65
- return text
66
- except sr.UnknownValueError:
67
- return "Sorry, I couldn't understand the speech."
68
- except sr.RequestError:
69
- return "Sorry, the speech service is down."
70
-
71
- # Function to generate speech (voice cloning)
72
  def voice_clone(text: str, speaker_wav: str, language: str):
 
73
  print("Speaker wav:", speaker_wav)
74
  tts.tts_to_file(text=text, speaker_wav=speaker_wav, language=language, file_path="output.wav")
75
  return "output.wav"
76
 
77
- # Gradio interface for interaction
78
- def interact_with_assistant(user_input, speaker_audio, language):
79
- if user_input:
80
- response = process_command(user_input)
81
- elif speaker_audio:
82
- # If audio input is provided, recognize the speech
83
- recognized_text = recognize_speech_from_audio(speaker_audio)
84
- response = process_command(recognized_text)
85
-
86
- # Generate speech output for the response
87
- output_audio = voice_clone(response, speaker_wav=speaker_audio, language=language)
88
- return output_audio
89
-
90
- iface = gr.Interface(fn=interact_with_assistant,
91
  inputs=[gr.Textbox(lines=2, placeholder="Enter the text...", label="Text"),
92
  gr.Audio(type="filepath", label="Upload audio file"),
93
  gr.Radio(['ru', 'en', 'zh-cn', 'ja', 'de', 'fr', 'it', 'pt', 'pl', 'tr', 'ko', 'nl', 'cs', 'ar', 'es', 'hu'], label="language"),
94
  ],
95
  outputs=gr.Audio(type="filepath", label="Generated audio file"),
96
- title="Voice Assistant - Menu and Voice Cloning")
97
 
98
  iface.launch()
 
 
 
1
  import subprocess
 
 
2
 
3
  # Run the setup.py install command
4
  try:
 
7
  except subprocess.CalledProcessError as e:
8
  print(f"Installation failed with error: {e}")
9
 
10
+ import gradio as gr
11
+ import torch
12
+ from TTS.api import TTS
13
+
14
  # Get device
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
 
17
  # Init TTS
18
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def voice_clone(text: str, speaker_wav: str, language: str):
21
+ # Run TTS
22
  print("Speaker wav:", speaker_wav)
23
  tts.tts_to_file(text=text, speaker_wav=speaker_wav, language=language, file_path="output.wav")
24
  return "output.wav"
25
 
26
+ iface = gr.Interface(fn=voice_clone,
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  inputs=[gr.Textbox(lines=2, placeholder="Enter the text...", label="Text"),
28
  gr.Audio(type="filepath", label="Upload audio file"),
29
  gr.Radio(['ru', 'en', 'zh-cn', 'ja', 'de', 'fr', 'it', 'pt', 'pl', 'tr', 'ko', 'nl', 'cs', 'ar', 'es', 'hu'], label="language"),
30
  ],
31
  outputs=gr.Audio(type="filepath", label="Generated audio file"),
32
+ title="Voice Cloning")
33
 
34
  iface.launch()