Spaces:
Build error
Build error
File size: 2,776 Bytes
b761fd2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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 '''
<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)
|