Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import yt_dlp
|
|
@@ -7,76 +10,166 @@ import requests
|
|
| 7 |
import uuid
|
| 8 |
import re
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def process_video(video_url):
|
| 14 |
try:
|
| 15 |
clean_old_files()
|
| 16 |
|
| 17 |
-
if video_url.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
else:
|
| 26 |
-
print("Downloading video from direct link...")
|
| 27 |
-
video_path = download_direct_video(video_url)
|
| 28 |
|
| 29 |
-
if not video_path
|
| 30 |
-
return "
|
| 31 |
|
| 32 |
print("Extracting audio...")
|
| 33 |
audio_path = extract_audio(video_path)
|
| 34 |
|
| 35 |
-
print("Transcribing...")
|
| 36 |
transcription = transcribe_audio(audio_path)
|
| 37 |
|
| 38 |
print("Classifying content...")
|
| 39 |
category = classify_content(transcription)
|
| 40 |
|
| 41 |
-
return
|
| 42 |
-
|
| 43 |
-
"category": category,
|
| 44 |
-
"video_path": video_path,
|
| 45 |
-
"audio_path": audio_path
|
| 46 |
-
}
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"
|
| 49 |
|
| 50 |
-
# Gradio
|
| 51 |
-
with gr.Blocks(title="
|
| 52 |
gr.Markdown("""
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
""")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
url_input = gr.Textbox(
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
with gr.Column():
|
| 63 |
-
transcription_output = gr.Textbox(
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with gr.Column():
|
| 67 |
-
video_preview = gr.Video(
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 70 |
submit_btn.click(
|
| 71 |
fn=process_video,
|
| 72 |
inputs=url_input,
|
| 73 |
-
outputs=
|
| 74 |
-
"transcription": transcription_output,
|
| 75 |
-
"category": category_output,
|
| 76 |
-
"video_path": video_preview,
|
| 77 |
-
"audio_path": audio_preview
|
| 78 |
-
}
|
| 79 |
)
|
| 80 |
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# First install required packages (this should be at the very top)
|
| 2 |
+
!pip install --upgrade gradio yt-dlp openai-whisper transformers ffmpeg-python pydub
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
import yt_dlp
|
|
|
|
| 10 |
import uuid
|
| 11 |
import re
|
| 12 |
|
| 13 |
+
# Delete old files
|
| 14 |
+
def clean_old_files():
|
| 15 |
+
files = ["video.mp4", "audio.mp3", "transcription.txt"]
|
| 16 |
+
for file in files:
|
| 17 |
+
if os.path.exists(file):
|
| 18 |
+
os.remove(file)
|
| 19 |
+
|
| 20 |
+
# Download TikTok video
|
| 21 |
+
def download_video(video_url):
|
| 22 |
+
unique_name = f"video_{uuid.uuid4().hex[:8]}.mp4"
|
| 23 |
+
|
| 24 |
+
ydl_opts = {
|
| 25 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]',
|
| 26 |
+
'outtmpl': unique_name,
|
| 27 |
+
'quiet': True,
|
| 28 |
+
'no_warnings': True,
|
| 29 |
+
'extractor_args': {
|
| 30 |
+
'tiktok': {
|
| 31 |
+
'skip_watermark': True
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 38 |
+
ydl.download([video_url])
|
| 39 |
+
return unique_name
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error downloading video: {e}")
|
| 42 |
+
return None
|
| 43 |
|
| 44 |
+
# Extract audio from video
|
| 45 |
+
def extract_audio(video_path):
|
| 46 |
+
audio_path = "audio.mp3"
|
| 47 |
+
os.system(f"ffmpeg -i \"{video_path}\" -vn -acodec libmp3lame -q:a 3 \"{audio_path}\" -y")
|
| 48 |
+
if not os.path.exists(audio_path):
|
| 49 |
+
raise RuntimeError("Error: Failed to extract audio.")
|
| 50 |
+
return audio_path
|
| 51 |
+
|
| 52 |
+
# Convert audio to text
|
| 53 |
+
def transcribe_audio(audio_path):
|
| 54 |
+
try:
|
| 55 |
+
model = whisper.load_model("base")
|
| 56 |
+
result = model.transcribe(audio_path)
|
| 57 |
+
return result['text']
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Transcription error: {str(e)}"
|
| 60 |
+
|
| 61 |
+
# Classify content
|
| 62 |
+
def classify_content(text):
|
| 63 |
+
try:
|
| 64 |
+
if not text or len(text.strip()) == 0:
|
| 65 |
+
return "No text to classify"
|
| 66 |
+
|
| 67 |
+
classifier = pipeline("zero-shot-classification",
|
| 68 |
+
model="facebook/bart-large-mnli")
|
| 69 |
+
|
| 70 |
+
labels = ["Challenge", "Comedy", "Dance", "Educational", "TikTok Trend",
|
| 71 |
+
"Music", "Lifestyle", "Beauty", "Cooking", "Fashion"]
|
| 72 |
+
|
| 73 |
+
clean_text = ' '.join(text.split()[:500])
|
| 74 |
+
result = classifier(clean_text,
|
| 75 |
+
candidate_labels=labels,
|
| 76 |
+
hypothesis_template="This content is about {}.")
|
| 77 |
+
|
| 78 |
+
return f"{result['labels'][0]} (Confidence: {result['scores'][0]:.2f})"
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"Classification error: {str(e)}"
|
| 81 |
+
|
| 82 |
+
# Main video processing function
|
| 83 |
def process_video(video_url):
|
| 84 |
try:
|
| 85 |
clean_old_files()
|
| 86 |
|
| 87 |
+
if not video_url or len(video_url.strip()) == 0:
|
| 88 |
+
return ["Please enter a valid video URL", "", None, None]
|
| 89 |
+
|
| 90 |
+
if "tiktok.com" not in video_url and "vm.tiktok.com" not in video_url:
|
| 91 |
+
return ["This app is for TikTok links only", "", None, None]
|
| 92 |
+
|
| 93 |
+
print(f"Downloading video: {video_url}")
|
| 94 |
+
video_path = download_video(video_url)
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
if not video_path:
|
| 97 |
+
return ["Failed to download video. Please check the URL.", "", None, None]
|
| 98 |
|
| 99 |
print("Extracting audio...")
|
| 100 |
audio_path = extract_audio(video_path)
|
| 101 |
|
| 102 |
+
print("Transcribing audio...")
|
| 103 |
transcription = transcribe_audio(audio_path)
|
| 104 |
|
| 105 |
print("Classifying content...")
|
| 106 |
category = classify_content(transcription)
|
| 107 |
|
| 108 |
+
return [transcription, category, video_path, audio_path]
|
| 109 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
+
return [f"Processing error: {str(e)}", "", None, None]
|
| 112 |
|
| 113 |
+
# Gradio interface
|
| 114 |
+
with gr.Blocks(title="TikTok Content Analyzer") as demo:
|
| 115 |
gr.Markdown("""
|
| 116 |
+
# 🎬 TikTok Content Analyzer
|
| 117 |
+
Enter a TikTok video URL to get transcription and content classification
|
| 118 |
""")
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
+
url_input = gr.Textbox(
|
| 122 |
+
label="TikTok URL",
|
| 123 |
+
placeholder="Enter TikTok video URL here...",
|
| 124 |
+
scale=4
|
| 125 |
+
)
|
| 126 |
+
submit_btn = gr.Button("Analyze Video", variant="primary", scale=1)
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
| 130 |
+
transcription_output = gr.Textbox(
|
| 131 |
+
label="Extracted Text",
|
| 132 |
+
interactive=True,
|
| 133 |
+
lines=10,
|
| 134 |
+
max_lines=20
|
| 135 |
+
)
|
| 136 |
+
category_output = gr.Textbox(
|
| 137 |
+
label="Content Category",
|
| 138 |
+
interactive=False
|
| 139 |
+
)
|
| 140 |
|
| 141 |
with gr.Column():
|
| 142 |
+
video_preview = gr.Video(
|
| 143 |
+
label="Downloaded Video",
|
| 144 |
+
interactive=False
|
| 145 |
+
)
|
| 146 |
+
audio_preview = gr.Audio(
|
| 147 |
+
label="Extracted Audio",
|
| 148 |
+
interactive=False
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# TikTok URL examples
|
| 152 |
+
gr.Examples(
|
| 153 |
+
examples=[
|
| 154 |
+
["https://www.tiktok.com/@example/video/123456789"],
|
| 155 |
+
["https://www.tiktok.com/@user2/video/987654321"],
|
| 156 |
+
["https://vm.tiktok.com/ZMexample/"]
|
| 157 |
+
],
|
| 158 |
+
inputs=url_input,
|
| 159 |
+
label="Try these examples"
|
| 160 |
+
)
|
| 161 |
|
| 162 |
+
# Button click event
|
| 163 |
submit_btn.click(
|
| 164 |
fn=process_video,
|
| 165 |
inputs=url_input,
|
| 166 |
+
outputs=[transcription_output, category_output, video_preview, audio_preview]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
+
# Launch the app
|
| 170 |
if __name__ == "__main__":
|
| 171 |
+
demo.launch(
|
| 172 |
+
server_name="0.0.0.0",
|
| 173 |
+
server_port=7860,
|
| 174 |
+
share=False
|
| 175 |
+
)
|