Shanzass501's picture
Update app.py
c849066 verified
import gradio as gr
from groq import Groq
import os
# --- Secure API Key Access ---
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
MODEL = "llama-3.3-70b-versatile"
def interview_bot(user_input, history, role):
if not client:
# Returning dictionary format as required by Gradio 6
history.append({"role": "assistant", "content": "Error: GROQ_API_KEY missing."})
return history
system_prompt = f"You are a Senior Tech Lead interviewing for {role}. Ask 1 question at a time. After 5 questions, give a feedback report."
# Groq API expects this format
messages = [{"role": "system", "content": system_prompt}]
# History is now a list of dicts: [{"role": "user", "content": "..."}, ...]
for msg in history:
messages.append({"role": msg["role"], "content": msg["content"]})
messages.append({"role": "user", "content": user_input})
try:
response = client.chat.completions.create(model=MODEL, messages=messages)
answer = response.choices[0].message.content
# We append dictionaries to history to satisfy Gradio 6
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": answer})
return history
except Exception as e:
history.append({"role": "assistant", "content": f"Error: {str(e)}"})
return history
# --- UI Layout ---
# Theme moved to launch() to stop the warning
with gr.Blocks() as demo:
gr.Markdown("# 🤖 AI Interview Coach")
role_box = gr.Textbox(label="Job Role", value="Software Developer")
# We DO NOT set type="messages" here; Gradio 6 will infer it from the data
chatbot = gr.Chatbot(label="Interview Chat", height=500)
with gr.Row():
msg = gr.Textbox(placeholder="Type your answer...", show_label=False, scale=4)
submit = gr.Button("Submit", variant="primary")
reset = gr.Button("Reset Interview")
def respond(user_msg, chat_history, job_role):
if not user_msg.strip():
return "", chat_history
# Ensure chat_history is a list if it starts as None
if chat_history is None:
chat_history = []
new_history = interview_bot(user_msg, chat_history, job_role)
return "", new_history
msg.submit(respond, [msg, chatbot, role_box], [msg, chatbot])
submit.click(respond, [msg, chatbot, role_box], [msg, chatbot])
reset.click(lambda: [], None, chatbot)
# Move theme here to satisfy the Gradio 6.0 UserWarning
demo.launch(theme=gr.themes.Soft())