import os
from ibm_watson_machine_learning.foundation_models import Model
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes, DecodingMethods
import gradio as gr


# Set up the API key and project ID for IBM Watson
watsonx_API = os.environ.get("watsonx_API") 
project_id = os.environ.get("project_id")

# Generation parameters
gen_parms = {
    "max_new_tokens": 512,  # Adjust as needed 
    "temperature": 0.7  # Adjust for creativity
}

# Model and project settings
model_id = "meta-llama/llama-2-13b-chat"

credentials={
        "apikey": watsonx_API,
        "url": "https://us-south.ml.cloud.ibm.com"
    }

model = Model(
    model_id = 'meta-llama/llama-2-13b-chat', # you can also specify like: ModelTypes.LLAMA_2_70B_CHAT
    params = gen_parms,
    credentials={
        "apikey": watsonx_API,
        "url": "https://us-south.ml.cloud.ibm.com"
    },
    project_id= project_id
    )

# Initialize the model
model = Model(model_id, credentials, gen_parms, project_id)

# Function to generate customized career advice
def generate_career_advice(field, position_name, current_qualifications, likes, skills):
    # Craft the prompt for the model 
    prompt = f"Generate a customized career advice using desired career field: {field}, \
    dream job: {position_name}, \
    current qualifications and certifications: {current_qualifications}, \
    likes: {likes}, \
    skills: {skills}. Include tips on which career paths make a good fit and are in demand, \
    what additional qualifications, courses, training or certifications to take, networking, \
    gaining experience, etc. Use a brief style and limit your answer within 512 tokens or less."

    generated_response = model.generate(prompt, gen_parms)

    # Extract the generated text
    career_advice = generated_response["results"][0]["generated_text"]
    return career_advice


# Create Gradio interface for the cover letter generation application
career_advice_app = gr.Interface(
    fn=generate_career_advice,
    allow_flagging="never", # Deactivate the flag function in gradio as it is not needed.
    inputs=[
        gr.Textbox(label="Desired Career Field (e.g., healthcare, trades, social service, etc., or enter 'not sure')", placeholder="Enter the field which you are interested in... or type 'not sure'."),
        gr.Textbox(label="Your Dream Job (e.g., nurse, personal support worker, software developer, plumber, etc., or enter 'not sure')", placeholder="Enter the name of the position you are interested in... or type 'not sure'"),
        gr.Textbox(label="Current Qualifications and or Certifications (e.g., studying in high school, high school diploma, college diploma, etc.)", placeholder="Enter your current qualifications ..."),
        gr.Textbox(label="Likes (e.g., I like working with my hands, I like to work outside, I like to help people, I like teaching, ...)", placeholder="Enter activities you like ...", lines=10),
        gr.Textbox(label="Skills (e.g., I am good at math, science, languages, computers, research, hand tools, etc.)", placeholder="Enter your skills ...", lines=10),
    ],
    outputs=gr.Textbox(label="Customized Career Advice"),
    title="Customized AI-Powered Career Advice - by Wael Nawara",
    description="This App will generate an AI-powered customized career advice based on the career field which you select, your dream job, current qualifications, likes and skills. A word of caution: even AI makes mistakes!"
)

# Launch the application
career_advice_app.launch(server_name="0.0.0.0", debug=True, server_port=7860, share=True)