Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +19 -28
src/app.py
CHANGED
|
@@ -60,51 +60,35 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 60 |
from reportlab.lib.units import inch
|
| 61 |
|
| 62 |
# Claude Chatbot Class
|
|
|
|
|
|
|
| 63 |
class ClaudeChatbot:
|
| 64 |
def __init__(self):
|
| 65 |
self.api_key = os.getenv('OPENROUTER_API_KEY')
|
| 66 |
self.base_url = "https://openrouter.ai/api/v1/chat/completions"
|
| 67 |
|
| 68 |
-
|
| 69 |
-
self.model = "meta-llama/llama-3.2-3b-instruct:free"
|
| 70 |
|
| 71 |
if not self.api_key:
|
| 72 |
st.error("β OPENROUTER_API_KEY not found in environment variables!")
|
| 73 |
st.info("Please set your OpenRouter API key in the environment variables.")
|
| 74 |
|
| 75 |
def generate_response(self, prompt, context="", max_tokens=1500):
|
| 76 |
-
"""Generate response using free models via OpenRouter
|
| 77 |
if not self.api_key:
|
| 78 |
return "Error: API key not configured"
|
| 79 |
|
| 80 |
headers = {
|
| 81 |
"Authorization": f"Bearer {self.api_key}",
|
| 82 |
"Content-Type": "application/json",
|
| 83 |
-
"HTTP-Referer": "https://your-app-url.com",
|
| 84 |
"X-Title": "AI Resume Analyzer"
|
| 85 |
}
|
| 86 |
|
| 87 |
-
|
| 88 |
-
system_prompt = """You are an expert resume and career consultant with deep knowledge of hiring practices, ATS systems, and industry requirements.
|
| 89 |
-
|
| 90 |
-
IMPORTANT GUIDELINES:
|
| 91 |
-
- Provide specific, actionable advice for resume improvement
|
| 92 |
-
- Keep responses focused on resume optimization and career advice
|
| 93 |
-
- Be encouraging but honest in feedback
|
| 94 |
-
- Limit responses to resume-related topics only
|
| 95 |
-
- If asked about non-resume topics, politely redirect to resume discussion
|
| 96 |
-
- Provide concrete examples when suggesting improvements
|
| 97 |
-
- Consider ATS compatibility in all recommendations
|
| 98 |
-
- Keep responses concise and practical"""
|
| 99 |
|
| 100 |
if context:
|
| 101 |
-
system_prompt += f"\n\
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# Check if prompt is resume-related
|
| 105 |
-
resume_keywords = ['resume', 'cv', 'job', 'career', 'skills', 'experience', 'education', 'work', 'interview', 'hiring', 'ats', 'application', 'improve', 'better', 'add', 'remove', 'section', 'format']
|
| 106 |
-
if not any(keyword in prompt.lower() for keyword in resume_keywords):
|
| 107 |
-
return "I'm specialized in resume analysis and career advice. Please ask me questions related to your resume, job applications, career development, or interview preparation."
|
| 108 |
|
| 109 |
data = {
|
| 110 |
"model": self.model,
|
|
@@ -119,6 +103,12 @@ class ClaudeChatbot:
|
|
| 119 |
try:
|
| 120 |
response = requests.post(self.base_url, headers=headers, json=data, timeout=30)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Handle payment required error
|
| 123 |
if response.status_code == 402:
|
| 124 |
return "Error: This model requires payment. Please switch to a free model or add credits to your OpenRouter account."
|
|
@@ -135,7 +125,7 @@ class ClaudeChatbot:
|
|
| 135 |
if e.response.status_code == 402:
|
| 136 |
return "Error: Payment required. This model is not free. Please use a free model or add credits."
|
| 137 |
elif e.response.status_code == 429:
|
| 138 |
-
return "Error:
|
| 139 |
else:
|
| 140 |
return f"HTTP Error: {e.response.status_code} - {str(e)}"
|
| 141 |
except requests.exceptions.RequestException as e:
|
|
@@ -160,6 +150,7 @@ class ClaudeChatbot:
|
|
| 160 |
self.model = model_name
|
| 161 |
st.success(f"Switched to model: {model_name}")
|
| 162 |
|
|
|
|
| 163 |
# Download NLTK data if not already present
|
| 164 |
@st.cache_resource
|
| 165 |
def download_nltk_data():
|
|
@@ -593,12 +584,12 @@ class ResumeAnalyzer:
|
|
| 593 |
|
| 594 |
def main():
|
| 595 |
st.set_page_config(
|
| 596 |
-
page_title="AI Resume Analyzer with
|
| 597 |
page_icon="π",
|
| 598 |
layout="wide"
|
| 599 |
)
|
| 600 |
|
| 601 |
-
st.title("π AI-Powered Resume Analyzer with
|
| 602 |
st.markdown("Upload your resume and get comprehensive analysis with AI-powered insights!")
|
| 603 |
|
| 604 |
# Show dependency and API status
|
|
@@ -624,9 +615,9 @@ def main():
|
|
| 624 |
|
| 625 |
with col4:
|
| 626 |
if os.getenv('OPENROUTER_API_KEY'):
|
| 627 |
-
st.success("β
|
| 628 |
else:
|
| 629 |
-
st.error("β
|
| 630 |
|
| 631 |
# Initialize analyzer
|
| 632 |
try:
|
|
|
|
| 60 |
from reportlab.lib.units import inch
|
| 61 |
|
| 62 |
# Claude Chatbot Class
|
| 63 |
+
import time
|
| 64 |
+
|
| 65 |
class ClaudeChatbot:
|
| 66 |
def __init__(self):
|
| 67 |
self.api_key = os.getenv('OPENROUTER_API_KEY')
|
| 68 |
self.base_url = "https://openrouter.ai/api/v1/chat/completions"
|
| 69 |
|
| 70 |
+
self.model = "meta-llama/llama-3.2-3b-instruct:free"
|
|
|
|
| 71 |
|
| 72 |
if not self.api_key:
|
| 73 |
st.error("β OPENROUTER_API_KEY not found in environment variables!")
|
| 74 |
st.info("Please set your OpenRouter API key in the environment variables.")
|
| 75 |
|
| 76 |
def generate_response(self, prompt, context="", max_tokens=1500):
|
| 77 |
+
"""Generate response using free models via OpenRouter"""
|
| 78 |
if not self.api_key:
|
| 79 |
return "Error: API key not configured"
|
| 80 |
|
| 81 |
headers = {
|
| 82 |
"Authorization": f"Bearer {self.api_key}",
|
| 83 |
"Content-Type": "application/json",
|
| 84 |
+
"HTTP-Referer": "https://your-app-url.com", # Replace with your actual URL
|
| 85 |
"X-Title": "AI Resume Analyzer"
|
| 86 |
}
|
| 87 |
|
| 88 |
+
system_prompt = """You are an expert resume and career consultant with deep knowledge of hiring practices, ATS systems, and industry requirements. You provide actionable, specific advice to help job seekers improve their resumes and career prospects. Always be encouraging but honest in your feedback."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if context:
|
| 91 |
+
system_prompt += f"\n\nContext about the user's resume:\n{context[:2000]}" # Limit context to avoid token limits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
data = {
|
| 94 |
"model": self.model,
|
|
|
|
| 103 |
try:
|
| 104 |
response = requests.post(self.base_url, headers=headers, json=data, timeout=30)
|
| 105 |
|
| 106 |
+
# Handle rate limiting
|
| 107 |
+
if response.status_code == 429:
|
| 108 |
+
st.warning("Rate limit hit. Waiting 10 seconds...")
|
| 109 |
+
time.sleep(10)
|
| 110 |
+
response = requests.post(self.base_url, headers=headers, json=data, timeout=30)
|
| 111 |
+
|
| 112 |
# Handle payment required error
|
| 113 |
if response.status_code == 402:
|
| 114 |
return "Error: This model requires payment. Please switch to a free model or add credits to your OpenRouter account."
|
|
|
|
| 125 |
if e.response.status_code == 402:
|
| 126 |
return "Error: Payment required. This model is not free. Please use a free model or add credits."
|
| 127 |
elif e.response.status_code == 429:
|
| 128 |
+
return "Error: Rate limit exceeded. Please try again later."
|
| 129 |
else:
|
| 130 |
return f"HTTP Error: {e.response.status_code} - {str(e)}"
|
| 131 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 150 |
self.model = model_name
|
| 151 |
st.success(f"Switched to model: {model_name}")
|
| 152 |
|
| 153 |
+
|
| 154 |
# Download NLTK data if not already present
|
| 155 |
@st.cache_resource
|
| 156 |
def download_nltk_data():
|
|
|
|
| 584 |
|
| 585 |
def main():
|
| 586 |
st.set_page_config(
|
| 587 |
+
page_title="AI Resume Analyzer with Chatbot",
|
| 588 |
page_icon="π",
|
| 589 |
layout="wide"
|
| 590 |
)
|
| 591 |
|
| 592 |
+
st.title("π AI-Powered Resume Analyzer with Chatbot Assistant")
|
| 593 |
st.markdown("Upload your resume and get comprehensive analysis with AI-powered insights!")
|
| 594 |
|
| 595 |
# Show dependency and API status
|
|
|
|
| 615 |
|
| 616 |
with col4:
|
| 617 |
if os.getenv('OPENROUTER_API_KEY'):
|
| 618 |
+
st.success("β
Chatbot API Available")
|
| 619 |
else:
|
| 620 |
+
st.error("β Chatbot API Not Available")
|
| 621 |
|
| 622 |
# Initialize analyzer
|
| 623 |
try:
|