Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pytube | |
| from transformers import pipeline | |
| from textblob import TextBlob | |
| # Initialize sentiment analysis pipeline | |
| sentiment_analyzer = pipeline("sentiment-analysis") | |
| def analyze_youtube_content(youtube_url, transcript_text=""): | |
| """Analyze YouTube content""" | |
| results = {} | |
| # Get video info | |
| 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) | |
| } | |
| # Analyze transcript | |
| if transcript_text: | |
| # TextBlob sentiment | |
| blob = TextBlob(transcript_text) | |
| sentiment = blob.sentiment | |
| # Hugging Face sentiment | |
| hf_result = sentiment_analyzer(transcript_text[:512])[0] | |
| results["sentiment"] = { | |
| "polarity": round(sentiment.polarity, 2), | |
| "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral", | |
| "huggingface": hf_result["label"] | |
| } | |
| return results | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=analyze_youtube_content, | |
| inputs=[ | |
| gr.Textbox(label="YouTube URL"), | |
| gr.Textbox(label="Transcript Text", lines=10) | |
| ], | |
| outputs=gr.JSON(label="Analysis Results"), | |
| title="YouTube Viral Moment Analyzer", | |
| description="Analyze viral moments from YouTube videos using ML models" | |
| ) | |
| # Launch with MCP server enabled | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) | |