Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def speak(text):
|
| 2 |
"""
|
| 3 |
No external TTS API needed.
|
| 4 |
-
Gradio will
|
| 5 |
"""
|
| 6 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# =========================
|
| 5 |
+
# 1) OpenAI API Key
|
| 6 |
+
# =========================
|
| 7 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 8 |
+
|
| 9 |
+
# =========================
|
| 10 |
+
# 2) Recruiter first lines
|
| 11 |
+
# =========================
|
| 12 |
+
FIRST_LINES = {
|
| 13 |
+
"Friendly": "I see you took a few years off. That’s interesting — tell me more about what you learned during that time.",
|
| 14 |
+
"Tough": "A gap that long raises questions. Why should we consider you over someone with recent experience?"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# =========================
|
| 18 |
+
# 3) GPT reply for recruiter
|
| 19 |
+
# =========================
|
| 20 |
+
def get_recruiter_reply(user_text, style, history):
|
| 21 |
+
prompt = f"""
|
| 22 |
+
You are a recruiter in an interview. Tone = {style} (friendly or tough).
|
| 23 |
+
Conversation so far:
|
| 24 |
+
{history}
|
| 25 |
+
Candidate just said: {user_text}
|
| 26 |
+
Rules:
|
| 27 |
+
- Keep replies under 25 words
|
| 28 |
+
- Ask one probing question at a time
|
| 29 |
+
- Avoid repeating previous questions
|
| 30 |
+
Context: mid-career woman re-entering the workforce
|
| 31 |
+
Return the next recruiter question only.
|
| 32 |
+
"""
|
| 33 |
+
response = openai.ChatCompletion.create(
|
| 34 |
+
model="gpt-4o-mini",
|
| 35 |
+
messages=[{"role":"user", "content":prompt}]
|
| 36 |
+
)
|
| 37 |
+
return response.choices[0].message.content.strip()
|
| 38 |
+
|
| 39 |
+
# =========================
|
| 40 |
+
# 4) GPT feedback after 10 exchanges
|
| 41 |
+
# =========================
|
| 42 |
+
def get_feedback(history):
|
| 43 |
+
prompt = f"""
|
| 44 |
+
Based on this 10-turn transcript:
|
| 45 |
+
{history}
|
| 46 |
+
Return concise feedback:
|
| 47 |
+
- Confidence (0-10)
|
| 48 |
+
- Strengths (2 bullets)
|
| 49 |
+
- One improvement tip
|
| 50 |
+
- Example reframe sentence (<=20 words)
|
| 51 |
+
"""
|
| 52 |
+
response = openai.ChatCompletion.create(
|
| 53 |
+
model="gpt-4o-mini",
|
| 54 |
+
messages=[{"role":"user", "content":prompt}]
|
| 55 |
+
)
|
| 56 |
+
return response.choices[0].message.content.strip()
|
| 57 |
+
|
| 58 |
+
# =========================
|
| 59 |
+
# 5) Dummy TTS function (Gradio handles text-to-speech)
|
| 60 |
+
# =========================
|
| 61 |
def speak(text):
|
| 62 |
"""
|
| 63 |
No external TTS API needed.
|
| 64 |
+
Gradio will synthesize audio automatically.
|
| 65 |
"""
|
| 66 |
return text
|
| 67 |
+
|
| 68 |
+
# =========================
|
| 69 |
+
# 6) Conversation handler
|
| 70 |
+
# =========================
|
| 71 |
+
def run_boosty(style, user_voice, history="", turn=1):
|
| 72 |
+
# Step 1: First recruiter line
|
| 73 |
+
if turn == 1:
|
| 74 |
+
first_line = FIRST_LINES[style]
|
| 75 |
+
history += f"Recruiter ({style}): {first_line}\n"
|
| 76 |
+
audio = speak(first_line)
|
| 77 |
+
return "", history, audio, turn + 1, ""
|
| 78 |
+
|
| 79 |
+
# Steps 2-10: transcribe user voice using OpenAI Whisper API
|
| 80 |
+
with open(user_voice.name, "rb") as f:
|
| 81 |
+
transcription = openai.Audio.transcriptions.create(file=f, model="whisper-1")["text"]
|
| 82 |
+
|
| 83 |
+
history += f"User: {transcription}\n"
|
| 84 |
+
|
| 85 |
+
# After 10 exchanges → feedback
|
| 86 |
+
if turn > 10:
|
| 87 |
+
feedback = get_feedback(history)
|
| 88 |
+
return "", history, None, turn, feedback
|
| 89 |
+
|
| 90 |
+
# Generate next recruiter line
|
| 91 |
+
reply = get_recruiter_reply(transcription, style, history)
|
| 92 |
+
history += f"Recruiter: {reply}\n"
|
| 93 |
+
audio = speak(reply)
|
| 94 |
+
|
| 95 |
+
return "", history, audio, turn + 1, ""
|
| 96 |
+
|
| 97 |
+
# =========================
|
| 98 |
+
# 7) Gradio interface
|
| 99 |
+
# =========================
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
+
style_var = gr.Radio(["Friendly", "Tough"], label="Choose recruiter style")
|
| 102 |
+
user_voice = gr.Audio(source="microphone", type="file", label="Speak your reply")
|
| 103 |
+
history = gr.Textbox(label="Transcript", interactive=False)
|
| 104 |
+
audio_output = gr.Audio(label="Recruiter voice", type="auto", interactive=False)
|
| 105 |
+
turn_var = gr.State(value=1)
|
| 106 |
+
feedback_text = gr.Textbox(label="Feedback", interactive=False)
|
| 107 |
+
|
| 108 |
+
submit_btn = gr.Button("Next")
|
| 109 |
+
submit_btn.click(
|
| 110 |
+
fn=run_boosty,
|
| 111 |
+
inputs=[style_var, user_voice, history, turn_var],
|
| 112 |
+
outputs=[style_var, history, audio_output, turn_var, feedback_text]
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
demo.launch()
|