import os from flask import Flask, request, jsonify from flask_cors import CORS from transformers import pipeline import torch from PIL import Image import requests from io import BytesIO app = Flask(__name__) CORS(app) # Allow CORS for all routes # Load the Hugging Face model for emotion detection (for image-based) emotion_model = pipeline("image-classification", model="microsoft/VisionTransformer", device=0) # Or any relevant model # Load Mistral AI model for responses (for text-based) mistral_model = pipeline("text-generation", model="facebook/mistral-7b") # Create the uploads folder if it doesn't exist UPLOAD_FOLDER = 'uploads' if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return jsonify({'error': 'No file part'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No selected file'}), 400 # Save file to the server filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) file.save(filepath) # Process the file based on its type (image or video) if file.filename.lower().endswith(('png', 'jpg', 'jpeg')): result = analyze_image(filepath) elif file.filename.lower().endswith(('mp4', 'avi', 'mov')): result = analyze_video(filepath) # This part will be handled later else: return jsonify({'error': 'Unsupported file format'}), 400 return jsonify(result) def analyze_image(image_path): # Open image image = Image.open(image_path) # Use the Hugging Face emotion recognition model results = emotion_model(image) # Process the emotion prediction results emotions = results[0]['label'] # Get a response from Mistral AI based on the detected emotion response = mistral_model(f"How to help someone feeling {emotions}") return { 'emotion': emotions, 'response': response[0]['generated_text'] } def analyze_video(video_path): # Here, you can process the video frame by frame or use a specialized model for video # For simplicity, let's assume we send a message for video return {'message': 'Video processed for emotions (yet to implement frame analysis).'} @app.route('/') def home(): return '''

Upload an Image or Video for Emotion Detection

''' if __name__ == '__main__': app.run(debug=True)