Badro commited on
Commit
ed149a7
·
verified ·
1 Parent(s): 22ee089

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pytube
3
+ from transformers import pipeline
4
+ import os
5
+ from textblob import TextBlob
6
+
7
+ # Initialize sentiment analysis pipeline
8
+ sentiment_analyzer = pipeline("sentiment-analysis")
9
+
10
+ def analyze_youtube_content(youtube_url, transcript_text=""):
11
+ """Main function to analyze YouTube content"""
12
+ results = {}
13
+
14
+ # If URL is provided, get video info
15
+ if youtube_url:
16
+ try:
17
+ # Create a YouTube object
18
+ yt = pytube.YouTube(youtube_url)
19
+ results["video_info"] = {
20
+ "title": yt.title,
21
+ "status": "success"
22
+ }
23
+ except Exception as e:
24
+ results["video_info"] = {
25
+ "status": "error",
26
+ "message": str(e)
27
+ }
28
+
29
+ # If transcript is provided, analyze it
30
+ if transcript_text:
31
+ # Analyze sentiment with TextBlob
32
+ blob = TextBlob(transcript_text)
33
+ textblob_sentiment = blob.sentiment
34
+
35
+ # Analyze sentiment with Hugging Face
36
+ hf_result = sentiment_analyzer(transcript_text[:512])[0]
37
+
38
+ results["sentiment"] = {
39
+ "textblob": {
40
+ "polarity": round(textblob_sentiment.polarity, 2),
41
+ "assessment": "positive" if textblob_sentiment.polarity > 0 else "negative" if textblob_sentiment.polarity < 0 else "neutral"
42
+ },
43
+ "huggingface": {
44
+ "label": hf_result["label"],
45
+ "score": round(hf_result["score"], 4)
46
+ }
47
+ }
48
+
49
+ # Identify key moments based on sentiment
50
+ sentences = [str(sentence) for sentence in blob.sentences]
51
+ key_moments = []
52
+
53
+ for i, sentence in enumerate(sentences):
54
+ sentiment = TextBlob(sentence).sentiment.polarity
55
+ if abs(sentiment) > 0.5:
56
+ key_moments.append({
57
+ "text": sentence,
58
+ "sentiment": sentiment
59
+ })
60
+
61
+ results["key_moments"] = key_moments[:5] # Top 5 moments
62
+
63
+ return results
64
+
65
+ # Create Gradio interface
66
+ with gr.Blocks(title="YouTube Viral Moment Analyzer") as demo:
67
+ gr.Markdown("# YouTube Viral Moment Analyzer")
68
+
69
+ with gr.Row():
70
+ youtube_url = gr.Textbox(label="YouTube URL")
71
+
72
+ with gr.Row():
73
+ transcript_text = gr.Textbox(label="Transcript Text", lines=10)
74
+
75
+ analyze_button = gr.Button("Analyze Content")
76
+ output = gr.JSON(label="Analysis Results")
77
+
78
+ analyze_button.click(
79
+ fn=analyze_youtube_content,
80
+ inputs=[youtube_url, transcript_text],
81
+ outputs=output,
82
+ api_name="analyze_content"
83
+ )
84
+
85
+ # Launch the app with server name and port for proper deployment
86
+ if __name__ == "__main__":
87
+ demo.launch(server_name="0.0.0.0", server_port=7860)