import os import gradio as gr from openai import OpenAI # Initialize OpenAI client client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # Define the response function with streaming def respond( message, history: list[tuple[str, str]], system_message, field_of_interest, dream_job, current_qualifications, likes, skills, ask_for_path_suggestions, max_tokens, temperature, top_p, ): # Build system message enhanced_system_message = ( f"{system_message}\n\n" f"Field of Interest: {field_of_interest}\n" f"Dream Job: {dream_job}\n" f"Current Qualifications: {current_qualifications}\n" f"Likes and Interests: {likes}\n" f"Skills: {skills}\n" ) if ask_for_path_suggestions: enhanced_system_message += " The user would also like suggestions for potential career paths based on their background and interests." # Build chat messages messages = [{"role": "system", "content": enhanced_system_message}] for user_msg, assistant_msg in history: if user_msg: messages.append({"role": "user", "content": user_msg}) if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) # Stream response response_text = "" try: response = client.chat.completions.create( model="gpt-4o-mini", # Use the appropriate model name messages=messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, stream=True, ) for chunk in response: if chunk.choices and chunk.choices[0].delta.content: token = chunk.choices[0].delta.content response_text += token yield response_text except Exception as e: yield f"❌ An error occurred: {str(e)}" # Gradio Interface for Career Coach demo = gr.ChatInterface( fn=respond, additional_inputs=[ gr.Textbox( label="Instructions to Bot", value="You are a friendly and insightful AI career coach. You help users discover potential career paths, suggest steps to reach their dream jobs, and offer advice based on their interests, qualifications, and skills.", lines=3, ), gr.Textbox(label="Field of Interest", placeholder="e.g., technology, healthcare, trades, education..."), gr.Textbox(label="Dream Job", placeholder="e.g., software developer, nurse, plumber, teacher..."), gr.Textbox(label="Current Qualifications", placeholder="e.g., high school diploma, college degree, self-taught..."), gr.Textbox(label="Likes & Interests", placeholder="e.g., helping others, solving puzzles, building things..."), gr.Textbox(label="Skills", placeholder="e.g., writing, coding, teamwork, analysis..."), gr.Checkbox(label="Ask for Career Path Suggestions", value=True), gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), ], title="Career Coach – Explore Your Path!", description=( "This app helps you explore your career path and plan how to reach your dream job. " "Click on the arrow > next to the Additional Inputs Line, provide details about your background, interests, skills and career goals, then enter a message to ask the assistant for help. " "Powered by OpenAI GPT-4o. Developed by wn. Disclaimer: AI may make mistakes. Use with caution." ), ) if __name__ == "__main__": demo.launch()