Spaces:
Running
Running
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # Initialize Hugging Face Inference Client | |
| HF_API_KEY = os.environ.get("HF_API_KEY") | |
| HF_MODEL_NAME = os.environ.get("HF_MODEL_NAME") | |
| client = InferenceClient(model=HF_MODEL_NAME, token=HF_API_KEY) | |
| # Streaming career coach logic | |
| 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, | |
| ): | |
| # Construct system message using inputs | |
| 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." | |
| messages = [{"role": "system", "content": enhanced_system_message}] | |
| # Add chat history | |
| 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}) | |
| # Generate the streamed response | |
| response = "" | |
| for msg in client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True, | |
| ): | |
| token = msg.choices[0].delta.content | |
| if token: | |
| response += token | |
| yield response | |
| # 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. " | |
| "Provide details about your background, interests, and goals, then enter a message to ask the assistant for help. " | |
| "Powered by Hugging Face Inference and GPT. Developed by wn. Disclaimer: AI may make mistakes. Use with caution." | |
| ), | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |