Spaces:
Sleeping
Sleeping
File size: 8,134 Bytes
07c0591 |
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 273 274 275 |
from flask import Flask, jsonify, request
from flask_cors import CORS
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import google.generativeai as genai
import urllib.parse
from models import UserSchema
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager, create_access_token
from middleware.authUser import auth_user
from datetime import timedelta
from controllers.demo import get_initial_data
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET')
# MongoDB configuration
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
restUri = os.getenv('REST_URI')
uri = f'mongodb+srv://{username}:{password}@cluster0.iidzcbc.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0'
client = MongoClient(uri)
db = client.GenUpNexus
users_collection = db["users3"]
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')
@app.route('/')
def index():
return "Server is Running..."
@app.route('/tree', methods=["POST", "GET"])
def tree():
if request.method == 'POST':
data = request.get_json()
query = data.get('query')
print(query)
response = model.generate_content('''I will give you a topic and you have to generate an explanation of the topic and respond with JSON structure as follows as i want to use this json are Nodes & Edges and visualise this using ReactFlow library, the json structure will be :
nodes = [
{
id: "1",
type: "input",
data: {
label: "Input Node",
},
position: { x: 250, y: 0 },
},
{
id: "2",
data: {
label: "Default Node",
},
position: { x: 100, y: 100 },
},
{
id: "3",
type: "output",
data: {
label: "Output Node",
},
position: { x: 400, y: 100 },
},
{
id: "4",
type: "custom",
position: { x: 100, y: 200 },
data: {
selects: {
"handle-0": "smoothstep",
"handle-1": "smoothstep",
},
},
},
{
id: "5",
type: "output",
data: {
label: "custom style",
},
className: "circle",
style: {
background: "#2B6CB0",
color: "white",
},
position: { x: 400, y: 200 },
sourcePosition: Position.Right,
targetPosition: Position.Left,
},
{
id: "6",
type: "output",
style: {
background: "#63B3ED",
color: "white",
width: 100,
},
data: {
label: "Node",
},
position: { x: 400, y: 325 },
sourcePosition: Position.Right,
targetPosition: Position.Left,
},
{
id: "7",
type: "default",
className: "annotation",
data: {
label: (
<>
On the bottom left you see the <strong>Controls</strong> and the
bottom right the <strong>MiniMap</strong>. This is also just a node 🥳
</>
),
},
draggable: false,
selectable: false,
position: { x: 150, y: 400 },
},
];
edges = [
{ id: "e1-2", source: "1", target: "2", label: "this is an edge label" },
{ id: "e1-3", source: "1", target: "3", animated: true },
{
id: "e4-5",
source: "4",
target: "5",
type: "smoothstep",
sourceHandle: "handle-0",
data: {
selectIndex: 0,
},
markerEnd: {
type: MarkerType.ArrowClosed,
},
},
{
id: "e4-6",
source: "4",
target: "6",
type: "smoothstep",
sourceHandle: "handle-1",
data: {
selectIndex: 1,
},
markerEnd: {
type: MarkerType.ArrowClosed,
},
},
];
Topic is: ''' + query)
# print(response.text)
return jsonify({'success': True, 'data': response.text})
# return temp
@app.route('/interview', methods=["POST", "GET"])
def interview():
if request.method == 'POST':
data = request.get_json()
if data.get('from') == 'client':
return "Success"
elif data.get('from') == 'gradio':
print(data)
return "Success"
# User Routes
@app.route('/user/signup', methods=['POST'])
def signup():
data = request.json
name = data.get('name')
email = data.get('email')
password = data.get('password')
if not email:
return jsonify({"error": "Invalid email"}), 400
existing_user = users_collection.find_one({"email": email})
if existing_user:
return jsonify({"message": "User already exists"}), 404
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
result = users_collection.insert_one({
"name": name,
"email": email,
"password": hashed_password
})
print(result);
expires = timedelta(days=7)
access_token = create_access_token(identity={"email": email, "id": str(result.inserted_id)}, expires_delta=expires)
res = {"name": name, "email": email}
return jsonify({"result": res, "token": access_token}), 201
@app.route('/user/signin', methods=['POST'])
def signin():
data = request.json
email = data.get('email')
password = data.get('password')
user = users_collection.find_one({"email": email})
if not user:
return jsonify({"message": "User doesn't exist"}), 404
if not bcrypt.check_password_hash(user['password'], password):
return jsonify({"message": "Invalid Credentials"}), 404
expires = timedelta(days=7)
access_token = create_access_token(identity={"email": user['email'], "id": str(user['_id'])}, expires_delta=expires)
res = {"name": user['name'], "email": user['email']}
return jsonify({"result": res, "token": access_token}), 200
#protected route wiht auth_user middleware
@app.route('/user/delete', methods=['POST'])
@auth_user
def delete_account():
email = request.email
print(email)
try:
result = users_collection.delete_one({"email": email})
if result.deleted_count == 1:
return jsonify({"result": True}), 200
else:
return jsonify({"result": False, "message": "User not found"}), 404
except Exception as e:
print(e)
return jsonify({"message": "Something went wrong"}), 500
@app.route('/mindmap/demo', methods=['POST'])
def mindmapDemo():
data = request.json
print(data)
return get_initial_data(), 200
CORS(app)
if __name__ == '__main__':
app.run(debug=True) |