Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
@@ -8,119 +8,94 @@ import base64
|
|
8 |
import google.generativeai as genai
|
9 |
|
10 |
app = Flask(__name__)
|
|
|
|
|
|
|
11 |
|
12 |
-
app.config['MYSQL_HOST'] = 'localhost'
|
13 |
-
app.config['MYSQL_USER'] = 'root'
|
14 |
-
app.config['MYSQL_PASSWORD'] = ''
|
15 |
-
app.config['MYSQL_DB'] = 'wanees_app'
|
16 |
|
17 |
-
mysql = MySQL(app)
|
18 |
|
19 |
|
20 |
-
def add_padding(base64_str):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
|
27 |
-
@app.route('/api/chat/gemini', methods=['POST'])
|
28 |
-
def receive_chat():
|
29 |
-
try:
|
30 |
-
data = request.get_json()
|
31 |
-
prompt = data['message']
|
32 |
-
print(data)
|
33 |
-
print('promp is : ',prompt)
|
34 |
-
genai.configure(api_key="AIzaSyB7bKpGzkT_f32d-Xv4tb9zIBtb8vJUCCs")
|
35 |
-
model = genai.GenerativeModel('gemini-pro')
|
36 |
-
response = model.generate_content(prompt)
|
37 |
-
print(response.text)
|
38 |
-
|
39 |
-
generated_text = response.text
|
40 |
-
print('generated_text: ',generated_text)
|
41 |
-
return jsonify({"response": generated_text}), 200
|
42 |
-
except Exception as e:
|
43 |
-
print('the catch is:',str(e))
|
44 |
-
return jsonify({"error": str(e)})
|
45 |
-
|
46 |
-
# openai.api_key = "sk-proj-QtkCbMdxHjgMAyughGjgT3BlbkFJuoyNuhK2zO3uKm1Zc5S6"
|
47 |
-
|
48 |
-
# @backoff.on_exception(backoff.expo, RateLimitError, max_tries=8)
|
49 |
-
# def completions_with_backoff(client, messages):
|
50 |
-
# response = client.chat.completions.create(
|
51 |
-
# model="gpt-3.5-turbo",
|
52 |
-
# messages=messages,
|
53 |
-
# temperature=0,
|
54 |
-
# )
|
55 |
-
# return response
|
56 |
-
|
57 |
-
|
58 |
-
# @app.route('/api/chat/gpt', methods=['POST'])
|
59 |
-
# def receive_message():
|
60 |
# try:
|
61 |
# data = request.get_json()
|
62 |
-
# prompt = data
|
63 |
# print(data)
|
64 |
# print('promp is : ',prompt)
|
65 |
-
#
|
66 |
-
#
|
67 |
-
#
|
68 |
-
#
|
69 |
-
# api_key="sk-proj-EPQes2YgSJEx1e6EwDarT3BlbkFJrEtY36IDssJKi6VkVP7v"
|
70 |
-
# )
|
71 |
-
# #
|
72 |
-
# response = client.chat.completions.create(
|
73 |
-
# model="gpt-3.5-turbo",
|
74 |
-
# messages=messages,
|
75 |
-
# temperature=0,
|
76 |
-
# )
|
77 |
-
|
78 |
-
# # response = openai.Completion.create(
|
79 |
-
# # engine="text-davinci-003",
|
80 |
-
# # prompt=prompt,
|
81 |
-
# # max_tokens=251,
|
82 |
-
# # temperature=0,
|
83 |
-
# # top_p=1
|
84 |
-
# # )
|
85 |
-
# print('response is: ',response.choices[0].message["content"])
|
86 |
|
87 |
-
# generated_text = response.
|
88 |
# print('generated_text: ',generated_text)
|
89 |
# return jsonify({"response": generated_text}), 200
|
90 |
# except Exception as e:
|
91 |
# print('the catch is:',str(e))
|
92 |
# return jsonify({"error": str(e)})
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
@app.route('/api/photo', methods=['POST'])
|
96 |
-
def receive_photo():
|
97 |
-
global photo_path
|
98 |
-
photo_data = request.files['photo']
|
99 |
-
photo_path = os.path.join('.', photo_data.filename)
|
100 |
-
photo_data.save(photo_path)
|
101 |
-
res = ai_model_photo.ai(photo_path)
|
102 |
-
print(photo_path)
|
103 |
-
return jsonify({'message': res}), 200
|
104 |
-
# return Response(status=200, mimetype='application/json')
|
105 |
-
|
106 |
-
|
107 |
-
@app.route('/api/facedetect', methods=['POST'])
|
108 |
-
def AddPhoto():
|
109 |
-
name = request.json['name']
|
110 |
-
email = request.json['email']
|
111 |
-
# mobile = request.json['mobile']
|
112 |
-
img64 = request.json['image']
|
113 |
-
img64_padded = add_padding(img64)
|
114 |
-
imageBinary = base64.b64decode(img64_padded)
|
115 |
-
|
116 |
-
imgdetect = request.json['imagedetect']
|
117 |
-
imgdetect_padded = add_padding(imgdetect)
|
118 |
-
imageBinarydetect = base64.b64decode(imgdetect_padded)
|
119 |
-
photo_path = os.path.join('.', imageBinarydetect.filename)
|
120 |
-
imageBinary.save(photo_path)
|
121 |
-
res = ai_model_photo.ai(photo_path)
|
122 |
-
print(photo_path)
|
123 |
|
|
|
124 |
# def receive_photo():
|
125 |
# global photo_path
|
126 |
# photo_data = request.files['photo']
|
@@ -128,7 +103,35 @@ def AddPhoto():
|
|
128 |
# photo_data.save(photo_path)
|
129 |
# res = ai_model_photo.ai(photo_path)
|
130 |
# print(photo_path)
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
|
134 |
# @app.route('/api/chat/palm', methods=['POST'])
|
@@ -141,135 +144,133 @@ def AddPhoto():
|
|
141 |
# return jsonify({'message': response_palm}), 200
|
142 |
|
143 |
|
144 |
-
@app.route('/add', methods=['POST'])
|
145 |
-
def Add():
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
@app.route('/delete/<string:email>', methods=['DELETE'])
|
193 |
-
def Delete(email):
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
@app.route('/update/<int:id>', methods=['PUT'])
|
209 |
-
def Update(id):
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
}
|
272 |
-
return jsonify(user_data), 200
|
273 |
|
274 |
|
275 |
# if __name__ == '__main__':
|
|
|
8 |
import google.generativeai as genai
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
+
@app.route('/',methods=['GET'])
|
12 |
+
def hello():
|
13 |
+
return {"hello":"The deploy run"}
|
14 |
|
15 |
+
# app.config['MYSQL_HOST'] = 'localhost'
|
16 |
+
# app.config['MYSQL_USER'] = 'root'
|
17 |
+
# app.config['MYSQL_PASSWORD'] = ''
|
18 |
+
# app.config['MYSQL_DB'] = 'wanees_app'
|
19 |
|
20 |
+
# mysql = MySQL(app)
|
21 |
|
22 |
|
23 |
+
# def add_padding(base64_str):
|
24 |
+
# missing_padding = len(base64_str) % 4
|
25 |
+
# if missing_padding != 0:
|
26 |
+
# base64_str += '=' * (4 - missing_padding)
|
27 |
+
# return base64_str
|
28 |
|
29 |
|
30 |
+
# @app.route('/api/chat/gemini', methods=['POST'])
|
31 |
+
# def receive_chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# try:
|
33 |
# data = request.get_json()
|
34 |
+
# prompt = data['message']
|
35 |
# print(data)
|
36 |
# print('promp is : ',prompt)
|
37 |
+
# genai.configure(api_key="AIzaSyB7bKpGzkT_f32d-Xv4tb9zIBtb8vJUCCs")
|
38 |
+
# model = genai.GenerativeModel('gemini-pro')
|
39 |
+
# response = model.generate_content(prompt)
|
40 |
+
# print(response.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# generated_text = response.text
|
43 |
# print('generated_text: ',generated_text)
|
44 |
# return jsonify({"response": generated_text}), 200
|
45 |
# except Exception as e:
|
46 |
# print('the catch is:',str(e))
|
47 |
# return jsonify({"error": str(e)})
|
48 |
|
49 |
+
# # openai.api_key = "sk-proj-QtkCbMdxHjgMAyughGjgT3BlbkFJuoyNuhK2zO3uKm1Zc5S6"
|
50 |
+
|
51 |
+
# # @backoff.on_exception(backoff.expo, RateLimitError, max_tries=8)
|
52 |
+
# # def completions_with_backoff(client, messages):
|
53 |
+
# # response = client.chat.completions.create(
|
54 |
+
# # model="gpt-3.5-turbo",
|
55 |
+
# # messages=messages,
|
56 |
+
# # temperature=0,
|
57 |
+
# # )
|
58 |
+
# # return response
|
59 |
+
|
60 |
+
|
61 |
+
# # @app.route('/api/chat/gpt', methods=['POST'])
|
62 |
+
# # def receive_message():
|
63 |
+
# # try:
|
64 |
+
# # data = request.get_json()
|
65 |
+
# # prompt = data.get('prompt', 'Hello')
|
66 |
+
# # print(data)
|
67 |
+
# # print('promp is : ',prompt)
|
68 |
+
# # # client = OpenAI()
|
69 |
+
# # messages = [{"role": "user", "content": prompt}]
|
70 |
+
# # client = OpenAI(
|
71 |
+
# # # This is the default and can be omitted
|
72 |
+
# # api_key="sk-proj-EPQes2YgSJEx1e6EwDarT3BlbkFJrEtY36IDssJKi6VkVP7v"
|
73 |
+
# # )
|
74 |
+
# # #
|
75 |
+
# # response = client.chat.completions.create(
|
76 |
+
# # model="gpt-3.5-turbo",
|
77 |
+
# # messages=messages,
|
78 |
+
# # temperature=0,
|
79 |
+
# # )
|
80 |
+
|
81 |
+
# # # response = openai.Completion.create(
|
82 |
+
# # # engine="text-davinci-003",
|
83 |
+
# # # prompt=prompt,
|
84 |
+
# # # max_tokens=251,
|
85 |
+
# # # temperature=0,
|
86 |
+
# # # top_p=1
|
87 |
+
# # # )
|
88 |
+
# # print('response is: ',response.choices[0].message["content"])
|
89 |
+
|
90 |
+
# # generated_text = response.choices[0].message["content"]
|
91 |
+
# # print('generated_text: ',generated_text)
|
92 |
+
# # return jsonify({"response": generated_text}), 200
|
93 |
+
# # except Exception as e:
|
94 |
+
# # print('the catch is:',str(e))
|
95 |
+
# # return jsonify({"error": str(e)})
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
# @app.route('/api/photo', methods=['POST'])
|
99 |
# def receive_photo():
|
100 |
# global photo_path
|
101 |
# photo_data = request.files['photo']
|
|
|
103 |
# photo_data.save(photo_path)
|
104 |
# res = ai_model_photo.ai(photo_path)
|
105 |
# print(photo_path)
|
106 |
+
# return jsonify({'message': res}), 200
|
107 |
+
# # return Response(status=200, mimetype='application/json')
|
108 |
+
|
109 |
+
|
110 |
+
# @app.route('/api/facedetect', methods=['POST'])
|
111 |
+
# def AddPhoto():
|
112 |
+
# name = request.json['name']
|
113 |
+
# email = request.json['email']
|
114 |
+
# # mobile = request.json['mobile']
|
115 |
+
# img64 = request.json['image']
|
116 |
+
# img64_padded = add_padding(img64)
|
117 |
+
# imageBinary = base64.b64decode(img64_padded)
|
118 |
+
|
119 |
+
# imgdetect = request.json['imagedetect']
|
120 |
+
# imgdetect_padded = add_padding(imgdetect)
|
121 |
+
# imageBinarydetect = base64.b64decode(imgdetect_padded)
|
122 |
+
# photo_path = os.path.join('.', imageBinarydetect.filename)
|
123 |
+
# imageBinary.save(photo_path)
|
124 |
+
# res = ai_model_photo.ai(photo_path)
|
125 |
+
# print(photo_path)
|
126 |
+
|
127 |
+
# def receive_photo():
|
128 |
+
# global photo_path
|
129 |
+
# photo_data = request.files['photo']
|
130 |
+
# photo_path = os.path.join('.', photo_data.filename)
|
131 |
+
# photo_data.save(photo_path)
|
132 |
+
# res = ai_model_photo.ai(photo_path)
|
133 |
+
# print(photo_path)
|
134 |
+
# return jsonify({'message': res}), 200
|
135 |
|
136 |
|
137 |
# @app.route('/api/chat/palm', methods=['POST'])
|
|
|
144 |
# return jsonify({'message': response_palm}), 200
|
145 |
|
146 |
|
147 |
+
# @app.route('/add', methods=['POST'])
|
148 |
+
# def Add():
|
149 |
+
# name = request.json['name']
|
150 |
+
# email = request.json['email']
|
151 |
+
# mobile = request.json['mobile']
|
152 |
+
# mobile_emergency = request.json['mobile_emergency']
|
153 |
+
# age = request.json['age']
|
154 |
+
# gender = request.json['gender']
|
155 |
+
# location = request.json['location']
|
156 |
+
# img64 = request.json['image']
|
157 |
+
# user_disease = request.json['user_disease']
|
158 |
+
# user_medicine = request.json['user_medicine']
|
159 |
+
|
160 |
+
# img64_padded = add_padding(img64)
|
161 |
+
# imageBinary = base64.b64decode(img64_padded)
|
162 |
+
|
163 |
+
# cur = mysql.connection.cursor()
|
164 |
+
# cur.execute("""
|
165 |
+
# INSERT INTO users
|
166 |
+
# (name, email ,mobile, mobile_emergency, age, gender, location, image)
|
167 |
+
# VALUES (%s,%s,%s,%s,%s,%s,%s,%s)
|
168 |
+
# """, (name, email, mobile, mobile_emergency, age, gender, location, imageBinary))
|
169 |
+
# mysql.connection.commit()
|
170 |
+
# cur.execute("""
|
171 |
+
# SELECT * FROM users
|
172 |
+
# WHERE name=%s AND email=%s
|
173 |
+
# """, (name, email))
|
174 |
+
# data = cur.fetchall()
|
175 |
+
# id = data[0][0]
|
176 |
+
# for disease in user_disease:
|
177 |
+
# print(disease)
|
178 |
+
# cur.execute("""INSERT INTO user_disease
|
179 |
+
# (user_id, disease_name)
|
180 |
+
# VALUES (%s, %s)
|
181 |
+
# """, (id, disease))
|
182 |
+
# for medicine in user_medicine:
|
183 |
+
# img64 = medicine['medicine_image']
|
184 |
+
# img64_padded = add_padding(img64)
|
185 |
+
# imageBinary = base64.b64decode(img64_padded)
|
186 |
+
# cur.execute("""INSERT INTO user_medicine
|
187 |
+
# (user_id, medicine_name,medicine_dose ,medicine_image)
|
188 |
+
# VALUES (%s, %s, %s, %s)
|
189 |
+
# """, (id, medicine['medicine_name'], medicine['medicine_dose'], medicine['medicine_image']))
|
190 |
+
# mysql.connection.commit()
|
191 |
+
# cur.close()
|
192 |
+
# return jsonify({"message": "done"}), 200
|
193 |
+
|
194 |
+
|
195 |
+
# @app.route('/delete/<string:email>', methods=['DELETE'])
|
196 |
+
# def Delete(email):
|
197 |
+
# cur = mysql.connection.cursor()
|
198 |
+
# cur.execute("""
|
199 |
+
# SELECT id FROM users
|
200 |
+
# WHERE email=%s
|
201 |
+
# """, (email, ))
|
202 |
+
# data = cur.fetchall()
|
203 |
+
# id = data[0][0]
|
204 |
+
# cur.execute("DELETE FROM user_disease WHERE user_id=%s", (id,))
|
205 |
+
# cur.execute("DELETE FROM user_medicine WHERE user_id=%s", (id,))
|
206 |
+
# cur.execute("DELETE FROM users WHERE id=%s", (id,))
|
207 |
+
# mysql.connection.commit()
|
208 |
+
# return jsonify({"message": "done"}), 200
|
209 |
+
|
210 |
+
|
211 |
+
# @app.route('/update/<int:id>', methods=['PUT'])
|
212 |
+
# def Update(id):
|
213 |
+
# name = request.json['name']
|
214 |
+
# email = request.json['email']
|
215 |
+
# mobile = request.json['mobile']
|
216 |
+
# mobile_emergency = request.json['mobile_emergency']
|
217 |
+
# age = request.json['age']
|
218 |
+
# gender = request.json['gender']
|
219 |
+
# location = request.json['location']
|
220 |
+
# img64 = request.json['image']
|
221 |
+
# user_disease = request.json['user_disease']
|
222 |
+
# user_medicine = request.json['user_medicine']
|
223 |
+
|
224 |
+
# img64_padded = add_padding(img64)
|
225 |
+
# imageBinary = base64.b64decode(img64_padded)
|
226 |
+
|
227 |
+
# cur = mysql.connection.cursor()
|
228 |
+
# cur.execute("""
|
229 |
+
# UPDATE users
|
230 |
+
# SET name=%s,email=%s, mobile=%s ,mobile_emergency=%s ,age=%s ,gender=%s ,location=%s ,image=%s
|
231 |
+
# WHERE id=%s
|
232 |
+
# """, (name, email, mobile, mobile_emergency, age, gender, location, imageBinary, id))
|
233 |
+
# cur.execute("DELETE FROM user_disease WHERE user_id=%s", (id,))
|
234 |
+
# cur.execute("DELETE FROM user_medicine WHERE user_id=%s", (id,))
|
235 |
+
|
236 |
+
# for disease in user_disease:
|
237 |
+
# cur.execute("""INSERT INTO user_disease
|
238 |
+
# (user_id, disease_name)
|
239 |
+
# VALUES (%s, %s)
|
240 |
+
# """, (id, disease['disease_name']))
|
241 |
+
# for medicine in user_medicine:
|
242 |
+
# cur.execute("""INSERT INTO user_medicine
|
243 |
+
# (user_id, medicine_name, medicine_image)
|
244 |
+
# VALUES (%s, %s, %s)
|
245 |
+
# """, (id, medicine['medicine_name'], medicine['medicine_image']))
|
246 |
+
# mysql.connection.commit()
|
247 |
+
# return jsonify({"message": "done"}), 200
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
# @app.route('/select/<int:id>', methods=['GET'])
|
252 |
+
# def Select(id):
|
253 |
+
# cur = mysql.connection.cursor()
|
254 |
+
# cur.execute("SELECT * FROM users WHERE id=%s", (id,))
|
255 |
+
# table1 = cur.fetchall()
|
256 |
+
# cur.execute("SELECT * FROM user_disease WHERE user_id=%s", (id,))
|
257 |
+
# table2 = cur.fetchall()
|
258 |
+
# cur.execute("SELECT * FROM user_medicine WHERE user_id=%s", (id,))
|
259 |
+
# table3 = cur.fetchall()
|
260 |
+
# cur.close()
|
261 |
+
# user_data = {
|
262 |
+
# "name": table1[0][1],
|
263 |
+
# "email": table1[0][2],
|
264 |
+
# "mobile": table1[0][3],
|
265 |
+
# "mobile_emergency": table1[0][4],
|
266 |
+
# "age": table1[0][5],
|
267 |
+
# "gender": table1[0][6],
|
268 |
+
# "location": table1[0][7],
|
269 |
+
# "image": base64.b64encode(table1[0][8]).decode('utf-8'),
|
270 |
+
# "user_disease": [{"disease_name": disease[1]} for disease in table2],
|
271 |
+
# "user_medicine": [{"medicine_name": medicine[1], "medicine_image": base64.b64encode(medicine[3]).decode('utf-8')} for medicine in table3]
|
272 |
+
# }
|
273 |
+
# return jsonify(user_data), 200
|
|
|
|
|
274 |
|
275 |
|
276 |
# if __name__ == '__main__':
|