RCaz commited on
Commit
eb13b82
·
verified ·
1 Parent(s): 31b6a29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -51
app.py CHANGED
@@ -1,66 +1,34 @@
1
- import os
2
- import tempfile
3
  import gradio as gr
4
- from pytube import YouTube
5
- from moviepy import VideoFileClip
6
- import whisper
7
  from textblob import TextBlob
8
 
9
- # Step 1: Transcribe video
10
- def transcribe_video_from_url(url: str) -> str:
11
- with tempfile.TemporaryDirectory() as tmpdir:
12
- video_path = os.path.join(tmpdir, "video.mp4")
13
- audio_path = os.path.join(tmpdir, "audio.wav")
14
-
15
- # Download the video
16
- yt = YouTube(url)
17
- stream = yt.streams.filter(file_extension='mp4', only_video=False).first()
18
- stream.download(output_path=tmpdir, filename="video.mp4")
19
-
20
- # Extract audio
21
- video_clip = VideoFileClip(video_path)
22
- video_clip.audio.write_audiofile(audio_path, logger=None)
23
- video_clip.close()
24
 
25
- # Transcribe with Whisper
26
- model = whisper.load_model("base")
27
- result = model.transcribe(audio_path)
28
- return result["text"]
29
 
30
- # Step 2: Analyze sentiment
31
- def sentiment_analysis(text: str) -> dict:
 
32
  blob = TextBlob(text)
33
  sentiment = blob.sentiment
 
34
  return {
35
- "polarity": round(sentiment.polarity, 2),
36
- "subjectivity": round(sentiment.subjectivity, 2),
37
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
38
  }
39
 
40
- # Step 3: Main function for Gradio
41
- def analyze_sentiment_from_video(url: str) -> dict:
42
- """
43
- Transcribe audio from a video URL and analyze sentiment.
44
- """
45
- transcription = transcribe_video_from_url(url)
46
- sentiment = sentiment_analysis(transcription)
47
- sentiment["transcription"] = transcription
48
- return sentiment
49
-
50
- # Gradio interface
51
  demo = gr.Interface(
52
- fn=analyze_sentiment_from_video,
53
- inputs=gr.Textbox(label="YouTube Video URL"),
54
- outputs={
55
- "assessment": gr.Textbox(label="Sentiment"),
56
- "polarity": gr.Number(label="Polarity"),
57
- "subjectivity": gr.Number(label="Subjectivity"),
58
- "transcription": gr.Textbox(label="Transcribed Text", lines=10),
59
- },
60
- title="🎥 Sentiment Analysis from YouTube Video",
61
- description="Enter a YouTube video URL. The app will transcribe its audio and analyze the sentiment of the spoken content."
62
  )
63
 
64
- # Launch for Hugging Face Space
65
  if __name__ == "__main__":
66
- demo.launch(mcp_server=True)
 
 
 
1
  import gradio as gr
 
 
 
2
  from textblob import TextBlob
3
 
4
+ def sentiment_analysis(text: str) -> dict:
5
+ """
6
+ Analyze the sentiment of the given text.
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ Args:
9
+ text (str): The text to analyze
 
 
10
 
11
+ Returns:
12
+ dict: A dictionary containing polarity, subjectivity, and assessment
13
+ """
14
  blob = TextBlob(text)
15
  sentiment = blob.sentiment
16
+
17
  return {
18
+ "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
19
+ "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
20
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
21
  }
22
 
23
+ # Create the Gradio interface
 
 
 
 
 
 
 
 
 
 
24
  demo = gr.Interface(
25
+ fn=sentiment_analysis,
26
+ inputs=gr.Textbox(placeholder="Enter text to analyze..."),
27
+ outputs=gr.JSON(),
28
+ title="Text Sentiment Analysis",
29
+ description="Analyze the sentiment of text using TextBlob"
 
 
 
 
 
30
  )
31
 
32
+ # Launch the interface and MCP server
33
  if __name__ == "__main__":
34
+ demo.launch(mcp_server=True)