Spaces:
Running
Running
Merge branch 'main' of https://huggingface.co/spaces/Badro/clip-engine
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import gradio as grfrom pytube import YouTubefrom textblob import TextBlobfrom transformers import pipelineimport loggingimport os# Configure basic logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')# Initialize sentiment analysis pipeline (once, to save resources)# Using a specific model for potentially better results or if default is too large/slow# You might need to adjust the model based on availability and performance on HF Spaces free tierSENTIMENT_MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english"sentiment_analyzer = Nonetry:logging.info(f"Attempting to load sentiment analysis pipeline: {SENTIMENT_MODEL_NAME}...")# Specify a cache directory within the Space's writable area if needed# cache_dir = "/data/.cache/huggingface/transformers" # Example for some HF environments# os.makedirs(cache_dir, exist_ok=True)sentiment_analyzer = pipeline("sentiment-analysis", model=SENTIMENT_MODEL_NAME) #, cache_dir=cache_dir)logging.info(f"Sentiment analysis pipeline '{SENTIMENT_MODEL_NAME}' loaded successfully.")except Exception as e:logging.warning(f"Failed to load {SENTIMENT_MODEL_NAME}: {e}. Falling back to default sentiment model.")try:sentiment_analyzer = pipeline("sentiment-analysis") #, cache_dir=cache_dir)logging.info("Default sentiment analysis pipeline loaded successfully.")except Exception as e_default:logging.error(f"Failed to load default sentiment analysis pipeline: {e_default}")# sentiment_analyzer will remain Nonedef analyze_youtube_content(youtube_url: str = "", transcript_text: str = "") -> dict:"""Analyzes YouTube video content.If a YouTube URL is provided, it attempts to fetch video information (e.g., title, views, length).If transcript text is provided, it performs sentiment analysis on the text usingTextBlob and a Hugging Face transformer model.Args:youtube_url (str, optional): The URL of the YouTube video. Defaults to "".transcript_text (str, optional): The transcript text of the video. Defaults to "".Returns:dict: A dictionary containing analysis results.Includes 'video_info' if URL is processed and 'sentiment_analysis' if transcript is processed."""results = {}logging.info(f"Tool 'analyze_youtube_content' called with URL: '{youtube_url}', Transcript provided: {bool(transcript_text)}")if not youtube_url and not transcript_text:logging.warning("No YouTube URL or transcript text provided.")return {"error": "No YouTube URL or transcript text provided for analysis."}if youtube_url:try:yt = YouTube(youtube_url)results["video_info"] = {"title": yt.title,"views": yt.views,"length_seconds": yt.length,"publish_date": yt.publish_date.strftime('%Y-%m-%d') if yt.publish_date else None,"status": "success"}logging.info(f"Successfully fetched info for video: {yt.title}")except Exception as e:logging.error(f"Error fetching video info from URL '{youtube_url}': {e}")results["video_info"] = {"status": "error","message": f"Could not fetch video info: {str(e)}"}if transcript_text:analysis_data = {}# TextBlob sentimenttry:blob = TextBlob(transcript_text)tb_sentiment = blob.sentimentanalysis_data["textblob"] = {"polarity": round(tb_sentiment.polarity, 3),"subjectivity": round(tb_sentiment.subjectivity, 3),"assessment": "positive" if tb_sentiment.polarity > 0.05 else "negative" if tb_sentiment.polarity < -0.05 else "neutral"}logging.info("TextBlob sentiment analysis complete.")except Exception as e:logging.error(f"Error during TextBlob sentiment analysis: {e}")analysis_data["textblob"] = {"error": str(e)}# Hugging Face sentimentif sentiment_analyzer:try:# Truncate for performance and model limits (default for many models is 512 tokens)max_length_chars = 1000 # Heuristic, actual token limit is what matterstruncated_text = transcript_text[:max_length_chars]hf_result = sentiment_analyzer(truncated_text)[0]analysis_data["huggingface_transformer"] = {"label": hf_result["label"],"score": round(hf_result["score"], 3)}if len(transcript_text) > max_length_chars:analysis_data["huggingface_transformer"]["note"] = f"Analyzed approximately the first {max_length_chars} characters of the transcript."logging.info("Hugging Face transformer sentiment analysis complete.")except Exception as e:logging.error(f"Error during Hugging Face sentiment analysis: {e}")analysis_data["huggingface_transformer"] = {"error": str(e)}else:analysis_data["huggingface_transformer"] = {"error": "Hugging Face sentiment analyzer not loaded."}logging.warning("Hugging Face sentiment analyzer was not available for analysis.")results["sentiment_analysis"] = analysis_dataif not results:return {"status": "No analysis performed, though input was provided. Check logs."}return results# Create Gradio interface for the tool.youtube_tool_interface = gr.Interface(fn=analyze_youtube_content,inputs=[gr.Textbox(label="YouTube Video URL (Optional)", placeholder="Enter YouTube video URL..."),gr.Textbox(label="Video Transcript Text (Optional)", placeholder="Paste video transcript here...", lines=5)],outputs=gr.JSON(label="Analysis Result"),title="YouTube Content Analyzer Tool",description="Provides information and sentiment analysis for a YouTube video URL or its transcript. (For Agent Use via MCP)")# Launch the Gradio app with the MCP server enabled.if __name__ == "__main__":logging.info("Launching Gradio app with MCP server enabled for the YouTube Content Analyzer Tool...")# The `mcp_server=True` flag is crucial for the agent to connect and use the tool.youtube_tool_interface.launch(mcp_server=True)12345# Your detailed YouTube analysis app.py contentimport gradio as grfrom pytube import YouTube# ... more of your code ...youtube_tool_interface.launch(mcp_server=True)
app.py+3-131