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"""user {message} 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 "model" in response: response = response.split("model")[-1] if "" in response: response = response.split("")[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("""

🚀 Career Guidance AI Assistant

Your personal AI career advisor, ready to help with career planning, job search strategies, interview preparation, and professional development guidance.

""") 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("""

📋 Important Notes

🔒 Privacy
Conversations are not stored
⚡ Response Time
~10-30 seconds per query
đŸŽ¯ Specialization
Career guidance & professional development
📝 Disclaimer
General guidance - consult professionals for specific advice
""") # Launch configuration if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, show_error=True, share=False )