logu29 commited on
Commit
e4fd89f
·
verified ·
1 Parent(s): f5bf6e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -27
app.py CHANGED
@@ -1,17 +1,19 @@
1
  import gradio as gr
2
  from textblob import TextBlob
3
  from deepface import DeepFace
4
- import moviepy.editor as mp
5
- import cv2
6
  import tempfile
7
  import os
 
 
8
 
 
9
  def analyze_text(text):
10
  blob = TextBlob(text)
11
  polarity = blob.sentiment.polarity
12
  sentiment = "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral"
13
- return f"Text Sentiment: {sentiment} (Polarity: {polarity:.2f}
14
-
 
15
  def analyze_image(image):
16
  try:
17
  result = DeepFace.analyze(image, actions=['emotion'], enforce_detection=False)
@@ -19,10 +21,12 @@ def analyze_image(image):
19
  return f"Detected Emotion: {dominant_emotion}"
20
  except Exception as e:
21
  return f"Error: {str(e)}"
22
- def analyze_video(video_file):
 
 
23
  try:
24
  tmpdir = tempfile.mkdtemp()
25
- clip = mp.VideoFileClip(video_file)
26
  frame = clip.get_frame(clip.duration / 2)
27
  frame_path = os.path.join(tmpdir, "frame.jpg")
28
  cv2.imwrite(frame_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
@@ -32,24 +36,32 @@ def analyze_video(video_file):
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
35
- with gr.Blocks() as demo:
36
- gr.Markdown("# 🧠 Emotion and Sentiment Analyzer")
37
- with gr.Tab("Text Analysis"):
38
- text_input = gr.Textbox(label="Enter Text")
39
- text_output = gr.Textbox(label="Sentiment Result")
40
- text_btn = gr.Button("Analyze Text")
41
- text_btn.click(analyze_text, inputs=text_input, outputs=text_output)
42
-
43
- with gr.Tab("Image Analysis"):
44
- img_input = gr.Image(type="filepath", label="Upload Face Image")
45
- img_output = gr.Textbox(label="Emotion Result")
46
- img_btn = gr.Button("Analyze Image")
47
- img_btn.click(analyze_image, inputs=img_input, outputs=img_output)
48
-
49
- with gr.Tab("Video Analysis"):
50
- video_input = gr.Video(label="Upload Face Video")
51
- video_output = gr.Textbox(label="Emotion Result")
52
- video_btn = gr.Button("Analyze Video")
53
- video_btn.click(analyze_video, inputs=video_input, outputs=video_output)
54
-
55
- demo.launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from textblob import TextBlob
3
  from deepface import DeepFace
 
 
4
  import tempfile
5
  import os
6
+ import cv2
7
+ import moviepy.editor as mp
8
 
9
+ # Sentiment Analysis for Text
10
  def analyze_text(text):
11
  blob = TextBlob(text)
12
  polarity = blob.sentiment.polarity
13
  sentiment = "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral"
14
+ return f"Sentiment: {sentiment} (Polarity: {polarity:.2f})"
15
+
16
+ # Emotion Analysis for Image (Face Recognition)
17
  def analyze_image(image):
18
  try:
19
  result = DeepFace.analyze(image, actions=['emotion'], enforce_detection=False)
 
21
  return f"Detected Emotion: {dominant_emotion}"
22
  except Exception as e:
23
  return f"Error: {str(e)}"
24
+
25
+ # Emotion Analysis for Video (Face Recognition)
26
+ def analyze_video(video):
27
  try:
28
  tmpdir = tempfile.mkdtemp()
29
+ clip = mp.VideoFileClip(video)
30
  frame = clip.get_frame(clip.duration / 2)
31
  frame_path = os.path.join(tmpdir, "frame.jpg")
32
  cv2.imwrite(frame_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
 
36
  except Exception as e:
37
  return f"Error: {str(e)}"
38
 
39
+ # Gradio Blocks UI
40
+ with gr.Blocks(theme="huggingface") as demo:
41
+ gr.Markdown("# 🎭 Sentiment & Emotion Decoder", elem_id="header")
42
+ gr.Markdown("Upload your text, face image, or video to decode emotions and sentiments!")
43
+
44
+ with gr.Tabs():
45
+ # Text Sentiment Analysis Tab
46
+ with gr.TabItem("📜 Text Sentiment"):
47
+ text_input = gr.Textbox(label="Enter Text Here", placeholder="Type your social media post here...")
48
+ text_button = gr.Button("🔍 Analyze Sentiment")
49
+ text_output = gr.Label(label="Sentiment Result")
50
+ text_button.click(analyze_text, inputs=text_input, outputs=text_output)
51
+
52
+ # Image Emotion Analysis Tab
53
+ with gr.TabItem("📸 Face Emotion Image"):
54
+ img_input = gr.Image(type="filepath", label="Upload Face Image")
55
+ img_output = gr.Label(label="Emotion Result")
56
+ img_button = gr.Button("🔍 Analyze Image")
57
+ img_button.click(analyze_image, inputs=img_input, outputs=img_output)
58
+
59
+ # Video Emotion Analysis Tab
60
+ with gr.TabItem("🎥 Face Emotion Video"):
61
+ video_input = gr.Video(label="Upload Face Video")
62
+ video_output = gr.Label(label="Emotion Result")
63
+ video_button = gr.Button("🔍 Analyze Video")
64
+ video_button.click(analyze_video, inputs=video_input, outputs=video_output)
65
+
66
+ # Launch the Interface
67
+ demo.launch()