Spaces:
Sleeping
Sleeping
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') | |
def index(): | |
return "Server is Running..." | |
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 | |
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 | |
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 | |
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 | |
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 | |
def mindmapDemo(): | |
data = request.json | |
print(data) | |
return get_initial_data(), 200 | |
CORS(app) | |
if __name__ == '__main__': | |
app.run(debug=True) |