Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,155 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
from deepface import DeepFace
|
4 |
-
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
5 |
-
import tempfile
|
6 |
-
|
7 |
-
analyzer = SentimentIntensityAnalyzer()
|
8 |
-
|
9 |
-
def analyze_text(text):
|
10 |
-
score = analyzer.polarity_scores(text)
|
11 |
-
if score['compound'] >= 0.05:
|
12 |
-
return "Positive π"
|
13 |
-
elif score['compound'] <= -0.05:
|
14 |
-
return "Negative π "
|
15 |
-
else:
|
16 |
-
return "Neutral π"
|
17 |
-
|
18 |
-
def process_all(text, video):
|
19 |
-
text_sentiment = analyze_sentiment(text)
|
20 |
-
video_emotion = analyze_video_emotion(video)
|
21 |
-
return f"Text Sentiment: {text_sentiment}\nFacial Emotion: {video_emotion}"
|
22 |
-
|
23 |
-
iface = gr.Interface(
|
24 |
-
fn=process_all,
|
25 |
-
inputs=[gr.Textbox(label="Social Media Post"), gr.Video(label="Upload Video")],
|
26 |
-
outputs="text",
|
27 |
-
title="Emotion & Sentiment Analyzer"
|
28 |
-
)
|
29 |
-
|
30 |
-
iface.launch()
|
31 |
-
|
32 |
-
def analyze_video(video_file):
|
33 |
-
if video_file is None:
|
34 |
-
return "No video uploaded"
|
35 |
-
|
36 |
-
# Save uploaded file temporarily
|
37 |
-
temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
38 |
-
with open(temp_path, "wb") as f:
|
39 |
-
f.write(video_file.read())
|
40 |
-
|
41 |
-
cap = cv2.VideoCapture(temp_path)
|
42 |
-
success, frame = cap.read()
|
43 |
-
cap.release()
|
44 |
-
|
45 |
-
def analyze_video_emotion(video_file):
|
46 |
-
# Save the uploaded video to a temp file
|
47 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
48 |
-
tmp.write(video_file.read())
|
49 |
-
tmp_path = tmp.name
|
50 |
-
|
51 |
-
cap = cv2.VideoCapture(tmp_path)
|
52 |
-
emotions = []
|
53 |
-
frame_count = 0
|
54 |
-
|
55 |
-
import cv2
|
56 |
-
import tempfile
|
57 |
from deepface import DeepFace
|
58 |
-
|
59 |
-
def analyze_video_emotion(video_file):
|
60 |
-
# Save the uploaded video to a temp file
|
61 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
62 |
-
tmp.write(video_file.read())
|
63 |
-
tmp_path = tmp.name
|
64 |
-
|
65 |
-
cap = cv2.VideoCapture(tmp_path)
|
66 |
-
emotions = []
|
67 |
-
frame_count = 0
|
68 |
-
|
69 |
-
while cap.isOpened():
|
70 |
-
ret, frame = cap.read()
|
71 |
-
if not ret or frame_count > 60: # Limit to first 60 frames
|
72 |
-
break
|
73 |
-
try:
|
74 |
-
result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
|
75 |
-
emotions.append(result[0]['dominant_emotion'])
|
76 |
-
except Exception as e:
|
77 |
-
print("Error analyzing frame:", e)
|
78 |
-
frame_count += 1
|
79 |
-
|
80 |
-
cap.release()
|
81 |
-
|
82 |
-
if emotions:
|
83 |
-
# Return most frequent emotion
|
84 |
-
return max(set(emotions), key=emotions.count)
|
85 |
-
else:
|
86 |
-
return "No emotion detected or face not found"
|
87 |
-
|
88 |
-
|
89 |
-
while cap.isOpened():
|
90 |
-
ret, frame = cap.read()
|
91 |
-
if not ret or frame_count > 60: # Limit to 60 frames max
|
92 |
-
break
|
93 |
-
try:
|
94 |
-
result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)
|
95 |
-
emotions.append(result[0]['dominant_emotion'])
|
96 |
-
except:
|
97 |
-
pass
|
98 |
-
frame_count += 1
|
99 |
-
|
100 |
-
cap.release()
|
101 |
-
|
102 |
-
if emotions:
|
103 |
-
# Return most common emotion
|
104 |
-
return max(set(emotions), key=emotions.count)
|
105 |
-
else:
|
106 |
-
return "No face detected"
|
107 |
-
|
108 |
-
|
109 |
-
if not success:
|
110 |
-
return "Could not read video"
|
111 |
-
|
112 |
-
try:
|
113 |
-
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
|
114 |
-
return result[0]['dominant_emotion'].capitalize()
|
115 |
-
except Exception as e:
|
116 |
-
return f"Error: {str(e)}"
|
117 |
-
|
118 |
-
def analyze_post(text, video):
|
119 |
-
sentiment = analyze_text(text)
|
120 |
-
emotion = analyze_video(video)
|
121 |
-
return f"π Sentiment: {sentiment}\nπ₯ Emotion: {emotion}"
|
122 |
-
import gradio as gr
|
123 |
-
|
124 |
-
def analyze_text(text):
|
125 |
-
from transformers import pipeline
|
126 |
-
classifier = pipeline("sentiment-analysis")
|
127 |
-
return classifier(text)[0]['label']
|
128 |
-
|
129 |
-
def process_all(text_input, video_input):
|
130 |
-
text_result = analyze_text(text_input)
|
131 |
-
video_result = analyze_video_emotion(video_input)
|
132 |
-
return f"Text Sentiment: {text_result}\nFacial Emotion: {video_result}"
|
133 |
-
|
134 |
-
gr.Interface(
|
135 |
-
fn=process_all,
|
136 |
-
inputs=[
|
137 |
-
gr.Textbox(label="Enter Social Media Text"),
|
138 |
-
gr.Video(label="Upload a Video Clip")
|
139 |
-
],
|
140 |
-
outputs="text",
|
141 |
-
title="Emotion & Sentiment Decoder",
|
142 |
-
description="Analyzes social media text & facial expressions from video."
|
143 |
-
).launch()
|
144 |
-
|
145 |
-
|
146 |
-
interface = gr.Interface(
|
147 |
-
fn=analyze_post,
|
148 |
-
inputs=[
|
149 |
-
gr.Textbox(label="Post Text", placeholder="Enter your message here"),
|
150 |
-
gr.File(label="Upload video (.mp4)", file_types=[".mp4"])
|
151 |
-
],
|
152 |
from transformers import pipeline
|
|
|
|
|
153 |
import moviepy.editor as mp
|
154 |
|
155 |
def analyze_text(text):
|
@@ -204,9 +57,11 @@ iface = gr.Interface(
|
|
204 |
|
205 |
iface.launch()
|
206 |
|
207 |
-
outputs="text",
|
208 |
-
title="π± Emotion & Sentiment Analyzer",
|
209 |
-
description="Analyze text sentiment and facial emotion from video. No re-running needed. Permanent on Hugging Face."
|
210 |
-
)
|
211 |
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from deepface import DeepFace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from transformers import pipeline
|
4 |
+
import tempfile
|
5 |
+
import cv2
|
6 |
import moviepy.editor as mp
|
7 |
|
8 |
def analyze_text(text):
|
|
|
57 |
|
58 |
iface.launch()
|
59 |
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|