File size: 5,119 Bytes
16f75b9
 
 
 
61ff2b8
16f75b9
61ff2b8
16f75b9
 
 
61ff2b8
16f75b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5759067
61ff2b8
 
16f75b9
61ff2b8
 
 
16f75b9
 
61ff2b8
16f75b9
 
 
61ff2b8
16f75b9
61ff2b8
 
16f75b9
61ff2b8
 
 
 
 
 
 
 
 
 
 
 
16f75b9
61ff2b8
16f75b9
61ff2b8
16f75b9
61ff2b8
 
 
16f75b9
61ff2b8
 
 
 
 
 
 
 
 
16f75b9
 
 
 
61ff2b8
16f75b9
61ff2b8
 
 
 
 
16f75b9
 
61ff2b8
 
 
 
 
 
 
 
 
 
 
 
 
16f75b9
61ff2b8
 
 
 
 
 
 
16f75b9
61ff2b8
 
 
 
16f75b9
61ff2b8
 
 
 
 
 
 
 
 
16f75b9
61ff2b8
 
 
 
 
 
 
 
 
16f75b9
075d6ff
16f75b9
 
 
61ff2b8
16f75b9
 
 
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
import json
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import openai
import asyncio
import os
from typing import List

app = FastAPI()

# Pydantic model for input
class StudyInput(BaseModel):
    overall_study_pattern: str
    memorization_study_pattern: str
    problem_solving_study_pattern: str
    visualization_study_pattern: str
    obstacle_study_pattern: str
    new_topic_approach: str
    old_topic_approach: str
    topic_ratio: str
    hours_of_study: str
    hours_of_study_weekends: str
    revision_days: str
    test_days: str
    physicsStartIndex: int
    chemistryStartIndex: int
    mathematicsStartIndex: int
    completed_phy_chapters: List[str]
    completed_chem_chapters: List[str]
    completed_maths_chapters: List[str]
    currentDate: str

# Utility function to remove completed chapters
def remove_completed_chapters(subject_data, completed_chapters):
    subject_data["chapters"] = [
        chapter for chapter in subject_data["chapters"] if chapter["chapter"] not in completed_chapters
    ]
    return subject_data

# Utility function to get chapter at a specific index
def get_data_at_index(json_data, index):
    if 0 <= index < len(json_data['chapters']):
        return json_data['chapters'][index]
    return {}

# Agent to generate a roadmap for a subject
async def generate_subject_roadmap(subject_name, subject_data, study_input):
    user_persona = f"""
    You are generating a JEE roadmap for {subject_name}.
    Student Preferences:
    - Study Pattern: {study_input.overall_study_pattern}
    - Memorization: {study_input.memorization_study_pattern}
    - Problem-Solving: {study_input.problem_solving_study_pattern}
    - Visualization: {study_input.visualization_study_pattern}
    - New Topics: {study_input.new_topic_approach}
    - Old Topics: {study_input.old_topic_approach}
    - Study Hours (Weekdays): {study_input.hours_of_study}
    - Study Hours (Weekends): {study_input.hours_of_study_weekends}
    - Revision Days: {study_input.revision_days}
    - Test Days: {study_input.test_days}
    """

    output_structure = """{
      "schedule": [
        {
          "dayNumber": int,
          "date": YYYY-MM-DD,
          "subjects": [
            {
              "name": "string",
              "tasks": [
                {
                  "ChapterName": "string",
                  "type": "string",
                  "topic": "string",
                  "time": "string"
                }
              ]
            }
          ]
        }
      ]
    }"""

    system_prompt = f"""
    Generate a structured roadmap for {subject_name} using the following data: {subject_data}.
    The roadmap must include Concept Learning, Question Practice, Revision, and Tests.
    Stick to the time allocations and ensure the JSON format follows:
    {output_structure}
    """

    openai.api_key = os.getenv("KEY")
    response = await openai.ChatCompletion.acreate(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_persona}
        ]
    )
    return json.loads(response["choices"][0]["message"]["content"])

# API endpoint for roadmap generation
@app.post("/generate_roadmap")
async def generate_roadmap(study_input: StudyInput):
    try:
        # Load JSON data for each subject
        with open('Physics.json', 'r', encoding='utf-8') as file:
            phy = json.load(file)
        with open('Chemistry.json', 'r', encoding='utf-8') as file:
            chem = json.load(file)
        with open('Maths.json', 'r', encoding='utf-8') as file:
            maths = json.load(file)

        # Remove completed chapters
        phy = remove_completed_chapters(phy, study_input.completed_phy_chapters)
        chem = remove_completed_chapters(chem, study_input.completed_chem_chapters)
        maths = remove_completed_chapters(maths, study_input.completed_maths_chapters)

        # Get the chapters at the given index
        phy = get_data_at_index(phy, study_input.physicsStartIndex)
        chem = get_data_at_index(chem, study_input.chemistryStartIndex)
        maths = get_data_at_index(maths, study_input.mathematicsStartIndex)

        # Run agents in parallel
        phy_task = asyncio.create_task(generate_subject_roadmap("Physics", phy, study_input))
        chem_task = asyncio.create_task(generate_subject_roadmap("Chemistry", chem, study_input))
        maths_task = asyncio.create_task(generate_subject_roadmap("Maths", maths, study_input))

        # Collect results
        physics_roadmap, chemistry_roadmap, maths_roadmap = await asyncio.gather(phy_task, chem_task, maths_task)

        # Combine results
        final_roadmap = {
            "Physics": physics_roadmap,
            "Chemistry": chemistry_roadmap,
            "Maths": maths_roadmap
        }

        return json.dumps(final_roadmap, indent=4)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run FastAPI
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)