Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from deepface import DeepFace
|
4 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# VADER for text sentiment
|
8 |
+
analyzer = SentimentIntensityAnalyzer()
|
9 |
+
|
10 |
+
def analyze_text(text):
|
11 |
+
score = analyzer.polarity_scores(text)
|
12 |
+
if score['compound'] >= 0.05:
|
13 |
+
return "Positive π"
|
14 |
+
elif score['compound'] <= -0.05:
|
15 |
+
return "Negative π "
|
16 |
+
else:
|
17 |
+
return "Neutral π"
|
18 |
+
|
19 |
+
def analyze_video(video_file):
|
20 |
+
if video_file is None:
|
21 |
+
return "No video uploaded"
|
22 |
+
|
23 |
+
# Save uploaded file to temp location
|
24 |
+
temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
25 |
+
with open(temp_path, "wb") as f:
|
26 |
+
f.write(video_file.read())
|
27 |
+
|
28 |
+
cap = cv2.VideoCapture(temp_path)
|
29 |
+
success, frame = cap.read()
|
30 |
+
cap.release()
|
31 |
+
|
32 |
+
if not success:
|
33 |
+
return "Couldn't read video"
|
34 |
+
|
35 |
+
try:
|
36 |
+
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
|
37 |
+
return result[0]['dominant_emotion'].capitalize()
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error: {str(e)}"
|
40 |
+
|
41 |
+
def analyze_post(text, video):
|
42 |
+
sentiment = analyze_text(text)
|
43 |
+
emotion = analyze_video(video)
|
44 |
+
return f"Text Sentiment: {sentiment}\nVideo Emotion: {emotion}"
|
45 |
+
|
46 |
+
interface = gr.Interface(
|
47 |
+
fn=analyze_post,
|
48 |
+
inputs=[
|
49 |
+
gr.Textbox(label="Post Text", placeholder="Type your post here..."),
|
50 |
+
gr.File(label="Upload Video (MP4)", file_types=[".mp4"])
|
51 |
+
],
|
52 |
+
outputs="text",
|
53 |
+
title="π± Emotion & Sentiment Analyzer",
|
54 |
+
description="Analyze text sentiment + video facial emotion in one post."
|
55 |
+
)
|
56 |
+
|
57 |
+
interface.launch()
|