starkbyte45896 commited on
Commit
b761fd2
·
verified ·
1 Parent(s): e2adde7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Flask, request, jsonify
3
+ from flask_cors import CORS
4
+ from transformers import pipeline
5
+ import torch
6
+ from PIL import Image
7
+ import requests
8
+ from io import BytesIO
9
+
10
+ app = Flask(__name__)
11
+ CORS(app) # Allow CORS for all routes
12
+
13
+ # Load the Hugging Face model for emotion detection (for image-based)
14
+ emotion_model = pipeline("image-classification", model="microsoft/VisionTransformer", device=0) # Or any relevant model
15
+
16
+ # Load Mistral AI model for responses (for text-based)
17
+ mistral_model = pipeline("text-generation", model="facebook/mistral-7b")
18
+
19
+ # Create the uploads folder if it doesn't exist
20
+ UPLOAD_FOLDER = 'uploads'
21
+ if not os.path.exists(UPLOAD_FOLDER):
22
+ os.makedirs(UPLOAD_FOLDER)
23
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
24
+
25
+
26
+ @app.route('/upload', methods=['POST'])
27
+ def upload_file():
28
+ if 'file' not in request.files:
29
+ return jsonify({'error': 'No file part'}), 400
30
+ file = request.files['file']
31
+ if file.filename == '':
32
+ return jsonify({'error': 'No selected file'}), 400
33
+
34
+ # Save file to the server
35
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
36
+ file.save(filepath)
37
+
38
+ # Process the file based on its type (image or video)
39
+ if file.filename.lower().endswith(('png', 'jpg', 'jpeg')):
40
+ result = analyze_image(filepath)
41
+ elif file.filename.lower().endswith(('mp4', 'avi', 'mov')):
42
+ result = analyze_video(filepath) # This part will be handled later
43
+ else:
44
+ return jsonify({'error': 'Unsupported file format'}), 400
45
+
46
+ return jsonify(result)
47
+
48
+
49
+ def analyze_image(image_path):
50
+ # Open image
51
+ image = Image.open(image_path)
52
+
53
+ # Use the Hugging Face emotion recognition model
54
+ results = emotion_model(image)
55
+
56
+ # Process the emotion prediction results
57
+ emotions = results[0]['label']
58
+
59
+ # Get a response from Mistral AI based on the detected emotion
60
+ response = mistral_model(f"How to help someone feeling {emotions}")
61
+
62
+ return {
63
+ 'emotion': emotions,
64
+ 'response': response[0]['generated_text']
65
+ }
66
+
67
+ def analyze_video(video_path):
68
+ # Here, you can process the video frame by frame or use a specialized model for video
69
+ # For simplicity, let's assume we send a message for video
70
+ return {'message': 'Video processed for emotions (yet to implement frame analysis).'}
71
+
72
+
73
+ @app.route('/')
74
+ def home():
75
+ return '''
76
+ <html>
77
+ <body>
78
+ <h2>Upload an Image or Video for Emotion Detection</h2>
79
+ <form action="/upload" method="post" enctype="multipart/form-data">
80
+ <input type="file" name="file">
81
+ <input type="submit">
82
+ </form>
83
+ </body>
84
+ </html>
85
+ '''
86
+
87
+
88
+ if __name__ == '__main__':
89
+ app.run(debug=True)