Kr08 commited on
Commit
444b9c9
1 Parent(s): 0b6f315

Optimized app.py with on-demand model loading and lighter models

Browse files
Files changed (1) hide show
  1. app.py +51 -32
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from audio_processing import process_audio, load_models
3
  from transformers import pipeline
4
  import spaces
5
  import torch
@@ -7,41 +7,27 @@ import logging
7
  import traceback
8
  import sys
9
 
10
- # Set up logging
11
  logging.basicConfig(
12
  level=logging.INFO,
13
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
14
  handlers=[
15
- logging.StreamHandler(sys.stdout),
16
- logging.FileHandler('app.log')
17
  ]
18
  )
19
  logger = logging.getLogger(__name__)
20
 
21
- # Check if CUDA is available
22
- cuda_available = torch.cuda.is_available()
23
- device = "cuda" if cuda_available else "cpu"
24
- logger.info(f"Using device: {device}")
25
-
26
- # Load Whisper model
27
- # print("Loading Whisper model...")
28
- # try:
29
- # load_models() # Load Whisper model
30
- # except Exception as e:
31
- # logger.error(f"Error loading Whisper model: {str(e)}")
32
- # raise
33
-
34
- print("Whisper model loaded successfully.")
35
-
36
  def load_summarization_model():
37
  logger.info("Loading summarization model...")
38
  try:
 
39
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=0 if cuda_available else -1)
 
 
40
  except Exception as e:
41
  logger.warning(f"Failed to load summarization model on GPU. Falling back to CPU. Error: {str(e)}")
42
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=-1)
43
- logger.info("Summarization model loaded.")
44
- return summarizer
45
 
46
  def process_with_fallback(func, *args, **kwargs):
47
  try:
@@ -51,7 +37,6 @@ def process_with_fallback(func, *args, **kwargs):
51
  logger.error(traceback.format_exc())
52
  if "CUDA" in str(e) or "GPU" in str(e):
53
  logger.info("Falling back to CPU processing...")
54
- # Modify kwargs to force CPU processing
55
  kwargs['use_gpu'] = False
56
  return func(*args, **kwargs)
57
  else:
@@ -59,24 +44,58 @@ def process_with_fallback(func, *args, **kwargs):
59
 
60
  @spaces.GPU(duration=60)
61
  def transcribe_audio(audio_file, translate, model_size, use_diarization):
62
- return process_with_fallback(process_audio, audio_file, translate=translate, model_size=model_size, use_diarization=use_diarization)
 
 
 
 
 
 
 
63
 
64
  @spaces.GPU(duration=60)
65
  def summarize_text(text):
66
- summarizer = load_summarization_model()
67
  try:
 
68
  summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
 
 
69
  except Exception as e:
70
- logger.error(f"Error during summarization: {str(e)}")
71
  logger.error(traceback.format_exc())
72
- summary = "Error occurred during summarization. Please try again."
73
- return summary
74
 
75
  @spaces.GPU(duration=60)
76
  def process_and_summarize(audio_file, translate, model_size, use_diarization, do_summarize):
77
- transcription, full_text = transcribe_audio(audio_file, translate, model_size, use_diarization)
78
- summary = summarize_text(full_text) if do_summarize else ""
79
- return transcription, summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # Main interface
82
  with gr.Blocks() as iface:
@@ -105,8 +124,8 @@ with gr.Blocks() as iface:
105
  gr.Markdown(
106
  f"""
107
  ## System Information
108
- - Device: {device}
109
- - CUDA Available: {"Yes" if cuda_available else "No"}
110
 
111
  ## ZeroGPU Support
112
  This application supports ZeroGPU for Hugging Face Spaces pro users.
 
1
  import gradio as gr
2
+ from audio_processing import process_audio
3
  from transformers import pipeline
4
  import spaces
5
  import torch
 
7
  import traceback
8
  import sys
9
 
 
10
  logging.basicConfig(
11
  level=logging.INFO,
12
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
13
  handlers=[
14
+ logging.StreamHandler(sys.stdout)
 
15
  ]
16
  )
17
  logger = logging.getLogger(__name__)
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def load_summarization_model():
20
  logger.info("Loading summarization model...")
21
  try:
22
+ cuda_available = torch.cuda.is_available()
23
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=0 if cuda_available else -1)
24
+ logger.info(f"Summarization model loaded successfully on {'GPU' if cuda_available else 'CPU'}")
25
+ return summarizer
26
  except Exception as e:
27
  logger.warning(f"Failed to load summarization model on GPU. Falling back to CPU. Error: {str(e)}")
28
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=-1)
29
+ logger.info("Summarization model loaded successfully on CPU")
30
+ return summarizer
31
 
32
  def process_with_fallback(func, *args, **kwargs):
33
  try:
 
37
  logger.error(traceback.format_exc())
38
  if "CUDA" in str(e) or "GPU" in str(e):
39
  logger.info("Falling back to CPU processing...")
 
40
  kwargs['use_gpu'] = False
41
  return func(*args, **kwargs)
42
  else:
 
44
 
45
  @spaces.GPU(duration=60)
46
  def transcribe_audio(audio_file, translate, model_size, use_diarization):
47
+ logger.info(f"Starting transcription: translate={translate}, model_size={model_size}, use_diarization={use_diarization}")
48
+ try:
49
+ result = process_with_fallback(process_audio, audio_file, translate=translate, model_size=model_size, use_diarization=use_diarization)
50
+ logger.info("Transcription completed successfully")
51
+ return result
52
+ except Exception as e:
53
+ logger.error(f"Transcription failed: {str(e)}")
54
+ raise gr.Error(f"Transcription failed: {str(e)}")
55
 
56
  @spaces.GPU(duration=60)
57
  def summarize_text(text):
58
+ logger.info("Starting text summarization")
59
  try:
60
+ summarizer = load_summarization_model()
61
  summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
62
+ logger.info("Summarization completed successfully")
63
+ return summary
64
  except Exception as e:
65
+ logger.error(f"Summarization failed: {str(e)}")
66
  logger.error(traceback.format_exc())
67
+ return "Error occurred during summarization. Please try again."
 
68
 
69
  @spaces.GPU(duration=60)
70
  def process_and_summarize(audio_file, translate, model_size, use_diarization, do_summarize):
71
+ logger.info(f"Starting process_and_summarize: translate={translate}, model_size={model_size}, use_diarization={use_diarization}, do_summarize={do_summarize}")
72
+ try:
73
+ language_segments, final_segments = transcribe_audio(audio_file, translate, model_size, use_diarization)
74
+
75
+ transcription = "Detected language changes:\n\n"
76
+ for segment in language_segments:
77
+ transcription += f"Language: {segment['language']}\n"
78
+ transcription += f"Time: {segment['start']:.2f}s - {segment['end']:.2f}s\n\n"
79
+
80
+ transcription += f"Transcription with language detection and speaker diarization (using {model_size} model):\n\n"
81
+ full_text = ""
82
+ for segment in final_segments:
83
+ transcription += f"[{segment['start']:.2f}s - {segment['end']:.2f}s] ({segment['language']}) {segment['speaker']}:\n"
84
+ transcription += f"Original: {segment['text']}\n"
85
+ if translate:
86
+ transcription += f"Translated: {segment['translated']}\n"
87
+ full_text += segment['translated'] + " "
88
+ else:
89
+ full_text += segment['text'] + " "
90
+ transcription += "\n"
91
+
92
+ summary = summarize_text(full_text) if do_summarize else ""
93
+ logger.info("Process and summarize completed successfully")
94
+ return transcription, summary
95
+ except Exception as e:
96
+ logger.error(f"Process and summarize failed: {str(e)}")
97
+ logger.error(traceback.format_exc())
98
+ raise gr.Error(f"Processing failed: {str(e)}")
99
 
100
  # Main interface
101
  with gr.Blocks() as iface:
 
124
  gr.Markdown(
125
  f"""
126
  ## System Information
127
+ - Device: {"CUDA" if torch.cuda.is_available() else "CPU"}
128
+ - CUDA Available: {"Yes" if torch.cuda.is_available() else "No"}
129
 
130
  ## ZeroGPU Support
131
  This application supports ZeroGPU for Hugging Face Spaces pro users.