Spaces:
Build error
Build error
Download app.py from starkbyte45896/Emotion_Detection: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/starkbyte45896/Emotion_Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/starkbyte45896/Emotion_Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/starkbyte45896/Emotion_Detection/resolve/main/app.py
2.78 kB
| 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 | |
| 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).'} | |
| def home(): | |
| return ''' | |
| <html> | |
| <body> | |
| <h2>Upload an Image or Video for Emotion Detection</h2> | |
| <form action="/upload" method="post" enctype="multipart/form-data"> | |
| <input type="file" name="file"> | |
| <input type="submit"> | |
| </form> | |
| </body> | |
| </html> | |
| ''' | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |