career_bot / app.py
transformer112's picture
Update app.py
aac0d43 verified
Raw
History Blame Contribute Delete
11.5 kB
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import time
from threading import Lock
import os
# Global variables for model caching
model = None
tokenizer = None
model_lock = Lock()
def load_model():
"""Load the trained model using standard transformers (CPU compatible)"""
global model, tokenizer
with model_lock:
if model is None:
try:
print("πŸ”„ Loading Career Guidance AI model...")
model_path = "./gemma_career_final"
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Add pad token if missing
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Load model for CPU inference
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float32, # Use float32 for CPU
device_map="cpu", # Force CPU
low_cpu_mem_usage=True, # Optimize CPU memory
trust_remote_code=True # Trust model code
)
# Set to evaluation mode
model.eval()
print("βœ… Model loaded successfully on CPU!")
return True
except Exception as e:
print(f"❌ Error loading model: {str(e)}")
print("πŸ“ Trying fallback loading method...")
try:
# Fallback: Load base model if fine-tuned model fails
base_model = "google/gemma-2-2b-it"
print(f"πŸ”„ Loading base model: {base_model}")
tokenizer = AutoTokenizer.from_pretrained(base_model)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.float32,
device_map="cpu",
low_cpu_mem_usage=True
)
model.eval()
print("βœ… Base model loaded successfully!")
return True
except Exception as fallback_error:
print(f"❌ Fallback loading failed: {str(fallback_error)}")
return False
return True
def generate_career_response(message, history):
"""Generate career guidance response using transformers"""
# Load model if not loaded
if not load_model():
return "❌ I'm having trouble loading. Please refresh and try again."
# Handle empty messages
if not message.strip():
return "Please ask me a career-related question! I'm here to help with career planning, job search, interviews, skills, and professional development."
try:
# Format the conversation prompt for Gemma
prompt = f"""<start_of_turn>user
{message}<end_of_turn>
<start_of_turn>model
"""
# Tokenize input
inputs = tokenizer(
prompt,
return_tensors="pt",
max_length=1024, # Limit input length for CPU
truncation=True,
padding=True
)
# Generate response with CPU-optimized settings
with torch.no_grad():
outputs = model.generate(
inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=200, # Shorter for faster CPU inference
temperature=0.7,
do_sample=True,
top_p=0.9,
top_k=50,
repetition_penalty=1.1,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3 # Reduce repetition
)
# Decode response
response = tokenizer.decode(outputs[0], skip_special_tokens=False)
# Extract model response
if "<start_of_turn>model" in response:
response = response.split("<start_of_turn>model")[-1]
if "<end_of_turn>" in response:
response = response.split("<end_of_turn>")[0]
response = response.strip()
# Fallback responses for common issues
if not response or len(response.split()) < 5:
if "career" in message.lower() or "job" in message.lower():
response = "I'd be happy to help with your career question! Could you provide more specific details about what aspect of your career you'd like guidance on?"
else:
response = "I specialize in career guidance and professional development. I can help with career planning, job search strategies, interview preparation, skill development, and professional growth. How can I assist with your career goals?"
return response
except Exception as e:
print(f"πŸ’₯ Generation error: {str(e)}")
# Provide helpful fallback response based on query type
career_keywords = ["career", "job", "interview", "resume", "skill", "work", "salary", "promotion"]
if any(keyword in message.lower() for keyword in career_keywords):
return """I understand you're looking for career guidance. While I'm experiencing some technical difficulties with my AI processing, here are some general tips:
**For Career Planning:**
- Identify your strengths and interests
- Research industry trends and requirements
- Network with professionals in your field
- Consider additional training or certifications
**For Job Search:**
- Tailor your resume to each position
- Practice common interview questions
- Build a strong LinkedIn profile
- Apply consistently and follow up professionally
Would you like to try rephrasing your question? I'll do my best to provide helpful career advice!"""
else:
return "I'm a career guidance assistant. I can help with career planning, job interviews, skill development, and professional growth. What career-related question can I help you with?"
# Enhanced CSS for professional appearance
css = """
#chatbot {
height: 650px !important;
}
.gradio-container {
max-width: 900px !important;
margin: auto !important;
}
.message.user {
background-color: #f0f8ff !important;
border-left: 4px solid #007bff !important;
padding-left: 15px !important;
margin: 10px 0 !important;
}
.message.bot {
background-color: #f8f9fa !important;
border-left: 4px solid #28a745 !important;
padding-left: 15px !important;
margin: 10px 0 !important;
}
.gradio-container .wrap {
max-width: 100% !important;
}
#component-0 {
max-height: none !important;
}
"""
# Career guidance examples
examples = [
["What skills do I need to become a data scientist?"],
["How should I prepare for a software engineering interview?"],
["What's the best career path for someone interested in AI?"],
["How do I transition from marketing to product management?"],
["What certifications are valuable for cybersecurity careers?"],
["How do I negotiate salary in my first job?"],
["What should I include in my LinkedIn profile?"],
["How do I network effectively in my industry?"]
]
# Create the Gradio interface
with gr.Blocks(css=css, title="Career Guidance AI Assistant", theme=gr.themes.Soft()) as demo:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1 style="color: #007bff; margin-bottom: 10px;">πŸš€ Career Guidance AI Assistant</h1>
<p style="font-size: 18px; color: #666; max-width: 700px; margin: 0 auto;">
Your personal AI career advisor, ready to help with career planning, job search strategies,
interview preparation, and professional development guidance.
</p>
</div>
""")
with gr.Row():
with gr.Column():
gr.Markdown("""
### πŸ’Ό I can help you with:
- **Career Planning** & goal setting
- **Job Search** strategies & tips
- **Interview Preparation** & practice
- **Skill Development** recommendations
- **Resume & LinkedIn** optimization
- **Salary Negotiation** guidance
- **Career Transitions** & pivots
- **Professional Networking** strategies
""")
# Main chat interface
chatbot = gr.ChatInterface(
generate_career_response,
chatbot=gr.Chatbot(
elem_id="chatbot",
height=600,
show_label=True,
show_copy_button=True,
bubble_full_width=False,
avatar_images=("πŸ‘¨β€πŸ’Ό", "πŸ€–"),
show_share_button=False
),
textbox=gr.Textbox(
placeholder="πŸ’¬ Ask me anything about careers, jobs, interviews, skills, or professional development...",
container=False,
scale=7,
max_lines=3
),
title=None, # Already added above
retry_btn="πŸ”„ Try Again",
undo_btn="↩️ Undo Last",
clear_btn="πŸ—‘οΈ Clear Chat",
submit_btn="Send πŸ“€"
)
# Example questions section
with gr.Row():
with gr.Column():
gr.Markdown("### πŸ’‘ Try these example questions:")
with gr.Row():
for i in range(0, len(examples), 2):
with gr.Column():
if i < len(examples):
gr.Examples(
examples=[examples[i]],
inputs=chatbot.textbox,
label=None
)
if i + 1 < len(examples):
gr.Examples(
examples=[examples[i + 1]],
inputs=chatbot.textbox,
label=None
)
# Footer section
gr.HTML("""
<div style="margin-top: 40px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;">
<div style="text-align: center;">
<h3 style="color: #007bff; margin-bottom: 15px;">πŸ“‹ Important Notes</h3>
<div style="display: flex; justify-content: space-around; flex-wrap: wrap; gap: 20px;">
<div>
<strong>πŸ”’ Privacy</strong><br>
<small>Conversations are not stored</small>
</div>
<div>
<strong>⚑ Response Time</strong><br>
<small>~10-30 seconds per query</small>
</div>
<div>
<strong>🎯 Specialization</strong><br>
<small>Career guidance & professional development</small>
</div>
<div>
<strong>πŸ“ Disclaimer</strong><br>
<small>General guidance - consult professionals for specific advice</small>
</div>
</div>
</div>
</div>
""")
# Launch configuration
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True,
share=False
)