Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,13 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
import json
|
| 4 |
import openai
|
| 5 |
-
from typing import List, Dict, Any
|
| 6 |
import os
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
overall_study_pattern: str
|
| 12 |
memorization_study_pattern: str
|
| 13 |
problem_solving_study_pattern: str
|
|
@@ -16,190 +239,140 @@ class RoadmapInput(BaseModel):
|
|
| 16 |
new_topic_approach: str
|
| 17 |
old_topic_approach: str
|
| 18 |
topic_ratio: str
|
| 19 |
-
hours_of_study:
|
| 20 |
-
hours_of_study_weekends:
|
| 21 |
revision_days: str
|
| 22 |
test_days: str
|
|
|
|
|
|
|
|
|
|
| 23 |
completed_phy_chapters: List[str]
|
| 24 |
completed_chem_chapters: List[str]
|
| 25 |
completed_maths_chapters: List[str]
|
| 26 |
-
|
| 27 |
-
startchemRoadmapFrom: str
|
| 28 |
-
startmathRoadmapFrom: str
|
| 29 |
-
userleft_topic_phy: List[dict]
|
| 30 |
-
userleft_topic_chem: List[dict]
|
| 31 |
-
userleft_topic_maths: List[dict]
|
| 32 |
-
|
| 33 |
-
output_structure = """
|
| 34 |
-
{
|
| 35 |
-
"schedule": [
|
| 36 |
-
"days": [
|
| 37 |
-
{
|
| 38 |
-
"dayNumber": int,
|
| 39 |
-
"subjects": [
|
| 40 |
-
{
|
| 41 |
-
"name": "string",
|
| 42 |
-
"tasks": [
|
| 43 |
-
{
|
| 44 |
-
"type": "string",
|
| 45 |
-
"topic": "string",
|
| 46 |
-
"time": "string"
|
| 47 |
-
}
|
| 48 |
-
]
|
| 49 |
-
}
|
| 50 |
-
]
|
| 51 |
-
}
|
| 52 |
-
]
|
| 53 |
-
]
|
| 54 |
-
}
|
| 55 |
-
"""
|
| 56 |
|
| 57 |
-
# Function to remove completed chapters
|
| 58 |
def remove_completed_chapters(subject_data, completed_chapters):
|
| 59 |
subject_data["chapters"] = [chapter for chapter in subject_data["chapters"]
|
| 60 |
if chapter["chapter"] not in completed_chapters]
|
| 61 |
return subject_data
|
| 62 |
|
| 63 |
-
# Function to get
|
| 64 |
-
def
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if 'chapters' not in subject:
|
| 70 |
-
print(f"Error: 'chapters' key not found in the subject: {subject}")
|
| 71 |
-
return collected_subtopics
|
| 72 |
-
|
| 73 |
-
for chapter in subject["chapters"]:
|
| 74 |
-
if 'subtopics' not in chapter:
|
| 75 |
-
print(f"Error: 'subtopics' key not found in the chapter: {chapter}")
|
| 76 |
-
continue
|
| 77 |
-
|
| 78 |
-
for subtopic in chapter["subtopics"]:
|
| 79 |
-
if (subtopic["subtopic"] == start_subtopic):
|
| 80 |
-
start_collecting = True
|
| 81 |
-
continue
|
| 82 |
-
|
| 83 |
-
if start_collecting and subtopic["subtopic_hours"]:
|
| 84 |
-
time_spent += float(subtopic["subtopic_hours"])
|
| 85 |
-
if time_spent <= allowed_time:
|
| 86 |
-
collected_subtopics.append({
|
| 87 |
-
"chapter": chapter["chapter"],
|
| 88 |
-
"subtopic": subtopic["subtopic"],
|
| 89 |
-
"subtopic_hours": subtopic["subtopic_hours"]
|
| 90 |
-
})
|
| 91 |
-
else:
|
| 92 |
-
break
|
| 93 |
-
if time_spent > allowed_time:
|
| 94 |
-
break
|
| 95 |
-
|
| 96 |
-
return collected_subtopics
|
| 97 |
-
|
| 98 |
|
| 99 |
@app.post("/generate_roadmap")
|
| 100 |
-
async def generate_roadmap(
|
| 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 |
-
YOU ARE NOT CONSTRAINED TO CREATE A ROADMAP FOR ONLY 'X' NUMBER OF DAYS , YOU CAN EXTEND TILL THE TOPICS ARE FINISHED BUT ONLY STICK TO THE TIMEFRAME ALLOTED FOR EACH SUBJECT AND DO NOT GO ABOVE OR BELOW THAT TIME FRAME.
|
| 172 |
-
"""
|
| 173 |
-
openai.api_key=os.getenv("KEY")
|
| 174 |
-
# First LLM call
|
| 175 |
response = openai.ChatCompletion.create(
|
| 176 |
-
model="gpt-
|
| 177 |
messages=[
|
| 178 |
{
|
| 179 |
"role": "system",
|
| 180 |
-
"content": sys_prompt + "MAKE SURE YOU VERY VERY
|
| 181 |
},
|
| 182 |
{
|
| 183 |
"role": "user",
|
| 184 |
-
"content":
|
| 185 |
}
|
| 186 |
]
|
| 187 |
)
|
| 188 |
|
| 189 |
answer = response['choices'][0]['message']['content'].strip()
|
| 190 |
|
| 191 |
-
#
|
| 192 |
-
provided_subtopics =
|
| 193 |
-
|
| 194 |
-
# Second LLM call
|
| 195 |
response = openai.ChatCompletion.create(
|
| 196 |
-
model="gpt-
|
| 197 |
messages=[
|
| 198 |
{
|
| 199 |
"role": "system",
|
| 200 |
"content": f'''
|
| 201 |
you created a very good roadmap {answer} but you make sure that you dont forget any subtopics from {provided_subtopics}. ensure that the style is same as the previous roadmap.
|
| 202 |
-
MAKE SURE YOU VERY VERY
|
| 203 |
DO not include json at the top of the answer
|
| 204 |
'''
|
| 205 |
},
|
|
@@ -210,8 +383,8 @@ async def generate_roadmap(input_data: RoadmapInput):
|
|
| 210 |
]
|
| 211 |
)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
parsed_json = json.loads(
|
| 215 |
|
| 216 |
return parsed_json
|
| 217 |
except Exception as e:
|
|
@@ -219,4 +392,4 @@ async def generate_roadmap(input_data: RoadmapInput):
|
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
import uvicorn
|
| 222 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
# from fastapi import FastAPI, HTTPException
|
| 2 |
+
# from pydantic import BaseModel
|
| 3 |
+
# import json
|
| 4 |
+
# import openai
|
| 5 |
+
# from typing import List, Dict, Any, Optional
|
| 6 |
+
# import os
|
| 7 |
+
|
| 8 |
+
# app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# class RoadmapInput(BaseModel):
|
| 11 |
+
# overall_study_pattern: str
|
| 12 |
+
# memorization_study_pattern: str
|
| 13 |
+
# problem_solving_study_pattern: str
|
| 14 |
+
# visualization_study_pattern: str
|
| 15 |
+
# obstacle_study_pattern: str
|
| 16 |
+
# new_topic_approach: str
|
| 17 |
+
# old_topic_approach: str
|
| 18 |
+
# topic_ratio: str
|
| 19 |
+
# hours_of_study: str
|
| 20 |
+
# hours_of_study_weekends: str
|
| 21 |
+
# revision_days: str
|
| 22 |
+
# test_days: str
|
| 23 |
+
# completed_phy_chapters: List[str]
|
| 24 |
+
# completed_chem_chapters: List[str]
|
| 25 |
+
# completed_maths_chapters: List[str]
|
| 26 |
+
# startphyRoadmapFrom: str
|
| 27 |
+
# startchemRoadmapFrom: str
|
| 28 |
+
# startmathRoadmapFrom: str
|
| 29 |
+
# userleft_topic_phy: List[dict]
|
| 30 |
+
# userleft_topic_chem: List[dict]
|
| 31 |
+
# userleft_topic_maths: List[dict]
|
| 32 |
+
|
| 33 |
+
# output_structure = """
|
| 34 |
+
# {
|
| 35 |
+
# "schedule": [
|
| 36 |
+
# "days": [
|
| 37 |
+
# {
|
| 38 |
+
# "dayNumber": int,
|
| 39 |
+
# "subjects": [
|
| 40 |
+
# {
|
| 41 |
+
# "name": "string",
|
| 42 |
+
# "tasks": [
|
| 43 |
+
# {
|
| 44 |
+
# "type": "string",
|
| 45 |
+
# "topic": "string",
|
| 46 |
+
# "time": "string"
|
| 47 |
+
# }
|
| 48 |
+
# ]
|
| 49 |
+
# }
|
| 50 |
+
# ]
|
| 51 |
+
# }
|
| 52 |
+
# ]
|
| 53 |
+
# ]
|
| 54 |
+
# }
|
| 55 |
+
# """
|
| 56 |
+
|
| 57 |
+
# # Function to remove completed chapters from each subject
|
| 58 |
+
# def remove_completed_chapters(subject_data, completed_chapters):
|
| 59 |
+
# subject_data["chapters"] = [chapter for chapter in subject_data["chapters"]
|
| 60 |
+
# if chapter["chapter"] not in completed_chapters]
|
| 61 |
+
# return subject_data
|
| 62 |
+
|
| 63 |
+
# # Function to get topics within timeframe
|
| 64 |
+
# def get_topics_within_timeframe(subject, allowed_time, start_subtopic):
|
| 65 |
+
# collected_subtopics = []
|
| 66 |
+
# time_spent = 0
|
| 67 |
+
# start_collecting = False
|
| 68 |
+
|
| 69 |
+
# if 'chapters' not in subject:
|
| 70 |
+
# print(f"Error: 'chapters' key not found in the subject: {subject}")
|
| 71 |
+
# return collected_subtopics
|
| 72 |
+
|
| 73 |
+
# for chapter in subject["chapters"]:
|
| 74 |
+
# if 'subtopics' not in chapter:
|
| 75 |
+
# print(f"Error: 'subtopics' key not found in the chapter: {chapter}")
|
| 76 |
+
# continue
|
| 77 |
+
|
| 78 |
+
# for subtopic in chapter["subtopics"]:
|
| 79 |
+
# if (subtopic["subtopic"] == start_subtopic):
|
| 80 |
+
# start_collecting = True
|
| 81 |
+
# continue
|
| 82 |
+
|
| 83 |
+
# if start_collecting and subtopic["subtopic_hours"]:
|
| 84 |
+
# time_spent += float(subtopic["subtopic_hours"])
|
| 85 |
+
# if time_spent <= allowed_time:
|
| 86 |
+
# collected_subtopics.append({
|
| 87 |
+
# "chapter": chapter["chapter"],
|
| 88 |
+
# "subtopic": subtopic["subtopic"],
|
| 89 |
+
# "subtopic_hours": subtopic["subtopic_hours"]
|
| 90 |
+
# })
|
| 91 |
+
# else:
|
| 92 |
+
# break
|
| 93 |
+
# if time_spent > allowed_time:
|
| 94 |
+
# break
|
| 95 |
+
|
| 96 |
+
# return collected_subtopics
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# @app.post("/generate_roadmap")
|
| 100 |
+
# async def generate_roadmap(input_data: RoadmapInput):
|
| 101 |
+
# try:
|
| 102 |
+
# # Load JSON data for each subject
|
| 103 |
+
# with open('Physics.json', 'r', encoding='utf-8') as file:
|
| 104 |
+
# phy = json.load(file)
|
| 105 |
+
|
| 106 |
+
# with open('Chemistry.json', 'r', encoding='utf-8') as file:
|
| 107 |
+
# chem = json.load(file)
|
| 108 |
+
|
| 109 |
+
# with open('Maths.json', 'r', encoding='utf-8') as file:
|
| 110 |
+
# maths = json.load(file)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# # Remove completed chapters
|
| 115 |
+
# phy = remove_completed_chapters(phy, input_data.completed_phy_chapters)
|
| 116 |
+
# chem = remove_completed_chapters(chem, input_data.completed_chem_chapters)
|
| 117 |
+
# maths = remove_completed_chapters(maths, input_data.completed_maths_chapters)
|
| 118 |
+
# total_time=10;
|
| 119 |
+
# # Get topics within timeframe
|
| 120 |
+
# physics_topics = get_topics_within_timeframe(phy, total_time, input_data.startphyRoadmapFrom)
|
| 121 |
+
# chemistry_topics = get_topics_within_timeframe(chem, total_time, input_data.startchemRoadmapFrom)
|
| 122 |
+
# mathematics_topics = get_topics_within_timeframe(maths, total_time, input_data.startmathRoadmapFrom)
|
| 123 |
+
|
| 124 |
+
# for topic in reversed(input_data.userleft_topic_phy):
|
| 125 |
+
# physics_topics.insert(0, topic)
|
| 126 |
+
# for topic in reversed(input_data.userleft_topic_chem):
|
| 127 |
+
# chemistry_topics.insert(0, topic)
|
| 128 |
+
# for topic in reversed(input_data.userleft_topic_maths):
|
| 129 |
+
# mathematics_topics.insert(0, topic)
|
| 130 |
+
|
| 131 |
+
# print(physics_topics)
|
| 132 |
+
# print(chemistry_topics)
|
| 133 |
+
# print(mathematics_topics)
|
| 134 |
+
|
| 135 |
+
# # User persona string
|
| 136 |
+
# userPersona = f"""
|
| 137 |
+
# You are required to generate a highly personalized roadmap for a student studying Physics, Chemistry, and Mathematics for the JEE Main exam.
|
| 138 |
+
# The roadmap should be tailored based on the following student-specific details:
|
| 139 |
+
|
| 140 |
+
# 1. *Study Preferences:*
|
| 141 |
+
# - Study Pattern: {input_data.overall_study_pattern}
|
| 142 |
+
# - Memorization Approach: {input_data.memorization_study_pattern}
|
| 143 |
+
# - Problem-Solving Approach: {input_data.problem_solving_study_pattern}
|
| 144 |
+
# - Visualization Approach: {input_data.visualization_study_pattern}
|
| 145 |
+
|
| 146 |
+
# 2. *Handling Challenges:*
|
| 147 |
+
# - If unable to understand a topic: {input_data.obstacle_study_pattern}
|
| 148 |
+
# - Approach to New Topics: {input_data.new_topic_approach}
|
| 149 |
+
# - Approach to Previously Encountered Topics: {input_data.old_topic_approach}
|
| 150 |
+
|
| 151 |
+
# 3. *Study Hours:*
|
| 152 |
+
# - Weekdays: {input_data.hours_of_study} hours/day
|
| 153 |
+
# - Weekends: {input_data.hours_of_study_weekends} hours/day
|
| 154 |
+
# - Time Allocation Ratio (Physics:Chemistry:Mathematics): {input_data.topic_ratio}
|
| 155 |
+
# - By weekdays I mean day 1, day 2 , day 3 , day 4 , day 5
|
| 156 |
+
# - By weekends I mean day 6 , day 7
|
| 157 |
+
# """
|
| 158 |
+
|
| 159 |
+
# # System prompt
|
| 160 |
+
# sys_prompt = f"""
|
| 161 |
+
# You are required to generate a highly personalized roadmap for a student studying Physics, Chemistry, and Mathematics for the JEE Main exam.
|
| 162 |
+
# The roadmap should be tailored based on the following student-specific details:
|
| 163 |
+
|
| 164 |
+
# The roadmap must be provided in the following format:
|
| 165 |
+
# {output_structure}
|
| 166 |
+
|
| 167 |
+
# Do not include anything other than the roadmap, and ensure the focus remains strictly on the subjects {physics_topics}, {chemistry_topics}, and {mathematics_topics} and associated chapters.
|
| 168 |
+
# MAKE SURE THAT YOU MAKE THE ROADMAP FOR ALL THE THREE CHAPTERS EACH OF PHYSICS , CHEMISTRY AND MATHS TO COMPLETE THOSE CHAPTERS WITH 4 ASPECTS i.e "CONCEPT UNDERSTANDING","QUESTION PRACTICE","REVISION","TEST". ALSO INCLUDE TIME FOR EACH TASK THAT YOU GENERATE
|
| 169 |
+
# MAKE SURE THAT WE FIRST COMPLETE 1) CONCEPT UNDERSTANDING , 2) QUESTION PRACTICE FOR EVERY SUBTOPIC AND THEN REVISION AND TEST FOR WHOLE CHAPTER TOGETHER.
|
| 170 |
+
# MAKE SURE THAT WE INCLUDE EACH SUBTOPIC OF EACH CHAPTER FROM {physics_topics},{chemistry_topics} and {mathematics_topics} IS FINISHED
|
| 171 |
+
# YOU ARE NOT CONSTRAINED TO CREATE A ROADMAP FOR ONLY 'X' NUMBER OF DAYS , YOU CAN EXTEND TILL THE TOPICS ARE FINISHED BUT ONLY STICK TO THE TIMEFRAME ALLOTED FOR EACH SUBJECT AND DO NOT GO ABOVE OR BELOW THAT TIME FRAME.
|
| 172 |
+
# """
|
| 173 |
+
# openai.api_key=os.getenv("KEY")
|
| 174 |
+
# # First LLM call
|
| 175 |
+
# response = openai.ChatCompletion.create(
|
| 176 |
+
# model="gpt-4o-mini",
|
| 177 |
+
# messages=[
|
| 178 |
+
# {
|
| 179 |
+
# "role": "system",
|
| 180 |
+
# "content": sys_prompt + "MAKE SURE YOU VERY VERY STRICTLY FOLLOW THE JSON STRUCTURE BECAUSE I WILL PARSE YOUR OUTPUT TO JSON"
|
| 181 |
+
# },
|
| 182 |
+
# {
|
| 183 |
+
# "role": "user",
|
| 184 |
+
# "content": userPersona
|
| 185 |
+
# }
|
| 186 |
+
# ]
|
| 187 |
+
# )
|
| 188 |
+
|
| 189 |
+
# answer = response['choices'][0]['message']['content'].strip()
|
| 190 |
+
|
| 191 |
+
# # Calculate provided subtopics
|
| 192 |
+
# provided_subtopics = physics_topics + chemistry_topics + mathematics_topics
|
| 193 |
+
|
| 194 |
+
# # Second LLM call
|
| 195 |
+
# response = openai.ChatCompletion.create(
|
| 196 |
+
# model="gpt-4o-mini",
|
| 197 |
+
# messages=[
|
| 198 |
+
# {
|
| 199 |
+
# "role": "system",
|
| 200 |
+
# "content": f'''
|
| 201 |
+
# you created a very good roadmap {answer} but you make sure that you dont forget any subtopics from {provided_subtopics}. ensure that the style is same as the previous roadmap.
|
| 202 |
+
# MAKE SURE YOU VERY VERY STRICTLY FOLLOW THE JSON STRUCTURE BECAUSE I WILL PARSE YOUR OUTPUT TO JSON.
|
| 203 |
+
# DO not include json at the top of the answer
|
| 204 |
+
# '''
|
| 205 |
+
# },
|
| 206 |
+
# {
|
| 207 |
+
# "role": "user",
|
| 208 |
+
# "content": "Generate"
|
| 209 |
+
# }
|
| 210 |
+
# ]
|
| 211 |
+
# )
|
| 212 |
+
|
| 213 |
+
# answer = response['choices'][0]['message']['content'].strip()
|
| 214 |
+
# parsed_json = json.loads(answer)
|
| 215 |
+
|
| 216 |
+
# return parsed_json
|
| 217 |
+
# except Exception as e:
|
| 218 |
+
# raise HTTPException(status_code=500, detail=str(e))
|
| 219 |
+
|
| 220 |
+
# if __name__ == "__main__":
|
| 221 |
+
# import uvicorn
|
| 222 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 223 |
+
import json
|
| 224 |
from fastapi import FastAPI, HTTPException
|
| 225 |
from pydantic import BaseModel
|
|
|
|
| 226 |
import openai
|
| 227 |
+
from typing import List, Dict, Any
|
| 228 |
import os
|
| 229 |
|
| 230 |
app = FastAPI()
|
| 231 |
|
| 232 |
+
# Pydantic models for request body
|
| 233 |
+
class StudyInput(BaseModel):
|
| 234 |
overall_study_pattern: str
|
| 235 |
memorization_study_pattern: str
|
| 236 |
problem_solving_study_pattern: str
|
|
|
|
| 239 |
new_topic_approach: str
|
| 240 |
old_topic_approach: str
|
| 241 |
topic_ratio: str
|
| 242 |
+
hours_of_study: int
|
| 243 |
+
hours_of_study_weekends: int
|
| 244 |
revision_days: str
|
| 245 |
test_days: str
|
| 246 |
+
physicsStartIndex: int
|
| 247 |
+
chemistryStartIndex: int
|
| 248 |
+
mathematicsStartIndex: int
|
| 249 |
completed_phy_chapters: List[str]
|
| 250 |
completed_chem_chapters: List[str]
|
| 251 |
completed_maths_chapters: List[str]
|
| 252 |
+
total_time: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
# Function to remove completed chapters
|
| 255 |
def remove_completed_chapters(subject_data, completed_chapters):
|
| 256 |
subject_data["chapters"] = [chapter for chapter in subject_data["chapters"]
|
| 257 |
if chapter["chapter"] not in completed_chapters]
|
| 258 |
return subject_data
|
| 259 |
|
| 260 |
+
# Function to get data at index
|
| 261 |
+
def get_data_at_index(json_data, index):
|
| 262 |
+
if 0 <= index < len(json_data['chapters']):
|
| 263 |
+
return json_data['chapters'][index]
|
| 264 |
+
else:
|
| 265 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
@app.post("/generate_roadmap")
|
| 268 |
+
async def generate_roadmap(study_input: StudyInput):
|
| 269 |
+
# Load JSON data for each subject
|
| 270 |
+
# Note: You'll need to adjust the file paths or include these JSON files in your Docker image
|
| 271 |
+
with open('Physics.json', 'r', encoding='utf-8') as file:
|
| 272 |
+
phy = json.load(file)
|
| 273 |
+
with open('Chemistry.json', 'r', encoding='utf-8') as file:
|
| 274 |
+
chem = json.load(file)
|
| 275 |
+
with open('Maths.json', 'r', encoding='utf-8') as file:
|
| 276 |
+
maths = json.load(file)
|
| 277 |
|
| 278 |
+
# Remove completed chapters
|
| 279 |
+
phy = remove_completed_chapters(phy, study_input.completed_phy_chapters)
|
| 280 |
+
chem = remove_completed_chapters(chem, study_input.completed_chem_chapters)
|
| 281 |
+
maths = remove_completed_chapters(maths, study_input.completed_maths_chapters)
|
| 282 |
|
| 283 |
+
# Get data at specified indices
|
| 284 |
+
phy = get_data_at_index(phy, study_input.physicsStartIndex)
|
| 285 |
+
chem = get_data_at_index(chem, study_input.chemistryStartIndex)
|
| 286 |
+
maths = get_data_at_index(maths, study_input.mathematicsStartIndex)
|
| 287 |
|
| 288 |
+
# Prepare user persona
|
| 289 |
+
user_persona = f"""
|
| 290 |
+
You are required to generate a highly personalized roadmap for a student studying Physics, Chemistry, and Mathematics for the JEE Main exam.
|
| 291 |
+
The roadmap should be tailored based on the following student-specific details:
|
| 292 |
|
| 293 |
+
1. *Study Preferences:*
|
| 294 |
+
- Study Pattern: {study_input.overall_study_pattern}
|
| 295 |
+
- Memorization Approach: {study_input.memorization_study_pattern}
|
| 296 |
+
- Problem-Solving Approach: {study_input.problem_solving_study_pattern}
|
| 297 |
+
- Visualization Approach: {study_input.visualization_study_pattern}
|
| 298 |
+
|
| 299 |
+
2. *Handling Challenges:*
|
| 300 |
+
- If unable to understand a topic: {study_input.obstacle_study_pattern}
|
| 301 |
+
- Approach to New Topics: {study_input.new_topic_approach}
|
| 302 |
+
- Approach to Previously Encountered Topics: {study_input.old_topic_approach}
|
| 303 |
+
|
| 304 |
+
3. *Study Hours:*
|
| 305 |
+
- Weekdays: {study_input.hours_of_study} hours/day
|
| 306 |
+
- Weekends: {study_input.hours_of_study_weekends} hours/day
|
| 307 |
+
- Time Allocation Ratio (Physics:Chemistry:Mathematics): {study_input.topic_ratio}
|
| 308 |
+
- By weekdays I mean day 1, day 2 , day 3 , day 4 , day 5
|
| 309 |
+
- By weekends I mean day 6 , day 7
|
| 310 |
+
"""
|
| 311 |
+
|
| 312 |
+
# Prepare system prompt
|
| 313 |
+
sys_prompt = f"""
|
| 314 |
+
You are required to generate a highly personalized roadmap for a student studying Physics, Chemistry, and Mathematics for the JEE Main exam.
|
| 315 |
+
The roadmap should be tailored based on the following student-specific details:
|
| 316 |
+
|
| 317 |
+
The roadmap must be provided in the following format:
|
| 318 |
+
{{
|
| 319 |
+
"schedule": [
|
| 320 |
+
"days": [
|
| 321 |
+
{{
|
| 322 |
+
"dayNumber": int,
|
| 323 |
+
"subjects": [
|
| 324 |
+
{{
|
| 325 |
+
"name": "string",
|
| 326 |
+
"tasks": [
|
| 327 |
+
{{
|
| 328 |
+
"type": "string",
|
| 329 |
+
"topic": "string",
|
| 330 |
+
"time": "string"
|
| 331 |
+
}}
|
| 332 |
+
]
|
| 333 |
+
}}
|
| 334 |
+
]
|
| 335 |
+
}}
|
| 336 |
+
]
|
| 337 |
+
]
|
| 338 |
+
}}
|
| 339 |
+
|
| 340 |
+
Do not include anything other than the roadmap, and ensure the focus remains strictly on the subjects {phy}, {chem}, and {maths} and associated chapters.
|
| 341 |
+
MAKE SURE THAT YOU MAKE THE ROADMAP FOR ALL THE THREE CHAPTERS EACH OF PHYSICS , CHEMISTRY AND MATHS TO COMPLETE THOSE CHAPTERS WITH 4 ASPECTS i.e "CONCEPT UNDERSTANDING","QUESTION PRACTICE","REVISION","TEST". ALSO INCLUDE TIME FOR EACH TASK THAT YOU GENERATE
|
| 342 |
+
MAKE SURE THAT WE FIRST COMPLETE 1) CONCEPT UNDERSTANDING , 2) QUESTION PRACTICE FOR EVERY SUBTOPIC AND THEN REVISION AND TEST FOR WHOLE CHAPTER TOGETHER.
|
| 343 |
+
MAKE SURE THAT WE INCULDE EACH SUBTOPIC OF EACH CHAPTER FROM {phy},{chem} and {maths} IS FINISHED
|
| 344 |
+
YOU ARE NOT CONSTRAINED TO CREATE A ROADMAP FOR ONLY 'X' NUMBER OF DAYS , YOU CAN EXTEND TILL THE TOPICS ARE FINISHED BUT ONLY STICK TO THE TIMEFRAME ALLOTED FOR EACH SUBJECT AND DO NOT GO ABOVE OR BELOW THAT TIME FRAME.
|
| 345 |
+
"""
|
| 346 |
+
|
| 347 |
+
# Make OpenAI API call
|
| 348 |
+
openai.api_key = os.getenv("KEY") # Replace with your actual API key or use environment variables
|
| 349 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
response = openai.ChatCompletion.create(
|
| 351 |
+
model="gpt-4-0613",
|
| 352 |
messages=[
|
| 353 |
{
|
| 354 |
"role": "system",
|
| 355 |
+
"content": sys_prompt + "MAKE SURE YOU VERY VERY STRUCTLY FOLLOW THE JSON STRUCTURE BECAUSE I WILL PARSE YOUR OUTPUT TO JSON"
|
| 356 |
},
|
| 357 |
{
|
| 358 |
"role": "user",
|
| 359 |
+
"content": user_persona
|
| 360 |
}
|
| 361 |
]
|
| 362 |
)
|
| 363 |
|
| 364 |
answer = response['choices'][0]['message']['content'].strip()
|
| 365 |
|
| 366 |
+
# Second OpenAI API call
|
| 367 |
+
provided_subtopics = list(phy.keys()) + list(chem.keys()) + list(maths.keys())
|
|
|
|
|
|
|
| 368 |
response = openai.ChatCompletion.create(
|
| 369 |
+
model="gpt-4-0613",
|
| 370 |
messages=[
|
| 371 |
{
|
| 372 |
"role": "system",
|
| 373 |
"content": f'''
|
| 374 |
you created a very good roadmap {answer} but you make sure that you dont forget any subtopics from {provided_subtopics}. ensure that the style is same as the previous roadmap.
|
| 375 |
+
MAKE SURE YOU VERY VERY STRUCTLY FOLLOW THE JSON STRUCTURE BECAUSE I WILL PARSE YOUR OUTPUT TO JSON.
|
| 376 |
DO not include json at the top of the answer
|
| 377 |
'''
|
| 378 |
},
|
|
|
|
| 383 |
]
|
| 384 |
)
|
| 385 |
|
| 386 |
+
final_answer = response['choices'][0]['message']['content'].strip()
|
| 387 |
+
parsed_json = json.loads(final_answer)
|
| 388 |
|
| 389 |
return parsed_json
|
| 390 |
except Exception as e:
|
|
|
|
| 392 |
|
| 393 |
if __name__ == "__main__":
|
| 394 |
import uvicorn
|
| 395 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|