Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -11,14 +11,14 @@ from flask_jwt_extended import JWTManager, create_access_token
|
|
11 |
from middleware.authUser import auth_user
|
12 |
from datetime import timedelta
|
13 |
from controllers.demo import get_initial_data
|
14 |
-
from controllers.mindmap import saveMindmap, getMindmap, getMindmapHistory, deleteMindmap, getMindmapByid, saveMindmapById
|
15 |
import json
|
16 |
-
from bson import ObjectId
|
17 |
from dotenv import load_dotenv
|
18 |
import os
|
19 |
import re
|
20 |
import requests
|
21 |
-
|
22 |
|
23 |
load_dotenv()
|
24 |
|
@@ -26,9 +26,10 @@ app = Flask(__name__)
|
|
26 |
|
27 |
bcrypt = Bcrypt(app)
|
28 |
jwt = JWTManager(app)
|
29 |
-
|
30 |
|
31 |
app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET')
|
|
|
32 |
|
33 |
# MongoDB configuration
|
34 |
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
|
@@ -55,7 +56,36 @@ except Exception as e:
|
|
55 |
GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
|
56 |
|
57 |
genai.configure(api_key=GOOGLE_API_KEY)
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Caches to reduce no of queries to MongoDB...
|
61 |
user_id_ping = {'current': 0}
|
@@ -69,7 +99,6 @@ def index():
|
|
69 |
def index2():
|
70 |
return "routes checking..."
|
71 |
|
72 |
-
|
73 |
#generative model routes
|
74 |
@app.route('/tree', methods=["POST", "GET"])
|
75 |
def tree():
|
@@ -165,6 +194,7 @@ def tree():
|
|
165 |
# return temp
|
166 |
|
167 |
@app.route('/tree/demo', methods=["POST"])
|
|
|
168 |
def treeDemo():
|
169 |
if request.method == 'POST':
|
170 |
data = request.get_json()
|
@@ -224,28 +254,36 @@ Topic is: ''' + query)
|
|
224 |
# return temp
|
225 |
|
226 |
@app.route('/mindmap/generate/data', methods=["POST"])
|
|
|
227 |
def generateData():
|
228 |
if request.method == 'POST':
|
229 |
data = request.get_json()
|
230 |
topic = data.get('topic')
|
231 |
description = data.get('description')
|
232 |
category = data.get('category')
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
print(response.text)
|
245 |
json_data = response.text
|
246 |
-
modified_json_data = json_data[8:-3]
|
247 |
|
248 |
-
|
|
|
|
|
249 |
|
250 |
def res(user_id):
|
251 |
avg_text = 0
|
@@ -274,7 +312,6 @@ def res(user_id):
|
|
274 |
|
275 |
return True
|
276 |
|
277 |
-
|
278 |
@app.route('/interview', methods=["POST", "GET"])
|
279 |
def interview():
|
280 |
if request.method == 'POST':
|
@@ -598,126 +635,155 @@ def mindmapDelete():
|
|
598 |
data = request.json
|
599 |
return deleteMindmap(userId, data, savedMindmap)
|
600 |
|
601 |
-
|
602 |
@app.route('/mindmap/demo', methods=['POST'])
|
603 |
def mindmapDemo():
|
604 |
data = request.json
|
605 |
print(data)
|
606 |
return get_initial_data(), 200
|
607 |
|
|
|
608 |
@app.route('/roadmap', methods=['POST'])
|
|
|
609 |
def roadmap():
|
610 |
if request.method == 'POST':
|
611 |
data = request.get_json()
|
612 |
user_id = data.get('user_id')
|
613 |
-
day_data = data.get('todayData')
|
614 |
|
615 |
if data.get('type') == 1:
|
|
|
|
|
|
|
|
|
|
|
616 |
# Generate New Roadmap
|
617 |
-
|
618 |
-
|
619 |
-
{
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
]
|
666 |
-
},
|
667 |
-
...
|
668 |
-
continue for other weeks...
|
669 |
-
]
|
670 |
-
Generate topics for all 7 days in a week and reference links only links(no header) accordingly.
|
671 |
-
''') # Prompt
|
672 |
|
673 |
-
|
674 |
-
|
|
|
|
|
|
|
675 |
print(response.text)
|
676 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
677 |
|
678 |
print(roads_data)
|
679 |
-
|
680 |
-
|
681 |
-
|
|
|
|
|
682 |
url = "https://youtube.googleapis.com/youtube/v3/search"
|
683 |
|
684 |
for index, ele in enumerate(roads_data):
|
685 |
for i in range(0,7):
|
686 |
params = {
|
687 |
"part": "snippet",
|
688 |
-
"q": ele[f"week{index + 1}"]['data'][f"day{i+1}"]['heading'],
|
689 |
-
"key": API_KEY
|
|
|
690 |
}
|
691 |
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
706 |
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
temp2.append({'link': link, 'visited': False})
|
711 |
|
712 |
-
|
713 |
-
|
714 |
else:
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
|
720 |
-
return jsonify({'success': True})
|
721 |
|
722 |
elif data.get('type') == 2:
|
723 |
# Get roadmap data
|
@@ -726,10 +792,29 @@ Generate topics for all 7 days in a week and reference links only links(no heade
|
|
726 |
return jsonify({'success': True, 'roadmapData': {'data': roadmap_data['data']}, 'activeDays': roadmap_data['activeDays']})
|
727 |
else:
|
728 |
return jsonify({'success': False})
|
729 |
-
# return jsonify({'success': True, 'roadmapData': roads_data_demo, 'activeDays': active_days})
|
730 |
|
731 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
732 |
@app.route('/roadmapmodder', methods=['POST'])
|
|
|
733 |
def roadmapmodder():
|
734 |
if request.method == 'POST':
|
735 |
data = request.get_json()
|
@@ -874,5 +959,6 @@ def roadmapmodder():
|
|
874 |
return jsonify({'success': True})
|
875 |
|
876 |
|
|
|
877 |
if __name__ == '__main__':
|
878 |
app.run(debug=True, host='0.0.0.0')
|
|
|
11 |
from middleware.authUser import auth_user
|
12 |
from datetime import timedelta
|
13 |
from controllers.demo import get_initial_data
|
14 |
+
from controllers.mindmap import saveMindmap, getMindmap, getMindmapHistory, deleteMindmap, getMindmapByid, saveMindmapById, saveGeneratedData
|
15 |
import json
|
16 |
+
from bson import ObjectId, json_util
|
17 |
from dotenv import load_dotenv
|
18 |
import os
|
19 |
import re
|
20 |
import requests
|
21 |
+
import time
|
22 |
|
23 |
load_dotenv()
|
24 |
|
|
|
26 |
|
27 |
bcrypt = Bcrypt(app)
|
28 |
jwt = JWTManager(app)
|
29 |
+
|
30 |
|
31 |
app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET')
|
32 |
+
CORS(app,resources={r"/*":{"origins":"*"}})
|
33 |
|
34 |
# MongoDB configuration
|
35 |
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
|
|
|
56 |
GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
|
57 |
|
58 |
genai.configure(api_key=GOOGLE_API_KEY)
|
59 |
+
|
60 |
+
generation_config = {
|
61 |
+
"temperature": 0.8,
|
62 |
+
"top_p": 1,
|
63 |
+
"top_k": 40,
|
64 |
+
"max_output_tokens": 2048,
|
65 |
+
}
|
66 |
+
|
67 |
+
safety_settings = [
|
68 |
+
{
|
69 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
70 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
74 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
78 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
82 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
83 |
+
},
|
84 |
+
]
|
85 |
+
|
86 |
+
model = genai.GenerativeModel('gemini-1.0-pro',
|
87 |
+
generation_config=generation_config,
|
88 |
+
safety_settings=safety_settings)
|
89 |
|
90 |
# Caches to reduce no of queries to MongoDB...
|
91 |
user_id_ping = {'current': 0}
|
|
|
99 |
def index2():
|
100 |
return "routes checking..."
|
101 |
|
|
|
102 |
#generative model routes
|
103 |
@app.route('/tree', methods=["POST", "GET"])
|
104 |
def tree():
|
|
|
194 |
# return temp
|
195 |
|
196 |
@app.route('/tree/demo', methods=["POST"])
|
197 |
+
@auth_user
|
198 |
def treeDemo():
|
199 |
if request.method == 'POST':
|
200 |
data = request.get_json()
|
|
|
254 |
# return temp
|
255 |
|
256 |
@app.route('/mindmap/generate/data', methods=["POST"])
|
257 |
+
# @auth_user
|
258 |
def generateData():
|
259 |
if request.method == 'POST':
|
260 |
data = request.get_json()
|
261 |
topic = data.get('topic')
|
262 |
description = data.get('description')
|
263 |
category = data.get('category')
|
264 |
+
nodeId = data.get('nodeId')
|
265 |
+
mapId = data.get('mapId')
|
266 |
+
# userId = request.userId
|
267 |
+
|
268 |
+
# try:
|
269 |
+
# mindMap = savedMindmap.find_one({"_id": ObjectId(mapId)})
|
270 |
+
|
271 |
+
# if mindMap["userId"] != userId:
|
272 |
+
# return jsonify({"message": "Unauthorized"}), 401
|
273 |
+
|
274 |
+
# except Exception as e:
|
275 |
+
# print(e)
|
276 |
+
# return jsonify({"error": "An error occurred"}), 500
|
277 |
+
|
278 |
+
response = model.generate_content('''I will provide you a topic & related description. You need to generate its detailed explanation, also provide its links to the related topics and respond in markdown.
|
279 |
+
- make sure the content is well structured and easy to read.
|
280 |
+
Topic: ''' + topic + '\n\n' + '''Description: ''' + description)
|
281 |
print(response.text)
|
282 |
json_data = response.text
|
|
|
283 |
|
284 |
+
saveGeneratedData(mapId, nodeId, json_data, savedMindmap)
|
285 |
+
|
286 |
+
return jsonify({'success': True, 'data': json_data})
|
287 |
|
288 |
def res(user_id):
|
289 |
avg_text = 0
|
|
|
312 |
|
313 |
return True
|
314 |
|
|
|
315 |
@app.route('/interview', methods=["POST", "GET"])
|
316 |
def interview():
|
317 |
if request.method == 'POST':
|
|
|
635 |
data = request.json
|
636 |
return deleteMindmap(userId, data, savedMindmap)
|
637 |
|
|
|
638 |
@app.route('/mindmap/demo', methods=['POST'])
|
639 |
def mindmapDemo():
|
640 |
data = request.json
|
641 |
print(data)
|
642 |
return get_initial_data(), 200
|
643 |
|
644 |
+
|
645 |
@app.route('/roadmap', methods=['POST'])
|
646 |
+
# @auth_user
|
647 |
def roadmap():
|
648 |
if request.method == 'POST':
|
649 |
data = request.get_json()
|
650 |
user_id = data.get('user_id')
|
|
|
651 |
|
652 |
if data.get('type') == 1:
|
653 |
+
day_data = data.get('todayData')
|
654 |
+
position = data.get('position')
|
655 |
+
time_road = data.get('time')
|
656 |
+
company = data.get('company')
|
657 |
+
|
658 |
# Generate New Roadmap
|
659 |
+
chat_starter = '''Generate a week-wise roadmap for '''+ position + ''' in English in the following JSON format:
|
660 |
+
{
|
661 |
+
"week1": {
|
662 |
+
"title": "...",
|
663 |
+
"description": "What will i learn in this week...",
|
664 |
+
"data": {
|
665 |
+
"day1": {
|
666 |
+
"description": "...",
|
667 |
+
"heading": "What will I do on this day...",
|
668 |
+
"links": [ "link1", "link2", .... ]
|
669 |
+
},
|
670 |
+
"day2": {
|
671 |
+
"description": "...",
|
672 |
+
"heading": "What will I do on this day...",
|
673 |
+
"links": [ "link1", "link2", .... ]
|
674 |
+
},
|
675 |
+
"day3": {
|
676 |
+
"description": "...",
|
677 |
+
"heading": "What will I do on this day...",
|
678 |
+
"links": [ "link1", "link2", .... ]
|
679 |
+
},
|
680 |
+
"day4": {
|
681 |
+
"description": "...",
|
682 |
+
"heading": "What will I do on this day...",
|
683 |
+
"links": [ "link1", "link2", .... ]
|
684 |
+
},
|
685 |
+
"day5": {
|
686 |
+
"description": "...",
|
687 |
+
"heading": "What will I do on this day...",
|
688 |
+
"links": [ "link1", "link2", .... ]
|
689 |
+
},
|
690 |
+
"day6": {
|
691 |
+
"description": "...",
|
692 |
+
"heading": "What will I do on this day...",
|
693 |
+
"links": [ "link1", "link2", .... ]
|
694 |
+
},
|
695 |
+
"day7": {
|
696 |
+
"description": "...",
|
697 |
+
"heading": "What will I do on this day...",
|
698 |
+
"links": [ "link1", "link2", .... ]
|
699 |
+
}
|
700 |
+
}
|
701 |
+
}
|
702 |
+
}
|
703 |
+
- I am preparing to get '''+ position + ''' at ''' + company + '''company.
|
704 |
+
- I will ask you one by one to generate for next weeks. Generate only links, do not add anything to links.
|
705 |
+
- It is required that the response should be an object. Don't include any other verbose explanations and don't include the markdown syntax anywhere.
|
706 |
+
- Roadmap length is of total ''' + time_road + ''' weeks. So adjust the content among the weeks accordingly.''' # Prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
707 |
|
708 |
+
roadmap_list = []
|
709 |
+
|
710 |
+
chat = model.start_chat(history=[])
|
711 |
+
response = chat.send_message(chat_starter)
|
712 |
+
|
713 |
print(response.text)
|
714 |
+
|
715 |
+
if response.text[0] == '{':
|
716 |
+
roadmap_list.append(json.loads(response.text))
|
717 |
+
elif response.text[0] == '`':
|
718 |
+
roadmap_list.append(json.loads(response.text[7:-3]))
|
719 |
+
|
720 |
+
for i in range(1,int(time_road)):
|
721 |
+
new_response = chat.send_message(f"Now generate for next week{i+1}")
|
722 |
+
print(new_response.text)
|
723 |
+
if new_response.text[0] == '{':
|
724 |
+
roadmap_list.append(json.loads(new_response.text))
|
725 |
+
elif new_response.text[0] == '`':
|
726 |
+
roadmap_list.append(json.loads(new_response.text[7:-3]))
|
727 |
+
|
728 |
+
roads_data = roadmap_list
|
729 |
|
730 |
print(roads_data)
|
731 |
+
|
732 |
+
time.sleep(60)
|
733 |
+
|
734 |
+
# Youtube Integration & links modification
|
735 |
+
API_KEY = os.getenv('YOUTUBE_API_KEY')
|
736 |
url = "https://youtube.googleapis.com/youtube/v3/search"
|
737 |
|
738 |
for index, ele in enumerate(roads_data):
|
739 |
for i in range(0,7):
|
740 |
params = {
|
741 |
"part": "snippet",
|
742 |
+
"q": ele[f"week{index + 1}"]['data'][f"day{i+1}"]['heading'] + ele[f"week{index + 1}"]['title'],
|
743 |
+
"key": API_KEY,
|
744 |
+
"maxResults" : 3
|
745 |
}
|
746 |
|
747 |
+
response = requests.get(url, params=params)
|
748 |
+
|
749 |
+
if response.status_code == 200:
|
750 |
+
video_data = response.json()
|
751 |
+
|
752 |
+
temp1 = []
|
753 |
+
for vid in video_data['items']:
|
754 |
+
if vid['id']['kind']=='youtube#playlist':
|
755 |
+
temp1.append({'viewed': False, 'type': 'playlist', 'playlistId': vid['id']['playlistId'], 'videoTitle': vid['snippet']['title'], 'channelName': vid['snippet']['channelTitle'], 'thumbnail': vid['snippet']['thumbnails']['high']['url']})
|
756 |
+
elif vid['id']['kind']=='youtube#video':
|
757 |
+
temp1.append({'viewed': False, 'type': 'video', 'videoId': vid['id']['videoId'], 'videoTitle': vid['snippet']['title'], 'channelName': vid['snippet']['channelTitle'], 'thumbnail': vid['snippet']['thumbnails']['high']['url']})
|
758 |
+
|
759 |
+
ele[f"week{index + 1}"]['data'][f"day{i+1}"]['youtube'] = temp1
|
760 |
+
|
761 |
+
# Reference Links
|
762 |
+
temp2 = []
|
763 |
+
for link in ele[f"week{index + 1}"]['data'][f"day{i+1}"]['links']:
|
764 |
+
temp2.append({'link': link, 'visited': False})
|
765 |
+
|
766 |
+
ele[f"week{index + 1}"]['data'][f"day{i+1}"]['links'] = temp2
|
767 |
+
|
768 |
+
else:
|
769 |
+
print("Error:", response.json())
|
770 |
+
|
771 |
+
# Save to MongoDB
|
772 |
+
roadmap_collection.insert_one({'data': roads_data, 'title': position, 'activeDays': [day_data], 'userId': user_id})
|
773 |
|
774 |
+
try:
|
775 |
+
results = roadmap_collection.find({"userId": user_id}, {"_id": 1, "title": 1, "activeDays": 1}).limit(5)
|
776 |
+
roadmaps = [{"_id": str(result["_id"]), "title": result["title"], "activeDays": result["activeDays"]} for result in results]
|
|
|
777 |
|
778 |
+
if roadmaps:
|
779 |
+
return json_util.dumps({"data": roadmaps}), 200
|
780 |
else:
|
781 |
+
return jsonify({"msg": "No Mindmap stored"}), 404
|
782 |
+
except Exception as e:
|
783 |
+
print(e)
|
784 |
+
return jsonify({"error": "An error occurred"}), 500
|
785 |
|
786 |
+
# return jsonify({'success': True})
|
787 |
|
788 |
elif data.get('type') == 2:
|
789 |
# Get roadmap data
|
|
|
792 |
return jsonify({'success': True, 'roadmapData': {'data': roadmap_data['data']}, 'activeDays': roadmap_data['activeDays']})
|
793 |
else:
|
794 |
return jsonify({'success': False})
|
|
|
795 |
|
796 |
+
|
797 |
+
|
798 |
+
@app.route('/roadmap/history', methods=['GET'])
|
799 |
+
@auth_user
|
800 |
+
def roadmapGetHistory():
|
801 |
+
userId = request.userId
|
802 |
+
try:
|
803 |
+
results = roadmap_collection.find({"userId": userId}, {"_id": 1, "title": 1, "activeDays": 1}).limit(5)
|
804 |
+
roadmaps = [{"_id": str(result["_id"]), "title": result["title"], "activeDays": result["activeDays"]} for result in results]
|
805 |
+
|
806 |
+
if roadmaps:
|
807 |
+
return json_util.dumps({"data": roadmaps}), 200
|
808 |
+
else:
|
809 |
+
return jsonify({"msg": "No Mindmap stored"}), 404
|
810 |
+
except Exception as e:
|
811 |
+
print(e)
|
812 |
+
return jsonify({"error": "An error occurred"}), 500
|
813 |
+
|
814 |
+
|
815 |
+
|
816 |
@app.route('/roadmapmodder', methods=['POST'])
|
817 |
+
@auth_user
|
818 |
def roadmapmodder():
|
819 |
if request.method == 'POST':
|
820 |
data = request.get_json()
|
|
|
959 |
return jsonify({'success': True})
|
960 |
|
961 |
|
962 |
+
|
963 |
if __name__ == '__main__':
|
964 |
app.run(debug=True, host='0.0.0.0')
|