-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
143 lines (117 loc) · 4.55 KB
/
app.py
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
# app.py
from flask import Flask, render_template, request, jsonify
import google.generativeai as genai
import json
from fpdf import FPDF
import re
app = Flask(__name__)
# Configure Gemini API
my_api_key="API_key_Goes_here"
genai.configure(api_key=my_api_key)
model = genai.GenerativeModel('models/gemini-1.5-flash')
# Rate limiting variables
from datetime import datetime, timedelta
import threading
class RateLimiter:
def __init__(self):
self.locks = threading.Lock()
self.requests = []
self.daily_requests = 0
self.last_reset = datetime.now()
def can_make_request(self):
now = datetime.now()
# Reset daily counter if it's a new day
if now.date() > self.last_reset.date():
with self.locks:
self.daily_requests = 0
self.last_reset = now
# Clean old requests
minute_ago = now - timedelta(minutes=1)
self.requests = [req for req in self.requests if req > minute_ago]
# Check limits
if (len(self.requests) >= 15 or # 15 RPM
self.daily_requests >= 1500): # 1,500 RPD
return False
return True
def add_request(self):
now = datetime.now()
with self.locks:
self.requests.append(now)
self.daily_requests += 1
rate_limiter = RateLimiter()
def extract_json_from_response(text):
"""Extract JSON from the response text, handling potential formatting issues."""
try:
# First try direct JSON parsing
return json.loads(text)
except json.JSONDecodeError:
try:
# Try to find JSON-like structure in the text
json_match = re.search(r'\{[\s\S]*\}', text)
if json_match:
return json.loads(json_match.group())
# If no JSON structure found, create a structured response
return format_unstructured_response(text)
except Exception:
return format_unstructured_response(text)
def format_unstructured_response(text):
"""Format unstructured text into our expected JSON structure."""
return {
"professions": [
{
"name": "Career Option",
"requiredSkills": ["Based on provided information"],
"careerPath": ["Please try again with more specific information"],
"salaryRange": "Varies",
"marketStats": "Data unavailable",
"successStory": "Please try again"
}
]
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/analyze', methods=['POST'])
def analyze_profile():
if not rate_limiter.can_make_request():
return jsonify({'error': 'Rate limit exceeded. Please try again later.'}), 429
data = request.json
# Structured prompt for Gemini
prompt = f"""
Act as a career counselor analyzing a student's profile. Based on the following information, suggest suitable career paths:
Goals: {data['goals']}
Interests: {data['interests']}
Current Skills: {data['currentSkills']}
Provide a detailed analysis in the following JSON format exactly:
{{
"professions": [
{{
"name": "Profession Name",
"requiredSkills": ["skill1", "skill2", "skill3"],
"careerPath": ["10th Grade - Focus on relevant subjects", "12th Grade - Choose appropriate stream", "Bachelor's Degree details", "Master's/Additional qualifications", "Entry-level position", "Career progression"],
"salaryRange": "Entry level to Senior level salary range in INR",
"marketStats": "Current job market statistics and future outlook",
"successStory": "A brief success story in this field"
}}
]
}}
Important:
1. Provide exactly 10 professions
2. Ensure all JSON fields are present for each profession
3. Make sure the response is properly formatted JSON
4. Be specific and detailed in each field
"""
rate_limiter.add_request()
try:
response = model.generate_content(prompt)
# Extract and parse the response
career_data = extract_json_from_response(response.text)
return jsonify(career_data)
except Exception as e:
print(f"Error processing request: {str(e)}")
return jsonify({
'error': 'An error occurred while processing your request. Please try again.',
'details': str(e)
}), 500
if __name__ == '__main__':
app.run(debug=True)