Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,15 @@
|
|
| 2 |
AI Graduate Job-Matching Platform
|
| 3 |
Complete Hugging Face Spaces Deployment Version
|
| 4 |
Developer: Najaf Ali Sharqi
|
| 5 |
-
MoviePy-free (uses
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
| 10 |
import tempfile
|
| 11 |
-
import
|
| 12 |
-
from groq import Groq
|
| 13 |
-
import imageio
|
| 14 |
import math
|
|
|
|
| 15 |
|
| 16 |
# ============================================================================
|
| 17 |
# CONFIGURATION
|
|
@@ -20,23 +19,35 @@ import math
|
|
| 20 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 21 |
|
| 22 |
TRANSCRIPTION_MODEL = "whisper-large-v3-turbo"
|
| 23 |
-
CHAT_MODEL = "llama-3.3-70b-versatile"
|
| 24 |
CHUNK_SIZE = 600 # 10 min in seconds
|
| 25 |
|
| 26 |
# ============================================================================
|
| 27 |
-
#
|
| 28 |
# ============================================================================
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def extract_audio_chunks(video_path, chunk_size=CHUNK_SIZE):
|
| 31 |
"""
|
| 32 |
Extract audio from video and split into chunks
|
| 33 |
Returns list of audio file paths
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
fps = reader.get_meta_data()['fps']
|
| 38 |
-
nframes = reader.get_meta_data()['nframes']
|
| 39 |
-
duration = nframes / fps
|
| 40 |
num_chunks = math.ceil(duration / chunk_size)
|
| 41 |
audio_chunks = []
|
| 42 |
|
|
@@ -44,7 +55,8 @@ def extract_audio_chunks(video_path, chunk_size=CHUNK_SIZE):
|
|
| 44 |
temp_audio_full = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 45 |
audio_full_path = temp_audio_full.name
|
| 46 |
temp_audio_full.close()
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
# split audio into chunks
|
| 50 |
for i in range(num_chunks):
|
|
@@ -53,14 +65,14 @@ def extract_audio_chunks(video_path, chunk_size=CHUNK_SIZE):
|
|
| 53 |
temp_chunk = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 54 |
chunk_path = temp_chunk.name
|
| 55 |
temp_chunk.close()
|
| 56 |
-
os.system(f"ffmpeg -y -i
|
| 57 |
audio_chunks.append(chunk_path)
|
| 58 |
|
| 59 |
# remove full audio file
|
| 60 |
if os.path.exists(audio_full_path):
|
| 61 |
os.unlink(audio_full_path)
|
| 62 |
|
| 63 |
-
return audio_chunks
|
| 64 |
except Exception as e:
|
| 65 |
raise RuntimeError(f"Audio extraction failed: {str(e)}")
|
| 66 |
|
|
@@ -69,9 +81,6 @@ def extract_audio_chunks(video_path, chunk_size=CHUNK_SIZE):
|
|
| 69 |
# ============================================================================
|
| 70 |
|
| 71 |
def transcribe_audio_chunks(audio_chunks, language="English"):
|
| 72 |
-
"""
|
| 73 |
-
Transcribe a list of audio chunks
|
| 74 |
-
"""
|
| 75 |
transcript_text = ""
|
| 76 |
for chunk in audio_chunks:
|
| 77 |
try:
|
|
@@ -100,7 +109,7 @@ def process_video_ui(video_file, language):
|
|
| 100 |
if not video_file:
|
| 101 |
return "", "Please upload a video file." if lang=="English" else "براہ کرم ایک ویڈیو فائل اپ لوڈ کریں۔"
|
| 102 |
try:
|
| 103 |
-
audio_chunks
|
| 104 |
transcript = transcribe_audio_chunks(audio_chunks, language=lang)
|
| 105 |
return transcript, "✅ Success / کامیاب"
|
| 106 |
except Exception as e:
|
|
|
|
| 2 |
AI Graduate Job-Matching Platform
|
| 3 |
Complete Hugging Face Spaces Deployment Version
|
| 4 |
Developer: Najaf Ali Sharqi
|
| 5 |
+
MoviePy-free (uses ffmpeg + tempfile)
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
| 10 |
import tempfile
|
| 11 |
+
import subprocess
|
|
|
|
|
|
|
| 12 |
import math
|
| 13 |
+
from groq import Groq
|
| 14 |
|
| 15 |
# ============================================================================
|
| 16 |
# CONFIGURATION
|
|
|
|
| 19 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 20 |
|
| 21 |
TRANSCRIPTION_MODEL = "whisper-large-v3-turbo"
|
|
|
|
| 22 |
CHUNK_SIZE = 600 # 10 min in seconds
|
| 23 |
|
| 24 |
# ============================================================================
|
| 25 |
+
# UTILITY FUNCTIONS
|
| 26 |
# ============================================================================
|
| 27 |
|
| 28 |
+
def get_video_duration(video_path):
|
| 29 |
+
"""Get video duration using ffprobe (more reliable than imageio)"""
|
| 30 |
+
try:
|
| 31 |
+
result = subprocess.run(
|
| 32 |
+
["ffprobe", "-v", "error", "-show_entries",
|
| 33 |
+
"format=duration", "-of",
|
| 34 |
+
"default=noprint_wrappers=1:nokey=1", video_path],
|
| 35 |
+
stdout=subprocess.PIPE,
|
| 36 |
+
stderr=subprocess.STDOUT,
|
| 37 |
+
text=True
|
| 38 |
+
)
|
| 39 |
+
duration = float(result.stdout.strip())
|
| 40 |
+
return duration
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise RuntimeError(f"Failed to get video duration: {str(e)}")
|
| 43 |
+
|
| 44 |
def extract_audio_chunks(video_path, chunk_size=CHUNK_SIZE):
|
| 45 |
"""
|
| 46 |
Extract audio from video and split into chunks
|
| 47 |
Returns list of audio file paths
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
+
duration = get_video_duration(video_path)
|
|
|
|
|
|
|
|
|
|
| 51 |
num_chunks = math.ceil(duration / chunk_size)
|
| 52 |
audio_chunks = []
|
| 53 |
|
|
|
|
| 55 |
temp_audio_full = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 56 |
audio_full_path = temp_audio_full.name
|
| 57 |
temp_audio_full.close()
|
| 58 |
+
|
| 59 |
+
os.system(f"ffmpeg -y -i \"{video_path}\" -q:a 0 -map a \"{audio_full_path}\"")
|
| 60 |
|
| 61 |
# split audio into chunks
|
| 62 |
for i in range(num_chunks):
|
|
|
|
| 65 |
temp_chunk = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 66 |
chunk_path = temp_chunk.name
|
| 67 |
temp_chunk.close()
|
| 68 |
+
os.system(f"ffmpeg -y -i \"{audio_full_path}\" -ss {start} -to {end} -c copy \"{chunk_path}\"")
|
| 69 |
audio_chunks.append(chunk_path)
|
| 70 |
|
| 71 |
# remove full audio file
|
| 72 |
if os.path.exists(audio_full_path):
|
| 73 |
os.unlink(audio_full_path)
|
| 74 |
|
| 75 |
+
return audio_chunks
|
| 76 |
except Exception as e:
|
| 77 |
raise RuntimeError(f"Audio extraction failed: {str(e)}")
|
| 78 |
|
|
|
|
| 81 |
# ============================================================================
|
| 82 |
|
| 83 |
def transcribe_audio_chunks(audio_chunks, language="English"):
|
|
|
|
|
|
|
|
|
|
| 84 |
transcript_text = ""
|
| 85 |
for chunk in audio_chunks:
|
| 86 |
try:
|
|
|
|
| 109 |
if not video_file:
|
| 110 |
return "", "Please upload a video file." if lang=="English" else "براہ کرم ایک ویڈیو فائل اپ لوڈ کریں۔"
|
| 111 |
try:
|
| 112 |
+
audio_chunks = extract_audio_chunks(video_file.name)
|
| 113 |
transcript = transcribe_audio_chunks(audio_chunks, language=lang)
|
| 114 |
return transcript, "✅ Success / کامیاب"
|
| 115 |
except Exception as e:
|