sagar007 commited on
Commit
0d574ff
·
verified ·
1 Parent(s): dc950e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -9,25 +9,28 @@ print("Using GPU for operations when available")
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
@@ -241,7 +244,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
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,7 +263,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
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>")
 
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
+ # Use the selected language for text-to-speech
28
+ audio_response = text_to_speech(response, selected_language)
29
+
30
+ return transcription, response, audio_response
31
  except Exception as e:
32
+ error_message = f"An error occurred: {str(e)}"
33
+ return error_message, error_message, None
34
 
35
  # Load sarvam-2b for text generation within a GPU-decorated function
36
  @spaces.GPU
 
244
  gr.Markdown("### Indic Assistant")
245
 
246
  input_type = gr.Radio(["audio", "text"], label="Input Type", value="audio")
247
+
248
+ with gr.Row():
249
+ audio_input = gr.Audio(type="filepath", label="Speak (if audio input selected)")
250
+ language_select = gr.Dropdown(
251
+ choices=["Bengali", "English", "Gujarati", "Hindi", "Kannada", "Malayalam", "Marathi", "Oriya", "Punjabi", "Tamil", "Telugu"],
252
+ label="Select Language",
253
+ value="English"
254
+ )
255
+
256
  text_input = gr.Textbox(label="Type your message (if text input selected)")
257
 
258
  submit_btn = gr.Button("Submit")
 
263
 
264
  submit_btn.click(
265
  fn=indic_language_assistant,
266
+ inputs=[input_type, audio_input, text_input, language_select],
267
  outputs=[output_transcription, output_response, output_audio]
268
  )
269
  gr.HTML("<footer>Powered by Indic Language AI</footer>")