Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import logging
|
|
| 7 |
from urllib.parse import urlparse
|
| 8 |
|
| 9 |
import dash
|
| 10 |
-
from dash import dcc, html, Input, Output, State
|
| 11 |
import dash_bootstrap_components as dbc
|
| 12 |
from dash.exceptions import PreventUpdate
|
| 13 |
|
|
@@ -101,19 +101,6 @@ def extract_audio(file_path):
|
|
| 101 |
logger.error(f"Error extracting audio: {str(e)}")
|
| 102 |
raise
|
| 103 |
|
| 104 |
-
def transcribe_audio(file_path):
|
| 105 |
-
logger.info(f"Transcribing audio: {file_path}")
|
| 106 |
-
try:
|
| 107 |
-
with open(file_path, "rb") as audio_file:
|
| 108 |
-
audio_data = audio_file.read()
|
| 109 |
-
|
| 110 |
-
response = model.generate_content(audio_data)
|
| 111 |
-
logger.info("Transcription completed successfully")
|
| 112 |
-
return response.text
|
| 113 |
-
except Exception as e:
|
| 114 |
-
logger.error(f"Error during transcription: {str(e)}")
|
| 115 |
-
raise
|
| 116 |
-
|
| 117 |
def process_media(contents, filename, url):
|
| 118 |
logger.info("Starting media processing")
|
| 119 |
try:
|
|
@@ -134,14 +121,22 @@ def process_media(contents, filename, url):
|
|
| 134 |
if temp_file_path.lower().endswith(('.mp4', '.avi', '.mov', '.flv', '.wmv')):
|
| 135 |
logger.info("Video file detected, extracting audio")
|
| 136 |
audio_file_path = extract_audio(temp_file_path)
|
| 137 |
-
|
|
|
|
| 138 |
os.unlink(audio_file_path)
|
| 139 |
else:
|
| 140 |
-
logger.info("Audio file detected,
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
os.unlink(temp_file_path)
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
except Exception as e:
|
| 146 |
logger.error(f"Error in process_media: {str(e)}")
|
| 147 |
raise
|
|
@@ -168,56 +163,77 @@ app.layout = dbc.Container([
|
|
| 168 |
},
|
| 169 |
multiple=False
|
| 170 |
),
|
|
|
|
| 171 |
dbc.Input(id="media-url", type="text", placeholder="Enter audio/video URL or YouTube link", className="my-3"),
|
| 172 |
dbc.Button("Transcribe", id="transcribe-button", color="primary", className="w-100 mb-3"),
|
| 173 |
dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
|
|
|
|
| 174 |
dbc.Button("Download Transcript", id="download-button", color="secondary", className="w-100 mt-3", style={'display': 'none'}),
|
| 175 |
-
dcc.Download(id="download-transcript")
|
|
|
|
| 176 |
])
|
| 177 |
])
|
| 178 |
])
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
@app.callback(
|
| 181 |
Output("transcription-output", "children"),
|
| 182 |
Output("download-button", "style"),
|
|
|
|
|
|
|
| 183 |
Input("transcribe-button", "n_clicks"),
|
|
|
|
| 184 |
State("upload-media", "contents"),
|
| 185 |
State("upload-media", "filename"),
|
| 186 |
State("media-url", "value"),
|
| 187 |
prevent_initial_call=True
|
| 188 |
)
|
| 189 |
-
def update_transcription(n_clicks, contents, filename, url):
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
try:
|
| 195 |
-
return process_media(contents, filename, url)
|
| 196 |
-
except Exception as e:
|
| 197 |
-
logger.error(f"Transcription failed: {str(e)}")
|
| 198 |
-
return f"An error occurred: {str(e)}"
|
| 199 |
-
|
| 200 |
-
thread = threading.Thread(target=transcribe)
|
| 201 |
-
thread.start()
|
| 202 |
-
thread.join(timeout=600) # 10 minutes timeout
|
| 203 |
-
|
| 204 |
-
if thread.is_alive():
|
| 205 |
-
logger.warning("Transcription timed out after 10 minutes")
|
| 206 |
-
return "Transcription timed out after 10 minutes", {'display': 'none'}
|
| 207 |
-
|
| 208 |
-
transcript = getattr(thread, 'result', "Transcription failed")
|
| 209 |
-
|
| 210 |
-
if transcript and not transcript.startswith("An error occurred"):
|
| 211 |
-
logger.info("Transcription successful")
|
| 212 |
-
return dbc.Card([
|
| 213 |
-
dbc.CardBody([
|
| 214 |
-
html.H5("Transcription Result"),
|
| 215 |
-
html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"})
|
| 216 |
-
])
|
| 217 |
-
]), {'display': 'block'}
|
| 218 |
-
else:
|
| 219 |
-
logger.error(f"Transcription failed: {transcript}")
|
| 220 |
-
return transcript, {'display': 'none'}
|
| 221 |
|
| 222 |
@app.callback(
|
| 223 |
Output("download-transcript", "data"),
|
|
|
|
| 7 |
from urllib.parse import urlparse
|
| 8 |
|
| 9 |
import dash
|
| 10 |
+
from dash import dcc, html, Input, Output, State, callback_context
|
| 11 |
import dash_bootstrap_components as dbc
|
| 12 |
from dash.exceptions import PreventUpdate
|
| 13 |
|
|
|
|
| 101 |
logger.error(f"Error extracting audio: {str(e)}")
|
| 102 |
raise
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def process_media(contents, filename, url):
|
| 105 |
logger.info("Starting media processing")
|
| 106 |
try:
|
|
|
|
| 121 |
if temp_file_path.lower().endswith(('.mp4', '.avi', '.mov', '.flv', '.wmv')):
|
| 122 |
logger.info("Video file detected, extracting audio")
|
| 123 |
audio_file_path = extract_audio(temp_file_path)
|
| 124 |
+
with open(audio_file_path, "rb") as audio_file:
|
| 125 |
+
audio_data = audio_file.read()
|
| 126 |
os.unlink(audio_file_path)
|
| 127 |
else:
|
| 128 |
+
logger.info("Audio file detected, reading directly")
|
| 129 |
+
with open(temp_file_path, "rb") as audio_file:
|
| 130 |
+
audio_data = audio_file.read()
|
| 131 |
|
| 132 |
os.unlink(temp_file_path)
|
| 133 |
+
|
| 134 |
+
# Create a Blob object from the audio data
|
| 135 |
+
audio_blob = genai.types.Blob(data=audio_data, mime_type="audio/wav")
|
| 136 |
+
|
| 137 |
+
response = model.generate_content(audio_blob)
|
| 138 |
+
logger.info("Transcription completed successfully")
|
| 139 |
+
return response.text
|
| 140 |
except Exception as e:
|
| 141 |
logger.error(f"Error in process_media: {str(e)}")
|
| 142 |
raise
|
|
|
|
| 163 |
},
|
| 164 |
multiple=False
|
| 165 |
),
|
| 166 |
+
html.Div(id='file-info', className="mt-2"),
|
| 167 |
dbc.Input(id="media-url", type="text", placeholder="Enter audio/video URL or YouTube link", className="my-3"),
|
| 168 |
dbc.Button("Transcribe", id="transcribe-button", color="primary", className="w-100 mb-3"),
|
| 169 |
dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
|
| 170 |
+
html.Div(id="progress-indicator", className="text-center mt-3"),
|
| 171 |
dbc.Button("Download Transcript", id="download-button", color="secondary", className="w-100 mt-3", style={'display': 'none'}),
|
| 172 |
+
dcc.Download(id="download-transcript"),
|
| 173 |
+
dcc.Interval(id='progress-interval', interval=500, n_intervals=0, disabled=True)
|
| 174 |
])
|
| 175 |
])
|
| 176 |
])
|
| 177 |
|
| 178 |
+
@app.callback(
|
| 179 |
+
Output("file-info", "children"),
|
| 180 |
+
Input("upload-media", "filename"),
|
| 181 |
+
Input("upload-media", "last_modified")
|
| 182 |
+
)
|
| 183 |
+
def update_file_info(filename, last_modified):
|
| 184 |
+
if filename is not None:
|
| 185 |
+
return f"File uploaded: {filename}"
|
| 186 |
+
return ""
|
| 187 |
+
|
| 188 |
@app.callback(
|
| 189 |
Output("transcription-output", "children"),
|
| 190 |
Output("download-button", "style"),
|
| 191 |
+
Output("progress-indicator", "children"),
|
| 192 |
+
Output("progress-interval", "disabled"),
|
| 193 |
Input("transcribe-button", "n_clicks"),
|
| 194 |
+
Input("progress-interval", "n_intervals"),
|
| 195 |
State("upload-media", "contents"),
|
| 196 |
State("upload-media", "filename"),
|
| 197 |
State("media-url", "value"),
|
| 198 |
prevent_initial_call=True
|
| 199 |
)
|
| 200 |
+
def update_transcription(n_clicks, n_intervals, contents, filename, url):
|
| 201 |
+
ctx = callback_context
|
| 202 |
+
if ctx.triggered_id == "transcribe-button":
|
| 203 |
+
if not contents and not url:
|
| 204 |
+
raise PreventUpdate
|
| 205 |
+
|
| 206 |
+
def transcribe():
|
| 207 |
+
try:
|
| 208 |
+
return process_media(contents, filename, url)
|
| 209 |
+
except Exception as e:
|
| 210 |
+
logger.error(f"Transcription failed: {str(e)}")
|
| 211 |
+
return f"An error occurred: {str(e)}"
|
| 212 |
+
|
| 213 |
+
thread = threading.Thread(target=transcribe)
|
| 214 |
+
thread.start()
|
| 215 |
+
return html.Div("Processing..."), {'display': 'none'}, "", False
|
| 216 |
+
|
| 217 |
+
elif ctx.triggered_id == "progress-interval":
|
| 218 |
+
dots = "." * (n_intervals % 4)
|
| 219 |
+
return html.Div("Processing" + dots), {'display': 'none'}, "", False
|
| 220 |
+
|
| 221 |
+
thread = threading.current_thread()
|
| 222 |
+
if hasattr(thread, 'result'):
|
| 223 |
+
transcript = thread.result
|
| 224 |
+
if transcript and not transcript.startswith("An error occurred"):
|
| 225 |
+
logger.info("Transcription successful")
|
| 226 |
+
return dbc.Card([
|
| 227 |
+
dbc.CardBody([
|
| 228 |
+
html.H5("Transcription Result"),
|
| 229 |
+
html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"})
|
| 230 |
+
])
|
| 231 |
+
]), {'display': 'block'}, "", True
|
| 232 |
+
else:
|
| 233 |
+
logger.error(f"Transcription failed: {transcript}")
|
| 234 |
+
return transcript, {'display': 'none'}, "", True
|
| 235 |
|
| 236 |
+
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
@app.callback(
|
| 239 |
Output("download-transcript", "data"),
|