Pingul commited on
Commit
5178b73
·
verified ·
1 Parent(s): ddacb0d

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +98 -100
api.py CHANGED
@@ -1,100 +1,98 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
3
- from utils.chatgpt_config import send_ChatGPT
4
- from utils.modelFace import predict_face
5
- from utils.modelText import predict_text
6
- import os
7
-
8
- app = Flask(__name__)
9
- CORS(app)
10
-
11
- # @app.route('/upload_image', methods=['POST'])
12
- # def upload_image():
13
- # try:
14
- # data = request.get_json()
15
- # base64_image = data.get('base64_image')
16
-
17
- # if not base64_image:
18
- # return jsonify({"error": "Missing 'base64_image' in the request"}), 400
19
-
20
- # emotion_face=predict_face(base64_image)
21
-
22
- # return jsonify({"message": "Image received and saved successfully!",
23
- # "chat":send_ChatGPT(emotion_face=emotion_face)}), 200
24
-
25
- # except Exception as e:
26
- # return jsonify({"error": str(e)}), 400
27
-
28
- # @app.route('/upload_text', methods=['POST'])
29
- # def upload_text():
30
- # try:
31
- # # Obtener el cuerpo de la solicitud como JSON
32
- # data = request.get_json()
33
-
34
- # # Verificar que el campo 'text' esté presente y sea una cadena
35
- # text = data.get('text')
36
-
37
- # if text is None:
38
- # return jsonify({"error": "Falta el campo 'text' en la solicitud."}), 400
39
-
40
- # # Verificar que 'text' sea una cadena de texto
41
- # if not isinstance(text, str):
42
- # return jsonify({"error": "El campo 'text' debe ser una cadena de texto."}), 400
43
-
44
- # # Verificar que 'text' no sea vacío o solo espacios
45
- # if not text.strip():
46
- # return jsonify({"error": "El campo 'text' no debe estar vacío."}), 400
47
-
48
- # # Procesar el texto (reemplaza con tu lógica)
49
- # return jsonify({
50
- # "message": "¡Texto recibido y guardado exitosamente!",
51
- # "chat": send_ChatGPT(emotion_text="sad") # Reemplaza con tu función real
52
- # }), 200
53
-
54
- # except Exception as e:
55
- # return jsonify({"error": str(e)}), 400
56
- @app.route('/')
57
- def home():
58
- return "¡Hola desde Flask!"
59
-
60
-
61
- @app.route('/upload_info', methods=['POST'])
62
- def upload_info():
63
- try:
64
- data = request.get_json()
65
- if not data:
66
- return jsonify({"error": "Request must be in JSON format."}), 400
67
-
68
- base64_image = data.get('img')
69
- text = data.get('text')
70
-
71
- # Validar presencia de los campos requeridos
72
- if not base64_image:
73
- return jsonify({"error": "Missing 'img' in the request."}), 400
74
-
75
- if text is None:
76
- return jsonify({"error": "Missing 'text' in the request."}), 400
77
-
78
- # Validar que 'text' sea una cadena no vacía
79
- if not isinstance(text, str) or not text.strip():
80
- return jsonify({"error": "'text' must be a non-empty string."}), 400
81
-
82
- emotion_face=predict_face(base64_image)
83
- emotion_text=predict_text(text)
84
-
85
-
86
- # Simular la función send_ChatGPT
87
- chat_response = send_ChatGPT(emotion_face=emotion_face,emotion_text=emotion_text) # Sustituir por la implementación real
88
-
89
- return jsonify({
90
- "message": "Image received and saved successfully!",
91
- "chat": chat_response
92
- }), 200
93
-
94
- except Exception as e:
95
- return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
96
-
97
- if __name__ == '__main__':
98
- port = int(os.environ.get("PORT", 4000))
99
- # app.run(debug=True)
100
- app.run(host="0.0.0.0", port=port)
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ from utils.chatgpt_config import send_ChatGPT
4
+ from utils.modelFace import predict_face
5
+ from utils.modelText import predict_text
6
+ import os
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ # @app.route('/upload_image', methods=['POST'])
12
+ # def upload_image():
13
+ # try:
14
+ # data = request.get_json()
15
+ # base64_image = data.get('base64_image')
16
+
17
+ # if not base64_image:
18
+ # return jsonify({"error": "Missing 'base64_image' in the request"}), 400
19
+
20
+ # emotion_face=predict_face(base64_image)
21
+
22
+ # return jsonify({"message": "Image received and saved successfully!",
23
+ # "chat":send_ChatGPT(emotion_face=emotion_face)}), 200
24
+
25
+ # except Exception as e:
26
+ # return jsonify({"error": str(e)}), 400
27
+
28
+ # @app.route('/upload_text', methods=['POST'])
29
+ # def upload_text():
30
+ # try:
31
+ # # Obtener el cuerpo de la solicitud como JSON
32
+ # data = request.get_json()
33
+
34
+ # # Verificar que el campo 'text' esté presente y sea una cadena
35
+ # text = data.get('text')
36
+
37
+ # if text is None:
38
+ # return jsonify({"error": "Falta el campo 'text' en la solicitud."}), 400
39
+
40
+ # # Verificar que 'text' sea una cadena de texto
41
+ # if not isinstance(text, str):
42
+ # return jsonify({"error": "El campo 'text' debe ser una cadena de texto."}), 400
43
+
44
+ # # Verificar que 'text' no sea vacío o solo espacios
45
+ # if not text.strip():
46
+ # return jsonify({"error": "El campo 'text' no debe estar vacío."}), 400
47
+
48
+ # # Procesar el texto (reemplaza con tu lógica)
49
+ # return jsonify({
50
+ # "message": "¡Texto recibido y guardado exitosamente!",
51
+ # "chat": send_ChatGPT(emotion_text="sad") # Reemplaza con tu función real
52
+ # }), 200
53
+
54
+ # except Exception as e:
55
+ # return jsonify({"error": str(e)}), 400
56
+ @app.route('/')
57
+ def home():
58
+ return "¡Hola desde Flask!"
59
+
60
+
61
+ @app.route('/upload_info', methods=['POST'])
62
+ def upload_info():
63
+ try:
64
+ data = request.get_json()
65
+ if not data:
66
+ return jsonify({"error": "Request must be in JSON format."}), 400
67
+
68
+ base64_image = data.get('img')
69
+ text = data.get('text')
70
+
71
+ # Validar presencia de los campos requeridos
72
+ if not base64_image:
73
+ return jsonify({"error": "Missing 'img' in the request."}), 400
74
+
75
+ if text is None:
76
+ return jsonify({"error": "Missing 'text' in the request."}), 400
77
+
78
+ # Validar que 'text' sea una cadena no vacía
79
+ if not isinstance(text, str) or not text.strip():
80
+ return jsonify({"error": "'text' must be a non-empty string."}), 400
81
+
82
+ emotion_face=predict_face(base64_image)
83
+ emotion_text=predict_text(text)
84
+
85
+
86
+ # Simular la función send_ChatGPT
87
+ chat_response = send_ChatGPT(emotion_face=emotion_face,emotion_text=emotion_text) # Sustituir por la implementación real
88
+
89
+ return jsonify({
90
+ "message": "Image received and saved successfully!",
91
+ "chat": chat_response
92
+ }), 200
93
+
94
+ except Exception as e:
95
+ return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
96
+
97
+ if __name__ == '__main__':
98
+ app.run(debug=True)