File size: 1,655 Bytes
d64e1bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import cv2
from deepface import DeepFace
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import tempfile

# VADER for text sentiment
analyzer = SentimentIntensityAnalyzer()

def analyze_text(text):
    score = analyzer.polarity_scores(text)
    if score['compound'] >= 0.05:
        return "Positive 😊"
    elif score['compound'] <= -0.05:
        return "Negative 😠"
    else:
        return "Neutral 😐"

def analyze_video(video_file):
    if video_file is None:
        return "No video uploaded"
    
    # Save uploaded file to temp location
    temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
    with open(temp_path, "wb") as f:
        f.write(video_file.read())

    cap = cv2.VideoCapture(temp_path)
    success, frame = cap.read()
    cap.release()

    if not success:
        return "Couldn't read video"

    try:
        result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
        return result[0]['dominant_emotion'].capitalize()
    except Exception as e:
        return f"Error: {str(e)}"

def analyze_post(text, video):
    sentiment = analyze_text(text)
    emotion = analyze_video(video)
    return f"Text Sentiment: {sentiment}\nVideo Emotion: {emotion}"

interface = gr.Interface(
    fn=analyze_post,
    inputs=[
        gr.Textbox(label="Post Text", placeholder="Type your post here..."),
        gr.File(label="Upload Video (MP4)", file_types=[".mp4"])
    ],
    outputs="text",
    title="πŸ“± Emotion & Sentiment Analyzer",
    description="Analyze text sentiment + video facial emotion in one post."
)

interface.launch()