Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://www.youtube.com/watch?v=SYHPQ0rXzWM
|
| 2 |
+
|
| 3 |
+
import yt_dlp
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load the fine-tuned BERT model and tokenizer
|
| 9 |
+
model = BertForSequenceClassification.from_pretrained('./results/checkpoint-150')
|
| 10 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
import yt_dlp
|
| 14 |
+
|
| 15 |
+
def download_youtube_video(video_url, output_path='Youtube Videos/Recent.mp4'):
|
| 16 |
+
ydl_opts = {'format': 'bestaudio/best', 'outtmpl': output_path}
|
| 17 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 18 |
+
ydl.download([video_url])
|
| 19 |
+
|
| 20 |
+
return output_path
|
| 21 |
+
|
| 22 |
+
from pydub import AudioSegment
|
| 23 |
+
|
| 24 |
+
def mp4_to_wav(mp4_path):
|
| 25 |
+
# Convert MP4 to WAV
|
| 26 |
+
sound = AudioSegment.from_file(mp4_path)
|
| 27 |
+
wav_path = "Audios/pydub_output.wav"
|
| 28 |
+
sound.export(wav_path, format="wav")
|
| 29 |
+
return wav_path
|
| 30 |
+
|
| 31 |
+
import speech_recognition as sr
|
| 32 |
+
|
| 33 |
+
def transcribe_audio(audio_path):
|
| 34 |
+
|
| 35 |
+
# Transcribe audio
|
| 36 |
+
recognizer = sr.Recognizer()
|
| 37 |
+
with sr.AudioFile(audio_path) as source:
|
| 38 |
+
audio = recognizer.record(source)
|
| 39 |
+
text = recognizer.recognize_google(audio)
|
| 40 |
+
|
| 41 |
+
return text
|
| 42 |
+
|
| 43 |
+
# Function to classify the transcription
|
| 44 |
+
def classify_transcription(transcription):
|
| 45 |
+
inputs = tokenizer(transcription, truncation=True, padding=True, max_length=512, return_tensors="pt")
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
outputs = model(**inputs)
|
| 48 |
+
logits = outputs.logits
|
| 49 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 50 |
+
return 'positive' if predicted_class == 1 else 'negative'
|
| 51 |
+
|
| 52 |
+
# Complete pipeline function
|
| 53 |
+
def classify_youtube_video(video_url):
|
| 54 |
+
video_mp4_path = download_youtube_video(video_url)
|
| 55 |
+
audio_wav_path = mp4_to_wav(video_mp4_path)
|
| 56 |
+
transcription = transcribe_audio(audio_wav_path)
|
| 57 |
+
sentiment = classify_transcription(transcription)
|
| 58 |
+
return transcription, sentiment
|
| 59 |
+
|
| 60 |
+
# Create Gradio Interface
|
| 61 |
+
interface = gr.Interface(
|
| 62 |
+
fn=classify_youtube_video,
|
| 63 |
+
inputs=["text"],
|
| 64 |
+
outputs=["text", "text"],
|
| 65 |
+
title="YouTube Video Sentiment Classifier",
|
| 66 |
+
description="Input a YouTube video URL to transcribe the audio and classify the sentiment as positive or negative."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Launch the interface
|
| 70 |
+
interface.launch(inline=False)
|