Spaces:
Running
Running
import os | |
import openai | |
import gradio as gr | |
# Set OpenAI API key from environment variable | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
# Function to generate customized career advice | |
def generate_career_advice(field, position_name, current_qualifications, likes, skills): | |
# Craft the prompt for the OpenAI model | |
prompt = f"""You are a career advisor AI. Provide customized career advice using the following details: | |
- Desired Career Field: {field} | |
- Dream Job: {position_name} | |
- Current Qualifications: {current_qualifications} | |
- Likes: {likes} | |
- Skills: {skills} | |
Include: | |
- Suitable career paths that are a good fit and in demand. | |
- Additional qualifications, courses, training, or certifications to pursue. | |
- Tips on networking and gaining experience. | |
Be concise and limit your response to 512 tokens or less.""" | |
# Call the OpenAI API | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-4", # Use "gpt-3.5-turbo" if desired | |
messages=[{"role": "system", "content": "You are a helpful and knowledgeable career advisor."}, | |
{"role": "user", "content": prompt}], | |
max_tokens=512, | |
temperature=0.7, | |
) | |
# Extract the response text | |
career_advice = response["choices"][0]["message"]["content"].strip() | |
except Exception as e: | |
career_advice = f"An error occurred: {str(e)}" | |
return career_advice | |
# Create Gradio interface for the career advice application | |
career_advice_app = gr.Interface( | |
fn=generate_career_advice, | |
allow_flagging="never", | |
inputs=[ | |
gr.Textbox(label="Desired Career Field", placeholder="Enter the field you're interested in or type 'not sure'."), | |
gr.Textbox(label="Your Dream Job", placeholder="Enter your dream job or type 'not sure'."), | |
gr.Textbox(label="Current Qualifications and Certifications", placeholder="Enter your qualifications..."), | |
gr.Textbox(label="Likes", placeholder="Enter things you like (e.g., helping others, working with hands)...", lines=10), | |
gr.Textbox(label="Skills", placeholder="Enter your skills (e.g., math, science, languages)...", lines=10), | |
], | |
outputs=gr.Textbox(label="Customized Career Advice"), | |
title="Customized AI-Powered Career Advice", | |
description="This app provides AI-powered customized career advice based on your input. Note: even AI can make mistakes!", | |
) | |
# Launch the application | |
career_advice_app.launch(server_name="0.0.0.0", debug=True, server_port=7860, share=True) | |