File size: 3,342 Bytes
5178b73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
from flask import Flask, request, jsonify
from flask_cors import CORS
from utils.chatgpt_config import send_ChatGPT
from utils.modelFace import predict_face
from utils.modelText import predict_text
import os

app = Flask(__name__)
CORS(app)

# @app.route('/upload_image', methods=['POST'])
# def upload_image():
#     try:
#         data = request.get_json()
#         base64_image = data.get('base64_image')

#         if not base64_image:
#             return jsonify({"error": "Missing 'base64_image' in the request"}), 400

#         emotion_face=predict_face(base64_image)

#         return jsonify({"message": "Image received and saved successfully!",
#                         "chat":send_ChatGPT(emotion_face=emotion_face)}), 200

#     except Exception as e:
#         return jsonify({"error": str(e)}), 400
    
# @app.route('/upload_text', methods=['POST'])
# def upload_text():
#     try:
#         # Obtener el cuerpo de la solicitud como JSON
#         data = request.get_json()
        
#         # Verificar que el campo 'text' esté presente y sea una cadena
#         text = data.get('text')
        
#         if text is None:
#             return jsonify({"error": "Falta el campo 'text' en la solicitud."}), 400
        
#         # Verificar que 'text' sea una cadena de texto
#         if not isinstance(text, str):
#             return jsonify({"error": "El campo 'text' debe ser una cadena de texto."}), 400
        
#         # Verificar que 'text' no sea vacío o solo espacios
#         if not text.strip():
#             return jsonify({"error": "El campo 'text' no debe estar vacío."}), 400

#         # Procesar el texto (reemplaza con tu lógica)
#         return jsonify({
#             "message": "¡Texto recibido y guardado exitosamente!",
#             "chat": send_ChatGPT(emotion_text="sad")  # Reemplaza con tu función real
#         }), 200

#     except Exception as e:
#         return jsonify({"error": str(e)}), 400
@app.route('/')
def home():
    return "¡Hola desde Flask!"


@app.route('/upload_info', methods=['POST'])
def upload_info():
    try:
        data = request.get_json()
        if not data:
            return jsonify({"error": "Request must be in JSON format."}), 400

        base64_image = data.get('img')
        text = data.get('text')

        # Validar presencia de los campos requeridos
        if not base64_image:
            return jsonify({"error": "Missing 'img' in the request."}), 400
        
        if text is None:
            return jsonify({"error": "Missing 'text' in the request."}), 400
        
        # Validar que 'text' sea una cadena no vacía
        if not isinstance(text, str) or not text.strip():
            return jsonify({"error": "'text' must be a non-empty string."}), 400

        emotion_face=predict_face(base64_image)
        emotion_text=predict_text(text)


        # Simular la función send_ChatGPT
        chat_response = send_ChatGPT(emotion_face=emotion_face,emotion_text=emotion_text) # Sustituir por la implementación real

        return jsonify({
            "message": "Image received and saved successfully!",
            "chat": chat_response
        }), 200

    except Exception as e:
        return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500

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