bluenevus commited on
Commit
e299575
·
verified ·
1 Parent(s): 7824869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -16
app.py CHANGED
@@ -14,6 +14,7 @@ import requests
14
  from pytube import YouTube
15
  from pydub import AudioSegment
16
  import google.generativeai as genai
 
17
 
18
  # Initialize the Dash app
19
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
@@ -32,19 +33,35 @@ def is_valid_url(url):
32
  except ValueError:
33
  return False
34
 
35
- def download_audio(url):
36
  if "youtube.com" in url or "youtu.be" in url:
37
  yt = YouTube(url)
38
- audio_stream = yt.streams.filter(only_audio=True).first()
39
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
40
- audio_stream.download(output_path=os.path.dirname(temp_file.name), filename=temp_file.name)
41
  return temp_file.name
42
  else:
43
  response = requests.get(url)
44
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
 
 
 
 
 
 
 
45
  temp_file.write(response.content)
46
  return temp_file.name
47
 
 
 
 
 
 
 
 
 
 
48
  def transcribe_audio(file_path):
49
  with open(file_path, "rb") as audio_file:
50
  audio_data = audio_file.read()
@@ -52,34 +69,40 @@ def transcribe_audio(file_path):
52
  response = model.generate_content(audio_data)
53
  return response.text
54
 
55
- def process_audio(contents, filename, url):
56
  if contents:
57
  content_type, content_string = contents.split(',')
58
  decoded = base64.b64decode(content_string)
59
- with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as temp_file:
 
60
  temp_file.write(decoded)
61
  temp_file_path = temp_file.name
62
  elif url:
63
- temp_file_path = download_audio(url)
64
  else:
65
  raise ValueError("No input provided")
66
 
67
  try:
68
- transcript = transcribe_audio(temp_file_path)
 
 
 
 
 
69
  finally:
70
  os.unlink(temp_file_path)
71
 
72
  return transcript
73
 
74
  app.layout = dbc.Container([
75
- html.H1("Audio Transcription App", className="text-center my-4"),
76
  dbc.Card([
77
  dbc.CardBody([
78
  dcc.Upload(
79
- id='upload-audio',
80
  children=html.Div([
81
  'Drag and Drop or ',
82
- html.A('Select Audio File')
83
  ]),
84
  style={
85
  'width': '100%',
@@ -93,7 +116,7 @@ app.layout = dbc.Container([
93
  },
94
  multiple=False
95
  ),
96
- dbc.Input(id="audio-url", type="text", placeholder="Enter audio URL or YouTube link", className="my-3"),
97
  dbc.Button("Transcribe", id="transcribe-button", color="primary", className="w-100 mb-3"),
98
  dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
99
  dbc.Button("Download Transcript", id="download-button", color="secondary", className="w-100 mt-3", style={'display': 'none'}),
@@ -106,9 +129,9 @@ app.layout = dbc.Container([
106
  Output("transcription-output", "children"),
107
  Output("download-button", "style"),
108
  Input("transcribe-button", "n_clicks"),
109
- State("upload-audio", "contents"),
110
- State("upload-audio", "filename"),
111
- State("audio-url", "value"),
112
  prevent_initial_call=True
113
  )
114
  def update_transcription(n_clicks, contents, filename, url):
@@ -117,7 +140,7 @@ def update_transcription(n_clicks, contents, filename, url):
117
 
118
  def transcribe():
119
  try:
120
- return process_audio(contents, filename, url)
121
  except Exception as e:
122
  return f"An error occurred: {str(e)}"
123
 
 
14
  from pytube import YouTube
15
  from pydub import AudioSegment
16
  import google.generativeai as genai
17
+ from moviepy.editor import VideoFileClip
18
 
19
  # Initialize the Dash app
20
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
 
33
  except ValueError:
34
  return False
35
 
36
+ def download_media(url):
37
  if "youtube.com" in url or "youtu.be" in url:
38
  yt = YouTube(url)
39
+ stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
40
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
41
+ stream.download(output_path=os.path.dirname(temp_file.name), filename=temp_file.name)
42
  return temp_file.name
43
  else:
44
  response = requests.get(url)
45
+ content_type = response.headers.get('content-type', '')
46
+ if 'video' in content_type:
47
+ suffix = '.mp4'
48
+ elif 'audio' in content_type:
49
+ suffix = '.mp3'
50
+ else:
51
+ suffix = ''
52
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
53
  temp_file.write(response.content)
54
  return temp_file.name
55
 
56
+ def extract_audio(file_path):
57
+ video = VideoFileClip(file_path)
58
+ audio = video.audio
59
+ audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
60
+ audio.write_audiofile(audio_file.name)
61
+ video.close()
62
+ audio.close()
63
+ return audio_file.name
64
+
65
  def transcribe_audio(file_path):
66
  with open(file_path, "rb") as audio_file:
67
  audio_data = audio_file.read()
 
69
  response = model.generate_content(audio_data)
70
  return response.text
71
 
72
+ def process_media(contents, filename, url):
73
  if contents:
74
  content_type, content_string = contents.split(',')
75
  decoded = base64.b64decode(content_string)
76
+ suffix = os.path.splitext(filename)[1]
77
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
78
  temp_file.write(decoded)
79
  temp_file_path = temp_file.name
80
  elif url:
81
+ temp_file_path = download_media(url)
82
  else:
83
  raise ValueError("No input provided")
84
 
85
  try:
86
+ if temp_file_path.lower().endswith(('.mp4', '.avi', '.mov', '.flv', '.wmv')):
87
+ audio_file_path = extract_audio(temp_file_path)
88
+ transcript = transcribe_audio(audio_file_path)
89
+ os.unlink(audio_file_path)
90
+ else:
91
+ transcript = transcribe_audio(temp_file_path)
92
  finally:
93
  os.unlink(temp_file_path)
94
 
95
  return transcript
96
 
97
  app.layout = dbc.Container([
98
+ html.H1("Audio/Video Transcription App", className="text-center my-4"),
99
  dbc.Card([
100
  dbc.CardBody([
101
  dcc.Upload(
102
+ id='upload-media',
103
  children=html.Div([
104
  'Drag and Drop or ',
105
+ html.A('Select Audio/Video File')
106
  ]),
107
  style={
108
  'width': '100%',
 
116
  },
117
  multiple=False
118
  ),
119
+ dbc.Input(id="media-url", type="text", placeholder="Enter audio/video URL or YouTube link", className="my-3"),
120
  dbc.Button("Transcribe", id="transcribe-button", color="primary", className="w-100 mb-3"),
121
  dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
122
  dbc.Button("Download Transcript", id="download-button", color="secondary", className="w-100 mt-3", style={'display': 'none'}),
 
129
  Output("transcription-output", "children"),
130
  Output("download-button", "style"),
131
  Input("transcribe-button", "n_clicks"),
132
+ State("upload-media", "contents"),
133
+ State("upload-media", "filename"),
134
+ State("media-url", "value"),
135
  prevent_initial_call=True
136
  )
137
  def update_transcription(n_clicks, contents, filename, url):
 
140
 
141
  def transcribe():
142
  try:
143
+ return process_media(contents, filename, url)
144
  except Exception as e:
145
  return f"An error occurred: {str(e)}"
146