| import gradio as gr |
| import pytube |
| from transformers import pipeline |
| import os |
| from textblob import TextBlob |
|
|
| |
| sentiment_analyzer = pipeline("sentiment-analysis") |
|
|
| def analyze_youtube_content(youtube_url, transcript_text=""): |
| """Main function to analyze YouTube content""" |
| results = {} |
| |
| |
| if youtube_url: |
| try: |
| |
| yt = pytube.YouTube(youtube_url) |
| results["video_info"] = { |
| "title": yt.title, |
| "status": "success" |
| } |
| except Exception as e: |
| results["video_info"] = { |
| "status": "error", |
| "message": str(e) |
| } |
| |
| |
| if transcript_text: |
| |
| blob = TextBlob(transcript_text) |
| textblob_sentiment = blob.sentiment |
| |
| |
| hf_result = sentiment_analyzer(transcript_text[:512])[0] |
| |
| results["sentiment"] = { |
| "textblob": { |
| "polarity": round(textblob_sentiment.polarity, 2), |
| "assessment": "positive" if textblob_sentiment.polarity > 0 else "negative" if textblob_sentiment.polarity < 0 else "neutral" |
| }, |
| "huggingface": { |
| "label": hf_result["label"], |
| "score": round(hf_result["score"], 4) |
| } |
| } |
| |
| |
| sentences = [str(sentence) for sentence in blob.sentences] |
| key_moments = [] |
| |
| for i, sentence in enumerate(sentences): |
| sentiment = TextBlob(sentence).sentiment.polarity |
| if abs(sentiment) > 0.5: |
| key_moments.append({ |
| "text": sentence, |
| "sentiment": sentiment |
| }) |
| |
| results["key_moments"] = key_moments[:5] |
| |
| return results |
|
|
| |
| with gr.Blocks(title="YouTube Viral Moment Analyzer") as demo: |
| gr.Markdown("# YouTube Viral Moment Analyzer") |
| |
| with gr.Row(): |
| youtube_url = gr.Textbox(label="YouTube URL") |
| |
| with gr.Row(): |
| transcript_text = gr.Textbox(label="Transcript Text", lines=10) |
| |
| analyze_button = gr.Button("Analyze Content") |
| output = gr.JSON(label="Analysis Results") |
| |
| analyze_button.click( |
| fn=analyze_youtube_content, |
| inputs=[youtube_url, transcript_text], |
| outputs=output, |
| api_name="analyze_content" |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |