logu29's picture
Update app.py
cef2d02 verified
raw
history blame
1.67 kB
import gradio as gr
import cv2
from deepface import DeepFace
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import tempfile
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 temporarily
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 "Could not 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"πŸ“ Sentiment: {sentiment}\nπŸŽ₯ Emotion: {emotion}"
interface = gr.Interface(
fn=analyze_post,
inputs=[
gr.Textbox(label="Post Text", placeholder="Enter your message here"),
gr.File(label="Upload video (.mp4)", file_types=[".mp4"])
],
outputs="text",
title="πŸ“± Emotion & Sentiment Analyzer",
description="Analyze text sentiment and facial emotion from video. No re-running needed. Permanent on Hugging Face."
)
interface.launch()