Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,66 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
import gradio as gr
|
| 4 |
+
from pytube import YouTube
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
import whisper
|
| 7 |
+
from textblob import TextBlob
|
| 8 |
|
| 9 |
+
# Step 1: Transcribe video
|
| 10 |
+
def transcribe_video_from_url(url: str) -> str:
|
| 11 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 12 |
+
video_path = os.path.join(tmpdir, "video.mp4")
|
| 13 |
+
audio_path = os.path.join(tmpdir, "audio.wav")
|
| 14 |
|
| 15 |
+
# Download the video
|
| 16 |
+
yt = YouTube(url)
|
| 17 |
+
stream = yt.streams.filter(file_extension='mp4', only_video=False).first()
|
| 18 |
+
stream.download(output_path=tmpdir, filename="video.mp4")
|
| 19 |
+
|
| 20 |
+
# Extract audio
|
| 21 |
+
video_clip = VideoFileClip(video_path)
|
| 22 |
+
video_clip.audio.write_audiofile(audio_path, logger=None)
|
| 23 |
+
video_clip.close()
|
| 24 |
+
|
| 25 |
+
# Transcribe with Whisper
|
| 26 |
+
model = whisper.load_model("base")
|
| 27 |
+
result = model.transcribe(audio_path)
|
| 28 |
+
return result["text"]
|
| 29 |
+
|
| 30 |
+
# Step 2: Analyze sentiment
|
| 31 |
+
def sentiment_analysis(text: str) -> dict:
|
| 32 |
+
blob = TextBlob(text)
|
| 33 |
+
sentiment = blob.sentiment
|
| 34 |
+
return {
|
| 35 |
+
"polarity": round(sentiment.polarity, 2),
|
| 36 |
+
"subjectivity": round(sentiment.subjectivity, 2),
|
| 37 |
+
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Step 3: Main function for Gradio
|
| 41 |
+
def analyze_sentiment_from_video(url: str) -> dict:
|
| 42 |
+
"""
|
| 43 |
+
Transcribe audio from a video URL and analyze sentiment.
|
| 44 |
+
"""
|
| 45 |
+
transcription = transcribe_video_from_url(url)
|
| 46 |
+
sentiment = sentiment_analysis(transcription)
|
| 47 |
+
sentiment["transcription"] = transcription
|
| 48 |
+
return sentiment
|
| 49 |
+
|
| 50 |
+
# Gradio interface
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=analyze_sentiment_from_video,
|
| 53 |
+
inputs=gr.Textbox(label="YouTube Video URL"),
|
| 54 |
+
outputs={
|
| 55 |
+
"assessment": gr.Textbox(label="Sentiment"),
|
| 56 |
+
"polarity": gr.Number(label="Polarity"),
|
| 57 |
+
"subjectivity": gr.Number(label="Subjectivity"),
|
| 58 |
+
"transcription": gr.Textbox(label="Transcribed Text", lines=10),
|
| 59 |
+
},
|
| 60 |
+
title="🎥 Sentiment Analysis from YouTube Video",
|
| 61 |
+
description="Enter a YouTube video URL. The app will transcribe its audio and analyze the sentiment of the spoken content."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch for Hugging Face Space
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch(mcp_server=True)
|