import gradio as gr from textblob import TextBlob from deepface import DeepFace import moviepy.editor as mp import cv2 import tempfile import os # Function to analyze text def analyze_text(text): blob = TextBlob(text) polarity = blob.sentiment.polarity sentiment = "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral" return f"Text Sentiment: {sentiment} (Polarity: {polarity:.2f})" # Function to analyze image (face emotion) def analyze_image(image): try: result = DeepFace.analyze(image, actions=['emotion'], enforce_detection=False) dominant_emotion = result[0]['dominant_emotion'] return f"Detected Emotion: {dominant_emotion}" except Exception as e: return f"Error: {str(e)}" # Function to analyze video (face emotion at center frame) def analyze_video(video_file): try: tmpdir = tempfile.mkdtemp() clip = mp.VideoFileClip(video_file) frame = clip.get_frame(clip.duration / 2) frame_path = os.path.join(tmpdir, "frame.jpg") cv2.imwrite(frame_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) result = DeepFace.analyze(frame_path, actions=['emotion'], enforce_detection=False) dominant_emotion = result[0]['dominant_emotion'] return f"Video Emotion: {dominant_emotion}" except Exception as e: return f"Error: {str(e)}" # Gradio UI with gr.Blocks() as demo: gr.Markdown("# 🧠 Emotion and Sentiment Analyzer") with gr.Tab("Text Analysis"): text_input = gr.Textbox(label="Enter Text") text_output = gr.Textbox(label="Sentiment Result") text_btn = gr.Button("Analyze Text") text_btn.click(analyze_text, inputs=text_input, outputs=text_output) with gr.Tab("Image Analysis"): img_input = gr.Image(type="filepath", label="Upload Face Image") img_output = gr.Textbox(label="Emotion Result") img_btn = gr.Button("Analyze Image") img_btn.click(analyze_image, inputs=img_input, outputs=img_output) with gr.Tab("Video Analysis"): video_input = gr.Video(label="Upload Face Video") video_output = gr.Textbox(label="Emotion Result") video_btn = gr.Button("Analyze Video") video_btn.click(analyze_video, inputs=video_input, outputs=video_output) demo.launch()