Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 analyze_video(video_file):
|
19 |
+
if video_file is None:
|
20 |
+
return "No video uploaded"
|
21 |
+
|
22 |
+
temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
23 |
+
with open(temp_path, "wb") as f:
|
24 |
+
f.write(video_file.read())
|
25 |
+
|
26 |
+
cap = cv2.VideoCapture(temp_path)
|
27 |
+
success, frame = cap.read()
|
28 |
+
cap.release()
|
29 |
+
|
30 |
+
if not success:
|
31 |
+
return "Could not read video"
|
32 |
+
|
33 |
+
try:
|
34 |
+
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
|
35 |
+
return result[0]['dominant_emotion'].capitalize()
|
36 |
+
except Exception as e:
|
37 |
+
return f"Error: {str(e)}"
|
38 |
+
|
39 |
+
def analyze_post(text, video):
|
40 |
+
sentiment = analyze_text(text)
|
41 |
+
emotion = analyze_video(video)
|
42 |
+
return f"π Sentiment: {sentiment}\nπ₯ Emotion: {emotion}"
|
43 |
+
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=analyze_post,
|
46 |
+
inputs=[
|
47 |
+
gr.Textbox(label="Enter post text"),
|
48 |
+
gr.File(label="Upload short video (mp4)", file_types=[".mp4"])
|
49 |
+
],
|
50 |
+
outputs="text",
|
51 |
+
title="π± Emotion & Sentiment Analyzer",
|
52 |
+
description="Analyze sentiment from text and facial emotion from video in one go."
|
53 |
+
)
|
54 |
+
|
55 |
+
interface.launch()
|