Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,43 @@ import gradio as gr
|
|
4 |
import re
|
5 |
import sys
|
6 |
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Try to import required packages with error handling
|
9 |
try:
|
@@ -13,16 +50,44 @@ except ImportError as e:
|
|
13 |
YT_DLP_AVAILABLE = False
|
14 |
print(f"yt-dlp import error: {e}")
|
15 |
|
|
|
|
|
|
|
|
|
16 |
try:
|
17 |
import whisper
|
18 |
WHISPER_AVAILABLE = True
|
|
|
|
|
19 |
except ImportError as e:
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
print(f"Python version: {sys.version}")
|
|
|
24 |
print(f"yt-dlp available: {YT_DLP_AVAILABLE}")
|
25 |
-
print(f"whisper available: {WHISPER_AVAILABLE}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def download_audio(url, cookies_file_path=None):
|
28 |
"""Download audio from YouTube URL and return the file path"""
|
@@ -64,16 +129,29 @@ def download_audio(url, cookies_file_path=None):
|
|
64 |
def transcribe_audio(file_path):
|
65 |
"""Transcribe audio file using Whisper"""
|
66 |
if not WHISPER_AVAILABLE:
|
67 |
-
raise Exception("OpenAI Whisper is not available. Please
|
68 |
|
69 |
try:
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
except Exception as e:
|
75 |
raise Exception(f"Failed to transcribe audio: {str(e)}")
|
76 |
|
|
|
77 |
def extract_stock_info_simple(text):
|
78 |
"""Extract stock information using simple pattern matching"""
|
79 |
try:
|
@@ -168,10 +246,10 @@ def process_video(url, cookies_file, progress=gr.Progress()):
|
|
168 |
|
169 |
# Check if required packages are available
|
170 |
if not YT_DLP_AVAILABLE:
|
171 |
-
return "Error: yt-dlp is not installed properly. Please
|
172 |
|
173 |
if not WHISPER_AVAILABLE:
|
174 |
-
return "Error: OpenAI Whisper is not installed properly. Please
|
175 |
|
176 |
if not url or not url.strip():
|
177 |
return "Please provide a valid YouTube URL", "", "β Error: Invalid URL"
|
@@ -251,6 +329,21 @@ with gr.Blocks(
|
|
251 |
|
252 |
with gr.Row():
|
253 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
# Cookies file upload
|
255 |
cookies_input = gr.File(
|
256 |
label="πͺ Upload Cookies File (cookies.txt)",
|
@@ -311,6 +404,15 @@ with gr.Blocks(
|
|
311 |
)
|
312 |
|
313 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
process_btn.click(
|
315 |
fn=process_video,
|
316 |
inputs=[url_input, cookies_input],
|
@@ -329,7 +431,41 @@ with gr.Blocks(
|
|
329 |
)
|
330 |
|
331 |
gr.Markdown("""
|
332 |
-
### π§ Troubleshooting:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
- **Bot Detection Error**: Upload your cookies.txt file
|
334 |
- **No Audio Found**: Check if video has audio track
|
335 |
- **Transcription Failed**: Video might be too long or audio quality poor
|
|
|
4 |
import re
|
5 |
import sys
|
6 |
import shutil
|
7 |
+
import importlib.util
|
8 |
+
|
9 |
+
def check_requirements():
|
10 |
+
"""Check if all required packages are installed and return status"""
|
11 |
+
requirements_status = []
|
12 |
+
|
13 |
+
packages = [
|
14 |
+
('gradio', 'gradio'),
|
15 |
+
('yt-dlp', 'yt_dlp'),
|
16 |
+
('openai-whisper', 'whisper'),
|
17 |
+
('torch', 'torch'),
|
18 |
+
('torchaudio', 'torchaudio'),
|
19 |
+
('numpy', 'numpy'),
|
20 |
+
('regex', 'regex'),
|
21 |
+
]
|
22 |
+
|
23 |
+
for package_name, import_name in packages:
|
24 |
+
try:
|
25 |
+
spec = importlib.util.find_spec(import_name)
|
26 |
+
if spec is None:
|
27 |
+
requirements_status.append(f"β {package_name}: Not found")
|
28 |
+
continue
|
29 |
+
|
30 |
+
module = importlib.import_module(import_name)
|
31 |
+
version = getattr(module, '__version__', 'Unknown version')
|
32 |
+
requirements_status.append(f"β
{package_name}: {version}")
|
33 |
+
|
34 |
+
except ImportError as e:
|
35 |
+
requirements_status.append(f"β {package_name}: Import error - {str(e)}")
|
36 |
+
except Exception as e:
|
37 |
+
requirements_status.append(f"β οΈ {package_name}: Found but error - {str(e)}")
|
38 |
+
|
39 |
+
# Add Python info
|
40 |
+
requirements_status.append(f"\nπ Python: {sys.version}")
|
41 |
+
requirements_status.append(f"π Python executable: {sys.executable}")
|
42 |
+
|
43 |
+
return "\n".join(requirements_status)
|
44 |
|
45 |
# Try to import required packages with error handling
|
46 |
try:
|
|
|
50 |
YT_DLP_AVAILABLE = False
|
51 |
print(f"yt-dlp import error: {e}")
|
52 |
|
53 |
+
# Try multiple whisper import methods
|
54 |
+
WHISPER_AVAILABLE = False
|
55 |
+
WHISPER_TYPE = None
|
56 |
+
|
57 |
try:
|
58 |
import whisper
|
59 |
WHISPER_AVAILABLE = True
|
60 |
+
WHISPER_TYPE = "openai-whisper"
|
61 |
+
print("Using OpenAI Whisper")
|
62 |
except ImportError as e:
|
63 |
+
print(f"OpenAI Whisper import error: {e}")
|
64 |
+
try:
|
65 |
+
from transformers import pipeline
|
66 |
+
WHISPER_AVAILABLE = True
|
67 |
+
WHISPER_TYPE = "transformers"
|
68 |
+
print("Using Transformers Whisper")
|
69 |
+
except ImportError as e2:
|
70 |
+
print(f"Transformers Whisper import error: {e2}")
|
71 |
|
72 |
print(f"Python version: {sys.version}")
|
73 |
+
print(f"Python executable: {sys.executable}")
|
74 |
print(f"yt-dlp available: {YT_DLP_AVAILABLE}")
|
75 |
+
print(f"whisper available: {WHISPER_AVAILABLE} (type: {WHISPER_TYPE})")
|
76 |
+
|
77 |
+
# Additional diagnostics
|
78 |
+
if YT_DLP_AVAILABLE:
|
79 |
+
try:
|
80 |
+
from yt_dlp import YoutubeDL
|
81 |
+
print(f"yt-dlp version: {YoutubeDL().__class__.__module__}")
|
82 |
+
except:
|
83 |
+
pass
|
84 |
+
|
85 |
+
if WHISPER_AVAILABLE and WHISPER_TYPE == "openai-whisper":
|
86 |
+
try:
|
87 |
+
import whisper
|
88 |
+
print(f"whisper version: {whisper.__version__}")
|
89 |
+
except:
|
90 |
+
pass
|
91 |
|
92 |
def download_audio(url, cookies_file_path=None):
|
93 |
"""Download audio from YouTube URL and return the file path"""
|
|
|
129 |
def transcribe_audio(file_path):
|
130 |
"""Transcribe audio file using Whisper"""
|
131 |
if not WHISPER_AVAILABLE:
|
132 |
+
raise Exception("OpenAI Whisper is not available. Please install it using: pip install openai-whisper")
|
133 |
|
134 |
try:
|
135 |
+
if WHISPER_TYPE == "openai-whisper":
|
136 |
+
# Use OpenAI Whisper
|
137 |
+
model = whisper.load_model("tiny")
|
138 |
+
result = model.transcribe(file_path)
|
139 |
+
return result["text"]
|
140 |
+
|
141 |
+
elif WHISPER_TYPE == "transformers":
|
142 |
+
# Use Transformers Whisper
|
143 |
+
from transformers import pipeline
|
144 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
145 |
+
result = transcriber(file_path)
|
146 |
+
return result["text"]
|
147 |
+
|
148 |
+
else:
|
149 |
+
raise Exception("No compatible Whisper installation found")
|
150 |
+
|
151 |
except Exception as e:
|
152 |
raise Exception(f"Failed to transcribe audio: {str(e)}")
|
153 |
|
154 |
+
|
155 |
def extract_stock_info_simple(text):
|
156 |
"""Extract stock information using simple pattern matching"""
|
157 |
try:
|
|
|
246 |
|
247 |
# Check if required packages are available
|
248 |
if not YT_DLP_AVAILABLE:
|
249 |
+
return "Error: yt-dlp is not installed properly. Please install it using: pip install yt-dlp", "", "β Error: Missing yt-dlp"
|
250 |
|
251 |
if not WHISPER_AVAILABLE:
|
252 |
+
return "Error: OpenAI Whisper is not installed properly. Please install it using: pip install openai-whisper", "", "β Error: Missing Whisper"
|
253 |
|
254 |
if not url or not url.strip():
|
255 |
return "Please provide a valid YouTube URL", "", "β Error: Invalid URL"
|
|
|
329 |
|
330 |
with gr.Row():
|
331 |
with gr.Column(scale=1):
|
332 |
+
# Requirements check button
|
333 |
+
gr.Markdown("### π System Check")
|
334 |
+
check_req_btn = gr.Button(
|
335 |
+
"Check Requirements",
|
336 |
+
variant="secondary",
|
337 |
+
size="sm"
|
338 |
+
)
|
339 |
+
|
340 |
+
requirements_output = gr.Textbox(
|
341 |
+
label="π Requirements Status",
|
342 |
+
lines=10,
|
343 |
+
interactive=False,
|
344 |
+
visible=False
|
345 |
+
)
|
346 |
+
|
347 |
# Cookies file upload
|
348 |
cookies_input = gr.File(
|
349 |
label="πͺ Upload Cookies File (cookies.txt)",
|
|
|
404 |
)
|
405 |
|
406 |
# Event handlers
|
407 |
+
def show_requirements():
|
408 |
+
status = check_requirements()
|
409 |
+
return gr.update(value=status, visible=True)
|
410 |
+
|
411 |
+
check_req_btn.click(
|
412 |
+
fn=show_requirements,
|
413 |
+
outputs=[requirements_output]
|
414 |
+
)
|
415 |
+
|
416 |
process_btn.click(
|
417 |
fn=process_video,
|
418 |
inputs=[url_input, cookies_input],
|
|
|
431 |
)
|
432 |
|
433 |
gr.Markdown("""
|
434 |
+
### π§ Installation & Troubleshooting:
|
435 |
+
|
436 |
+
**Step 1: Click "Check Requirements" button above to see what's missing**
|
437 |
+
|
438 |
+
**If you get "Whisper Missing" error:**
|
439 |
+
```bash
|
440 |
+
pip install openai-whisper
|
441 |
+
```
|
442 |
+
|
443 |
+
**If you get "yt-dlp Missing" error:**
|
444 |
+
```bash
|
445 |
+
pip install yt-dlp
|
446 |
+
```
|
447 |
+
|
448 |
+
**Install all requirements at once:**
|
449 |
+
```bash
|
450 |
+
pip install gradio==4.44.0 yt-dlp==2023.12.30 openai-whisper==20231117 torch==2.1.0 torchaudio==2.1.0 numpy==1.24.3 regex==2023.8.8
|
451 |
+
```
|
452 |
+
|
453 |
+
**Alternative Whisper installation:**
|
454 |
+
```bash
|
455 |
+
pip install transformers torch torchaudio
|
456 |
+
```
|
457 |
+
|
458 |
+
**If using virtual environment:**
|
459 |
+
```bash
|
460 |
+
# Create and activate virtual environment first
|
461 |
+
python -m venv myenv
|
462 |
+
# Windows: myenv\\Scripts\\activate
|
463 |
+
# Mac/Linux: source myenv/bin/activate
|
464 |
+
# Then install packages
|
465 |
+
pip install -r requirements.txt
|
466 |
+
```
|
467 |
+
|
468 |
+
**Other Issues:**
|
469 |
- **Bot Detection Error**: Upload your cookies.txt file
|
470 |
- **No Audio Found**: Check if video has audio track
|
471 |
- **Transcription Failed**: Video might be too long or audio quality poor
|