from flask import Flask, jsonify, request import os # from "./emotion_recognition/ai_model_photo" import ai # from emotion_recognition import ai_model_photo # from palm_response import getResponse, init_connect from flask_mysqldb import MySQL import base64 import google.generativeai as genai app = Flask(__name__) @app.route('/',methods=['GET']) def hello(): return {"hello":"The deploy run"} # app.config['MYSQL_HOST'] = 'localhost' # app.config['MYSQL_USER'] = 'root' # app.config['MYSQL_PASSWORD'] = '' # app.config['MYSQL_DB'] = 'wanees_app' # mysql = MySQL(app) # def add_padding(base64_str): # missing_padding = len(base64_str) % 4 # if missing_padding != 0: # base64_str += '=' * (4 - missing_padding) # return base64_str # @app.route('/api/chat/gemini', methods=['POST']) # def receive_chat(): # try: # data = request.get_json() # prompt = data['message'] # print(data) # print('promp is : ',prompt) # genai.configure(api_key="AIzaSyB7bKpGzkT_f32d-Xv4tb9zIBtb8vJUCCs") # model = genai.GenerativeModel('gemini-pro') # response = model.generate_content(prompt) # print(response.text) # generated_text = response.text # print('generated_text: ',generated_text) # return jsonify({"response": generated_text}), 200 # except Exception as e: # print('the catch is:',str(e)) # return jsonify({"error": str(e)}) # # openai.api_key = "sk-proj-QtkCbMdxHjgMAyughGjgT3BlbkFJuoyNuhK2zO3uKm1Zc5S6" # # @backoff.on_exception(backoff.expo, RateLimitError, max_tries=8) # # def completions_with_backoff(client, messages): # # response = client.chat.completions.create( # # model="gpt-3.5-turbo", # # messages=messages, # # temperature=0, # # ) # # return response # # @app.route('/api/chat/gpt', methods=['POST']) # # def receive_message(): # # try: # # data = request.get_json() # # prompt = data.get('prompt', 'Hello') # # print(data) # # print('promp is : ',prompt) # # # client = OpenAI() # # messages = [{"role": "user", "content": prompt}] # # client = OpenAI( # # # This is the default and can be omitted # # api_key="sk-proj-EPQes2YgSJEx1e6EwDarT3BlbkFJrEtY36IDssJKi6VkVP7v" # # ) # # # # # response = client.chat.completions.create( # # model="gpt-3.5-turbo", # # messages=messages, # # temperature=0, # # ) # # # response = openai.Completion.create( # # # engine="text-davinci-003", # # # prompt=prompt, # # # max_tokens=251, # # # temperature=0, # # # top_p=1 # # # ) # # print('response is: ',response.choices[0].message["content"]) # # generated_text = response.choices[0].message["content"] # # print('generated_text: ',generated_text) # # return jsonify({"response": generated_text}), 200 # # except Exception as e: # # print('the catch is:',str(e)) # # return jsonify({"error": str(e)}) # @app.route('/api/photo', methods=['POST']) # def receive_photo(): # global photo_path # photo_data = request.files['photo'] # photo_path = os.path.join('.', photo_data.filename) # photo_data.save(photo_path) # res = ai_model_photo.ai(photo_path) # print(photo_path) # return jsonify({'message': res}), 200 # # return Response(status=200, mimetype='application/json') # @app.route('/api/facedetect', methods=['POST']) # def AddPhoto(): # name = request.json['name'] # email = request.json['email'] # # mobile = request.json['mobile'] # img64 = request.json['image'] # img64_padded = add_padding(img64) # imageBinary = base64.b64decode(img64_padded) # imgdetect = request.json['imagedetect'] # imgdetect_padded = add_padding(imgdetect) # imageBinarydetect = base64.b64decode(imgdetect_padded) # photo_path = os.path.join('.', imageBinarydetect.filename) # imageBinary.save(photo_path) # res = ai_model_photo.ai(photo_path) # print(photo_path) # def receive_photo(): # global photo_path # photo_data = request.files['photo'] # photo_path = os.path.join('.', photo_data.filename) # photo_data.save(photo_path) # res = ai_model_photo.ai(photo_path) # print(photo_path) # return jsonify({'message': res}), 200 # @app.route('/api/chat/palm', methods=['POST']) # def receive_message(): # message ={ # "message" : request.json['message'] # } # response_palm = getResponse(message["message"]) # print("res palm =====", response_palm) # return jsonify({'message': response_palm}), 200 # @app.route('/add', methods=['POST']) # def Add(): # name = request.json['name'] # email = request.json['email'] # mobile = request.json['mobile'] # mobile_emergency = request.json['mobile_emergency'] # age = request.json['age'] # gender = request.json['gender'] # location = request.json['location'] # img64 = request.json['image'] # user_disease = request.json['user_disease'] # user_medicine = request.json['user_medicine'] # img64_padded = add_padding(img64) # imageBinary = base64.b64decode(img64_padded) # cur = mysql.connection.cursor() # cur.execute(""" # INSERT INTO users # (name, email ,mobile, mobile_emergency, age, gender, location, image) # VALUES (%s,%s,%s,%s,%s,%s,%s,%s) # """, (name, email, mobile, mobile_emergency, age, gender, location, imageBinary)) # mysql.connection.commit() # cur.execute(""" # SELECT * FROM users # WHERE name=%s AND email=%s # """, (name, email)) # data = cur.fetchall() # id = data[0][0] # for disease in user_disease: # print(disease) # cur.execute("""INSERT INTO user_disease # (user_id, disease_name) # VALUES (%s, %s) # """, (id, disease)) # for medicine in user_medicine: # img64 = medicine['medicine_image'] # img64_padded = add_padding(img64) # imageBinary = base64.b64decode(img64_padded) # cur.execute("""INSERT INTO user_medicine # (user_id, medicine_name,medicine_dose ,medicine_image) # VALUES (%s, %s, %s, %s) # """, (id, medicine['medicine_name'], medicine['medicine_dose'], medicine['medicine_image'])) # mysql.connection.commit() # cur.close() # return jsonify({"message": "done"}), 200 # @app.route('/delete/', methods=['DELETE']) # def Delete(email): # cur = mysql.connection.cursor() # cur.execute(""" # SELECT id FROM users # WHERE email=%s # """, (email, )) # data = cur.fetchall() # id = data[0][0] # cur.execute("DELETE FROM user_disease WHERE user_id=%s", (id,)) # cur.execute("DELETE FROM user_medicine WHERE user_id=%s", (id,)) # cur.execute("DELETE FROM users WHERE id=%s", (id,)) # mysql.connection.commit() # return jsonify({"message": "done"}), 200 # @app.route('/update/', methods=['PUT']) # def Update(id): # name = request.json['name'] # email = request.json['email'] # mobile = request.json['mobile'] # mobile_emergency = request.json['mobile_emergency'] # age = request.json['age'] # gender = request.json['gender'] # location = request.json['location'] # img64 = request.json['image'] # user_disease = request.json['user_disease'] # user_medicine = request.json['user_medicine'] # img64_padded = add_padding(img64) # imageBinary = base64.b64decode(img64_padded) # cur = mysql.connection.cursor() # cur.execute(""" # UPDATE users # SET name=%s,email=%s, mobile=%s ,mobile_emergency=%s ,age=%s ,gender=%s ,location=%s ,image=%s # WHERE id=%s # """, (name, email, mobile, mobile_emergency, age, gender, location, imageBinary, id)) # cur.execute("DELETE FROM user_disease WHERE user_id=%s", (id,)) # cur.execute("DELETE FROM user_medicine WHERE user_id=%s", (id,)) # for disease in user_disease: # cur.execute("""INSERT INTO user_disease # (user_id, disease_name) # VALUES (%s, %s) # """, (id, disease['disease_name'])) # for medicine in user_medicine: # cur.execute("""INSERT INTO user_medicine # (user_id, medicine_name, medicine_image) # VALUES (%s, %s, %s) # """, (id, medicine['medicine_name'], medicine['medicine_image'])) # mysql.connection.commit() # return jsonify({"message": "done"}), 200 # @app.route('/select/', methods=['GET']) # def Select(id): # cur = mysql.connection.cursor() # cur.execute("SELECT * FROM users WHERE id=%s", (id,)) # table1 = cur.fetchall() # cur.execute("SELECT * FROM user_disease WHERE user_id=%s", (id,)) # table2 = cur.fetchall() # cur.execute("SELECT * FROM user_medicine WHERE user_id=%s", (id,)) # table3 = cur.fetchall() # cur.close() # user_data = { # "name": table1[0][1], # "email": table1[0][2], # "mobile": table1[0][3], # "mobile_emergency": table1[0][4], # "age": table1[0][5], # "gender": table1[0][6], # "location": table1[0][7], # "image": base64.b64encode(table1[0][8]).decode('utf-8'), # "user_disease": [{"disease_name": disease[1]} for disease in table2], # "user_medicine": [{"medicine_name": medicine[1], "medicine_image": base64.b64encode(medicine[3]).decode('utf-8')} for medicine in table3] # } # return jsonify(user_data), 200 # if __name__ == '__main__': # app.run(host='0.0.0.0', port=7860, debug=True) # init_connect() # app.run(host='192.168.137.241', port=5000, debug=True) # 192.168.1.114 http://192.168.137.241:5000 192.168.1.122