JaganathC commited on
Commit
0d27e5e
·
verified ·
1 Parent(s): ca92cc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -51,18 +51,15 @@ def download_youtube_audio(url):
51
  'preferredcodec': 'wav',
52
  'preferredquality': '192',
53
  }],
54
- 'outtmpl': output_path,
55
  }
56
 
57
  try:
58
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
59
  ydl.download([url])
60
- if os.path.exists(output_path + ".wav"):
61
- os.rename(output_path + ".wav", output_path) # Ensure correct naming
62
  except Exception as e:
63
  return f"Error downloading audio: {str(e)}"
64
-
65
- return output_path if os.path.exists(output_path) else "Download Failed"
66
 
67
  def transcribe_audio(file_path):
68
  """Transcribes audio using `insanely-fast-whisper` and handles large files efficiently."""
@@ -96,9 +93,8 @@ def transcribe_audio(file_path):
96
  result = []
97
  try:
98
  with open(output_file, "r") as f:
99
- for line in f:
100
- chunk = json.loads(line.strip()) # Read JSON line by line
101
- result.append(chunk.get("text", ""))
102
  except Exception as e:
103
  return f"Error reading transcription file: {str(e)}"
104
 
@@ -110,18 +106,24 @@ def transcribe_audio(file_path):
110
 
111
  def generate_summary_stream(transcription):
112
  """Summarizes the transcription efficiently to avoid memory overflow."""
 
 
 
113
  detected_language = langdetect.detect(transcription[:1000]) # Detect using a smaller portion
114
 
115
  # Use smaller chunks for processing
116
- chunk_size = 2000
117
  transcript_chunks = [transcription[i:i+chunk_size] for i in range(0, len(transcription), chunk_size)]
118
  summary_result = []
119
 
120
- for chunk in transcript_chunks[:3]: # Process only the first 3 chunks to avoid OOM
121
  prompt = f"""Summarize the following video transcription in 150-300 words in {detected_language}:\n{chunk}"""
122
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
123
- output_ids = model.generate(input_ids, max_length=300) # Limit output size
124
- response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
 
 
125
  summary_result.append(response)
126
 
127
  return "\n\n".join(summary_result)
@@ -167,4 +169,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
167
  url_button.click(process_youtube, inputs=[url_input], outputs=[transcription_output, summary_output])
168
  summary_button.click(generate_summary_stream, inputs=[transcription_output], outputs=[summary_output])
169
 
170
- demo.launch()
 
51
  'preferredcodec': 'wav',
52
  'preferredquality': '192',
53
  }],
54
+ 'outtmpl': output_path[:-4] # Remove .wav to prevent duplication
55
  }
56
 
57
  try:
58
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
59
  ydl.download([url])
60
+ return output_path if os.path.exists(output_path) else "Download Failed"
 
61
  except Exception as e:
62
  return f"Error downloading audio: {str(e)}"
 
 
63
 
64
  def transcribe_audio(file_path):
65
  """Transcribes audio using `insanely-fast-whisper` and handles large files efficiently."""
 
93
  result = []
94
  try:
95
  with open(output_file, "r") as f:
96
+ data = json.load(f) # Load full JSON safely
97
+ result = [chunk.get("text", "") for chunk in data]
 
98
  except Exception as e:
99
  return f"Error reading transcription file: {str(e)}"
100
 
 
106
 
107
  def generate_summary_stream(transcription):
108
  """Summarizes the transcription efficiently to avoid memory overflow."""
109
+ if not transcription:
110
+ return "No transcription available."
111
+
112
  detected_language = langdetect.detect(transcription[:1000]) # Detect using a smaller portion
113
 
114
  # Use smaller chunks for processing
115
+ chunk_size = 1000 # Reduce chunk size
116
  transcript_chunks = [transcription[i:i+chunk_size] for i in range(0, len(transcription), chunk_size)]
117
  summary_result = []
118
 
119
+ for chunk in transcript_chunks[:5]: # Process only the first 5 chunks
120
  prompt = f"""Summarize the following video transcription in 150-300 words in {detected_language}:\n{chunk}"""
121
+ try:
122
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
123
+ output_ids = model.generate(input_ids, max_length=300) # Limit output size
124
+ response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
125
+ except Exception as e:
126
+ response = f"Error generating summary: {str(e)}"
127
  summary_result.append(response)
128
 
129
  return "\n\n".join(summary_result)
 
169
  url_button.click(process_youtube, inputs=[url_input], outputs=[transcription_output, summary_output])
170
  summary_button.click(generate_summary_stream, inputs=[transcription_output], outputs=[summary_output])
171
 
172
+ demo.launch(share=True, debug=True, queue=True)