sagar007 commited on
Commit
89f6b85
·
verified ·
1 Parent(s): 190a41a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -36
app.py CHANGED
@@ -9,35 +9,26 @@ print("Using GPU for operations when available")
9
 
10
  # Function to safely load pipeline within a GPU-decorated function
11
  @spaces.GPU
12
- def indic_language_assistant(input_type, audio_input, text_input, selected_language):
13
  try:
14
- # Load models within the GPU-decorated function
15
- whisper_processor, whisper_model = load_whisper()
16
- sarvam_pipe = load_sarvam()
17
-
18
- if input_type == "audio" and audio_input is not None:
19
- transcription = process_audio_input(audio_input, whisper_processor, whisper_model)
20
- elif input_type == "text" and text_input:
21
- transcription = text_input
22
- else:
23
- return "Please provide either audio or text input.", "No input provided.", None
24
 
25
- response = generate_response(transcription, sarvam_pipe)
26
-
27
- # Convert the selected language to the appropriate language code
28
- lang_code = {
29
- "Bengali": "bn", "English": "en", "Gujarati": "gu", "Hindi": "hi",
30
- "Kannada": "kn", "Malayalam": "ml", "Marathi": "mr", "Oriya": "or",
31
- "Punjabi": "pa", "Tamil": "ta", "Telugu": "te"
32
- }.get(selected_language, "en")
33
-
34
- # Use the selected language for text-to-speech
35
- audio_response = text_to_speech(response, lang_code)
36
-
37
- return transcription, response, audio_response
38
  except Exception as e:
39
- error_message = f"An error occurred: {str(e)}"
40
- return error_message, error_message, None
 
41
  # Load sarvam-2b for text generation within a GPU-decorated function
42
  @spaces.GPU
43
  def load_sarvam():
@@ -250,15 +241,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
250
  gr.Markdown("### Indic Assistant")
251
 
252
  input_type = gr.Radio(["audio", "text"], label="Input Type", value="audio")
253
-
254
- with gr.Row():
255
- audio_input = gr.Audio(type="filepath", label="Speak (if audio input selected)")
256
- language_select = gr.Dropdown(
257
- choices=["Bengali", "English", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"],
258
- label="Select Language",
259
- value="English"
260
- )
261
-
262
  text_input = gr.Textbox(label="Type your message (if text input selected)")
263
 
264
  submit_btn = gr.Button("Submit")
@@ -269,7 +252,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
269
 
270
  submit_btn.click(
271
  fn=indic_language_assistant,
272
- inputs=[input_type, audio_input, text_input, language_select],
273
  outputs=[output_transcription, output_response, output_audio]
274
  )
275
  gr.HTML("<footer>Powered by Indic Language AI</footer>")
 
9
 
10
  # Function to safely load pipeline within a GPU-decorated function
11
  @spaces.GPU
12
+ def load_pipeline(model_name, **kwargs):
13
  try:
14
+ device = 0 if torch.cuda.is_available() else "cpu"
15
+ return pipeline(model=model_name, device=device, **kwargs)
16
+ except Exception as e:
17
+ print(f"Error loading {model_name} pipeline: {e}")
18
+ return None
 
 
 
 
 
19
 
20
+ # Load Whisper model for speech recognition within a GPU-decorated function
21
+ @spaces.GPU
22
+ def load_whisper():
23
+ try:
24
+ device = 0 if torch.cuda.is_available() else "cpu"
25
+ processor = WhisperProcessor.from_pretrained("openai/whisper-small")
26
+ model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").to(device)
27
+ return processor, model
 
 
 
 
 
28
  except Exception as e:
29
+ print(f"Error loading Whisper model: {e}")
30
+ return None, None
31
+
32
  # Load sarvam-2b for text generation within a GPU-decorated function
33
  @spaces.GPU
34
  def load_sarvam():
 
241
  gr.Markdown("### Indic Assistant")
242
 
243
  input_type = gr.Radio(["audio", "text"], label="Input Type", value="audio")
244
+ audio_input = gr.Audio(type="filepath", label="Speak (if audio input selected)")
 
 
 
 
 
 
 
 
245
  text_input = gr.Textbox(label="Type your message (if text input selected)")
246
 
247
  submit_btn = gr.Button("Submit")
 
252
 
253
  submit_btn.click(
254
  fn=indic_language_assistant,
255
+ inputs=[input_type, audio_input, text_input],
256
  outputs=[output_transcription, output_response, output_audio]
257
  )
258
  gr.HTML("<footer>Powered by Indic Language AI</footer>")