Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -51,25 +51,26 @@ def process_audio_input(audio, whisper_processor, whisper_model):
|
|
51 |
|
52 |
# Generate response within a GPU-decorated function
|
53 |
@spaces.GPU
|
54 |
-
def generate_response(text_input, sarvam_pipe):
|
55 |
-
if sarvam_pipe is None:
|
56 |
-
return "Error: sarvam-2b model is not available. The assistant cannot generate responses at this time."
|
57 |
-
|
58 |
-
try:
|
59 |
-
response = sarvam_pipe(text_input, max_new_tokens=100, temperature=0.7, repetition_penalty=1.2)[0]['generated_text']
|
60 |
-
return response
|
61 |
-
except Exception as e:
|
62 |
-
return f"Error generating response: {str(e)}"
|
63 |
-
|
64 |
def text_to_speech(text, lang='hi'):
|
65 |
try:
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
tts.save("response.mp3")
|
68 |
return "response.mp3"
|
69 |
except Exception as e:
|
70 |
print(f"Error in text-to-speech: {str(e)}")
|
71 |
return None
|
72 |
|
|
|
73 |
def detect_language(text):
|
74 |
lang_codes = {
|
75 |
'bn': 'Bengali', 'gu': 'Gujarati', 'hi': 'Hindi', 'kn': 'Kannada',
|
@@ -77,10 +78,15 @@ def detect_language(text):
|
|
77 |
'ta': 'Tamil', 'te': 'Telugu', 'en': 'English'
|
78 |
}
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
@spaces.GPU
|
86 |
def indic_language_assistant(input_type, audio_input, text_input):
|
|
|
51 |
|
52 |
# Generate response within a GPU-decorated function
|
53 |
@spaces.GPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def text_to_speech(text, lang='hi'):
|
55 |
try:
|
56 |
+
# Use a better TTS engine for Indic languages
|
57 |
+
if lang in ['hi', 'bn', 'gu', 'kn', 'ml', 'mr', 'or', 'pa', 'ta', 'te']:
|
58 |
+
# You might want to use a different TTS library here
|
59 |
+
# For example, you could use the Google Cloud Text-to-Speech API
|
60 |
+
# or a specialized Indic language TTS library
|
61 |
+
|
62 |
+
# This is a placeholder for a better Indic TTS solution
|
63 |
+
tts = gTTS(text=text, lang=lang, tld='co.in') # Use Indian TLD
|
64 |
+
else:
|
65 |
+
tts = gTTS(text=text, lang=lang)
|
66 |
+
|
67 |
tts.save("response.mp3")
|
68 |
return "response.mp3"
|
69 |
except Exception as e:
|
70 |
print(f"Error in text-to-speech: {str(e)}")
|
71 |
return None
|
72 |
|
73 |
+
# Replace the existing detect_language function with this improved version
|
74 |
def detect_language(text):
|
75 |
lang_codes = {
|
76 |
'bn': 'Bengali', 'gu': 'Gujarati', 'hi': 'Hindi', 'kn': 'Kannada',
|
|
|
78 |
'ta': 'Tamil', 'te': 'Telugu', 'en': 'English'
|
79 |
}
|
80 |
|
81 |
+
try:
|
82 |
+
detected_lang = detect(text)
|
83 |
+
return detected_lang if detected_lang in lang_codes else 'en'
|
84 |
+
except:
|
85 |
+
# Fallback to simple script-based detection
|
86 |
+
for code, lang in lang_codes.items():
|
87 |
+
if any(ord(char) >= 0x0900 and ord(char) <= 0x097F for char in text): # Devanagari script
|
88 |
+
return 'hi'
|
89 |
+
return 'en' # Default to English if no Indic script is detected
|
90 |
|
91 |
@spaces.GPU
|
92 |
def indic_language_assistant(input_type, audio_input, text_input):
|