Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ from dash.exceptions import PreventUpdate
|
|
| 14 |
import requests
|
| 15 |
from pytube import YouTube
|
| 16 |
from pydub import AudioSegment
|
| 17 |
-
import
|
| 18 |
|
| 19 |
# Try different import statements for moviepy
|
| 20 |
try:
|
|
@@ -38,16 +38,13 @@ logger = logging.getLogger(__name__)
|
|
| 38 |
# Initialize the Dash app
|
| 39 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 40 |
|
| 41 |
-
# Retrieve the
|
| 42 |
-
|
| 43 |
-
if not
|
| 44 |
-
logger.error("
|
| 45 |
-
raise ValueError("
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Initialize Gemini model
|
| 50 |
-
model = genai.GenerativeModel('gemini-2.0-flash-lite')
|
| 51 |
|
| 52 |
def is_valid_url(url):
|
| 53 |
try:
|
|
@@ -101,6 +98,17 @@ def extract_audio(file_path):
|
|
| 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,20 +129,14 @@ def process_media(contents, filename, url):
|
|
| 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 |
-
|
| 125 |
-
audio_data = audio_file.read()
|
| 126 |
os.unlink(audio_file_path)
|
| 127 |
else:
|
| 128 |
-
logger.info("Audio file detected,
|
| 129 |
-
|
| 130 |
-
audio_data = audio_file.read()
|
| 131 |
|
| 132 |
os.unlink(temp_file_path)
|
| 133 |
-
|
| 134 |
-
# Use the audio data directly with the Gemini model
|
| 135 |
-
response = model.generate_content(audio_data)
|
| 136 |
-
logger.info("Transcription completed successfully")
|
| 137 |
-
return response.text
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"Error in process_media: {str(e)}")
|
| 140 |
raise
|
|
|
|
| 14 |
import requests
|
| 15 |
from pytube import YouTube
|
| 16 |
from pydub import AudioSegment
|
| 17 |
+
import openai
|
| 18 |
|
| 19 |
# Try different import statements for moviepy
|
| 20 |
try:
|
|
|
|
| 38 |
# Initialize the Dash app
|
| 39 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 40 |
|
| 41 |
+
# Retrieve the OpenAI API key from Hugging Face Spaces
|
| 42 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 43 |
+
if not OPENAI_API_KEY:
|
| 44 |
+
logger.error("OPENAI_API_KEY not found in environment variables")
|
| 45 |
+
raise ValueError("OPENAI_API_KEY not set")
|
| 46 |
|
| 47 |
+
openai.api_key = OPENAI_API_KEY
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def is_valid_url(url):
|
| 50 |
try:
|
|
|
|
| 98 |
logger.error(f"Error extracting audio: {str(e)}")
|
| 99 |
raise
|
| 100 |
|
| 101 |
+
def transcribe_audio(file_path):
|
| 102 |
+
logger.info(f"Transcribing audio: {file_path}")
|
| 103 |
+
try:
|
| 104 |
+
with open(file_path, "rb") as audio_file:
|
| 105 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 106 |
+
logger.info("Transcription completed successfully")
|
| 107 |
+
return transcript["text"]
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logger.error(f"Error during transcription: {str(e)}")
|
| 110 |
+
raise
|
| 111 |
+
|
| 112 |
def process_media(contents, filename, url):
|
| 113 |
logger.info("Starting media processing")
|
| 114 |
try:
|
|
|
|
| 129 |
if temp_file_path.lower().endswith(('.mp4', '.avi', '.mov', '.flv', '.wmv')):
|
| 130 |
logger.info("Video file detected, extracting audio")
|
| 131 |
audio_file_path = extract_audio(temp_file_path)
|
| 132 |
+
transcript = transcribe_audio(audio_file_path)
|
|
|
|
| 133 |
os.unlink(audio_file_path)
|
| 134 |
else:
|
| 135 |
+
logger.info("Audio file detected, transcribing directly")
|
| 136 |
+
transcript = transcribe_audio(temp_file_path)
|
|
|
|
| 137 |
|
| 138 |
os.unlink(temp_file_path)
|
| 139 |
+
return transcript
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
logger.error(f"Error in process_media: {str(e)}")
|
| 142 |
raise
|