logu29's picture
Create app.py
d64e1bf verified
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()