Rakshitjan commited on
Commit
73bfdb1
·
verified ·
1 Parent(s): 8624bd7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +102 -94
main.py CHANGED
@@ -1,95 +1,103 @@
1
- # main.py
2
- from fastapi import FastAPI, HTTPException
3
- from pydantic import BaseModel
4
- from typing import Dict
5
- import os
6
- from groq import Groq
7
-
8
- app = FastAPI()
9
-
10
- # Pydantic model for request
11
- class ScoreInput(BaseModel):
12
- score_percentages: Dict[str, float]
13
- time_percentages: Dict[str, float]
14
-
15
- # Helper functions
16
- def get_final_score(score_percentages: Dict[str, float], time_percentages: Dict[str, float]) -> Dict[str, float]:
17
- final_score = {}
18
- for skill in score_percentages:
19
- score_avg = (score_percentages[skill] + time_percentages[skill]) / 2
20
- final_score[skill] = score_avg
21
- return final_score
22
-
23
- def get_strengths_and_weaknesses(final_score: Dict[str, float]):
24
- sorted_skills = sorted(
25
- [(skill, score) for skill, score in final_score.items()],
26
- key=lambda item: item[1],
27
- reverse=True
28
- )
29
- num_skills = len(sorted_skills)
30
- if num_skills == 0:
31
- return [], [], []
32
-
33
- split1 = num_skills // 3
34
- split2 = 2 * (num_skills // 3)
35
-
36
- strengths = sorted_skills[:split1]
37
- opportunities = sorted_skills[split1:split2]
38
- challenges = sorted_skills[split2:]
39
-
40
- return strengths, opportunities, challenges
41
-
42
- # FastAPI route
43
- @app.post("/analyze")
44
- async def analyze_scores(input_data: ScoreInput):
45
- final_score = get_final_score(input_data.score_percentages, input_data.time_percentages)
46
- strengths, opportunities, challenges = get_strengths_and_weaknesses(final_score)
47
-
48
- # Groq API call
49
- api_key = os.getenv("GROQ_API_KEY")
50
- if not api_key:
51
- raise HTTPException(status_code=500, detail="Groq API key not found")
52
-
53
- client = Groq(api_key=api_key)
54
- sys_prompt = f"""You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
55
- You have been provided with the strengths {strengths}, Opportunities {opportunities} and Challenges {challenges} skills of the user
56
- Output Structure:
57
- SCO Analysis:
58
- Strengths:
59
- - List the student's strengths based on their {strengths} skills
60
- - Let the student now how they can use these strengths in their JEE preparation and exam to improve their score.
61
- - Also tell them how do they improve their score more.
62
- Opportunities:
63
- - List the student's strengths based on their {opportunities} skills
64
- - Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
65
- - Recommend ways to enhance their score in the {opportunities} skills.
66
- - Also tell them if they improve in these skills what opportunities they have in improving their scores
67
- Challenges:
68
- - List the student's strengths based on their {challenges} skills
69
- - Guide the student that these skills are basically the core area where they are lacking
70
- - Tell them that if they continue not focusing upon them they might get far away from their JEE goal.
71
- Action Plan:
72
- - Provide a detailed plan to the student to improve in the {challenges} skills.
73
- - Recommend targeted strategies, resources, and techniques to improve their {challenges} skills.
74
- - Let them know if they improve these areas how it can help boost their scores and make their preparation more effective.
75
- - Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
76
-
77
- Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics."""
78
-
79
- try:
80
- chat_completion = client.chat.completions.create(
81
- messages=[
82
- {"role": "system", "content": sys_prompt},
83
- {"role": "user", "content": f"Generate the SOCA analysis based on the system prompt and {strengths}, {opportunities} and {challenges}. MAKE SURE WE STRICTLY FOLLOW THE STRUCTURE."},
84
- ],
85
- model="llama3-70b-8192",
86
- )
87
- analysis = chat_completion.choices[0].message.content
88
- except Exception as e:
89
- raise HTTPException(status_code=500, detail=f"Error calling Groq API: {str(e)}")
90
-
91
- return {"analysis": analysis}
92
-
93
- if __name__ == "__main__":
94
- import uvicorn
 
 
 
 
 
 
 
 
95
  uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ # main.py
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel
4
+ from typing import Dict
5
+ import os
6
+ from groq import Groq
7
+
8
+ app = FastAPI()
9
+
10
+ # Pydantic model for request
11
+ class ScoreInput(BaseModel):
12
+ score_percentages: Dict[str, float]
13
+ time_percentages: Dict[str, float]
14
+
15
+ # Helper functions
16
+ def get_final_score(score_percentages: Dict[str, float], time_percentages: Dict[str, float]) -> Dict[str, float]:
17
+ final_score = {}
18
+ for skill in score_percentages:
19
+ score_avg = (score_percentages[skill] + time_percentages[skill]) / 2
20
+ final_score[skill] = score_avg
21
+ return final_score
22
+
23
+ def get_strengths_and_weaknesses(final_score: Dict[str, float]):
24
+ sorted_skills = sorted(
25
+ [(skill, score) for skill, score in final_score.items()],
26
+ key=lambda item: item[1],
27
+ reverse=True
28
+ )
29
+ num_skills = len(sorted_skills)
30
+ if num_skills == 0:
31
+ return [], [], []
32
+
33
+ split1 = num_skills // 3
34
+ split2 = 2 * (num_skills // 3)
35
+
36
+ strengths = sorted_skills[:split1]
37
+ opportunities = sorted_skills[split1:split2]
38
+ challenges = sorted_skills[split2:]
39
+
40
+ return strengths, opportunities, challenges
41
+
42
+ # FastAPI route
43
+ @app.post("/analyze")
44
+ async def analyze_scores(input_data: ScoreInput):
45
+ final_score = get_final_score(input_data.score_percentages, input_data.time_percentages)
46
+ strengths, opportunities, challenges = get_strengths_and_weaknesses(final_score)
47
+
48
+ # Groq API call
49
+ api_key = os.getenv("GROQ_API_KEY")
50
+ if not api_key:
51
+ raise HTTPException(status_code=500, detail="Groq API key not found")
52
+
53
+ client = Groq(api_key=api_key)
54
+ sys_prompt = f"""You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
55
+ You have been provided with the strengths {strengths}, Opportunities {opportunities} and Challenges {challenges} skills of the user
56
+ Output Structure:
57
+ SCO Analysis:
58
+ Strengths:
59
+ - List the student's strengths based on their {strengths} skills
60
+ - Let the student now how they can use these strengths in their JEE preparation and exam to improve their score.
61
+ - Also tell them how do they improve their score more.
62
+ Opportunities:
63
+ - List the student's strengths based on their {opportunities} skills
64
+ - Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
65
+ - Recommend ways to enhance their score in the {opportunities} skills.
66
+ - Also tell them if they improve in these skills what opportunities they have in improving their scores
67
+ Challenges:
68
+ - List the student's strengths based on their {challenges} skills
69
+ - Guide the student that these skills are basically the core area where they are lacking
70
+ - Tell them that if they continue not focusing upon them they might get far away from their JEE goal.
71
+ Action Plan:
72
+ - Provide a detailed plan to the student to improve in the {challenges} skills.
73
+ - Recommend targeted strategies, resources, and techniques to improve their {challenges} skills.
74
+ - Let them know if they improve these areas how it can help boost their scores and make their preparation more effective.
75
+ - Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
76
+
77
+ Things that LLM need to make sure:
78
+ 1) Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics.
79
+ 2) Make sure you give very much visually appealing output , with symbols and logos.
80
+ 3) Make sure you give out output in bullet points.
81
+ 4) While entering a new line in the output use "\n" new line character.
82
+ 5) Make the output very much JEE (Joint Entrance Examination) based and give everything with context to Physics , Chemistry and Maths JEE syllabus.
83
+ 6) Use Italics, Bold and underline appropriately to improve the output more.
84
+ """
85
+
86
+
87
+ try:
88
+ chat_completion = client.chat.completions.create(
89
+ messages=[
90
+ {"role": "system", "content": sys_prompt},
91
+ {"role": "user", "content": f"Generate the SOCA analysis based on the system prompt and {strengths}, {opportunities} and {challenges}. MAKE SURE WE STRICTLY FOLLOW THE STRUCTURE."},
92
+ ],
93
+ model="llama3-70b-8192",
94
+ )
95
+ analysis = chat_completion.choices[0].message.content
96
+ except Exception as e:
97
+ raise HTTPException(status_code=500, detail=f"Error calling Groq API: {str(e)}")
98
+
99
+ return {"analysis": analysis}
100
+
101
+ if __name__ == "__main__":
102
+ import uvicorn
103
  uvicorn.run(app, host="0.0.0.0", port=8000)