notes73
Fix API key handling and improve error handling
23be2dd
import os
import openai
import streamlit as st
# Get the OpenAI API key from the environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
# Raise an error if the API key is missing
if not openai.api_key:
raise ValueError("⚠️ OpenAI API key is missing! Add it in Hugging Face environment variables.")
# Streamlit UI Setup
st.set_page_config(page_title="AI Job Interview Coach", layout="centered")
st.title("🎀 AI-Powered Job Interview Coach")
st.write("πŸ’‘ Enter your **job description** and **interview question** to get tailored responses and practical tips.")
# User Inputs
job_description = st.text_area("πŸ“Œ Job Description", placeholder="e.g. AI Engineer with expertise in NLP and Deep Learning")
interview_question = st.text_input("❓ Interview Question", placeholder="e.g. Explain AI in simple terms.")
# πŸ”Ή Feature: Model Selection
model_choice = st.selectbox("🧠 Choose AI Model", ["gpt-4", "gpt-3.5-turbo"])
# πŸ”Ή Feature: Response Customization
temperature = st.slider("πŸŽ›οΈ Creativity (Temperature)", 0.0, 1.0, 0.7, 0.1)
max_tokens = st.slider("✍️ Max Response Length", 50, 500, 300, 50)
# Function to Generate AI Response
def generate_interview_response(job_description, interview_question):
messages = [
{"role": "system", "content": "You are an AI-powered job interview coach. Provide structured responses with practical tips."},
{"role": "user", "content": f"Job Description: {job_description}\nInterview Question: {interview_question}\n\nProvide a detailed response with actionable advice."}
]
try:
response = openai.chat.completions.create(
model=model_choice,
messages=messages,
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"⚠️ Error: {str(e)}"
# πŸ”Ή Generate AI Response on Button Click
if st.button("πŸš€ Generate Response"):
if job_description and interview_question:
response = generate_interview_response(job_description, interview_question)
st.subheader("πŸ’¬ AI Response:")
st.write(response)
# πŸ”Ή Feature: Save Response
if st.button("πŸ’Ύ Save Response"):
with open("AI_Interview_Response.txt", "w") as file:
file.write(response)
st.success("Response saved! You can find the file in your working directory.")
# πŸ”Ή Feature: Download Response
st.download_button("⬇️ Download Response", response, "interview_response.txt", "text/plain")
else:
st.error("⚠️ Please enter both a **Job Description** and an **Interview Question**.")
# πŸ”Ή Feature: General Interview Tips
st.subheader("πŸ“Œ General Interview Tips")
st.write("""
βœ… **Research the Company:** Understand its values, products, and culture.
βœ… **Practice Common Questions:** Prepare responses for typical interview questions.
βœ… **Use the STAR Method:** Structure your answers with **Situation, Task, Action, and Result**.
βœ… **Be Confident & Clear:** Speak slowly, maintain eye contact, and express your thoughts clearly.
βœ… **Ask Questions:** Show interest by asking insightful questions about the role and company.
""")