Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
import whisper
|
| 4 |
from pydub import AudioSegment
|
| 5 |
|
| 6 |
-
#
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
whisper_model = whisper.load_model("base")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def transcribe_audio(file_path):
|
| 12 |
try:
|
| 13 |
print(f"📂 Processing audio: {file_path}")
|
|
@@ -19,37 +21,94 @@ def transcribe_audio(file_path):
|
|
| 19 |
except Exception as e:
|
| 20 |
return f"❌ ERROR: {str(e)}"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def set_type(choice, user_profile):
|
| 24 |
user_profile["interview_type"] = choice
|
| 25 |
return "Great! What’s your background and what field/role are you aiming for?", user_profile
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def save_background(info, user_profile):
|
| 29 |
user_profile["field"] = info
|
| 30 |
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 31 |
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def respond(message, chat_history, user_profile):
|
|
|
|
|
|
|
| 34 |
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 35 |
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
| 36 |
chat_history.append((message, bot_msg))
|
| 37 |
return chat_history
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
messages = [
|
| 40 |
-
{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in
|
|
|
|
| 41 |
]
|
| 42 |
-
for q, a in chat_history:
|
| 43 |
-
messages.append({"role": "user", "content": q})
|
| 44 |
-
messages.append({"role": "assistant", "content": a})
|
| 45 |
-
messages.append({"role": "user", "content": message})
|
| 46 |
-
|
| 47 |
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 48 |
-
bot_msg = response.choices[0].message.content
|
| 49 |
chat_history.append((message, bot_msg))
|
| 50 |
return chat_history
|
| 51 |
|
| 52 |
-
#
|
| 53 |
def handle_audio(audio_file, chat_history, user_profile):
|
| 54 |
transcribed = transcribe_audio(audio_file)
|
| 55 |
if transcribed.startswith("❌"):
|
|
@@ -57,28 +116,26 @@ def handle_audio(audio_file, chat_history, user_profile):
|
|
| 57 |
return chat_history
|
| 58 |
return respond(transcribed, chat_history, user_profile)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
user_profile = gr.State({"interview_type": "", "field": ""})
|
| 63 |
chat_history = gr.State([])
|
| 64 |
|
| 65 |
-
gr.Markdown(
|
| 66 |
-
gr.
|
| 67 |
|
| 68 |
-
# Step 1: Choose Interview Type
|
| 69 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column():
|
| 72 |
-
btn1 = gr.Button("
|
| 73 |
-
btn2 = gr.Button("
|
| 74 |
-
btn3 = gr.Button("
|
| 75 |
type_output = gr.Textbox(label="Bot response", interactive=False)
|
| 76 |
|
| 77 |
-
btn1.click(set_type, inputs=[gr.Textbox(value="
|
| 78 |
-
btn2.click(set_type, inputs=[gr.Textbox(value="
|
| 79 |
-
btn3.click(set_type, inputs=[gr.Textbox(value="
|
| 80 |
|
| 81 |
-
# Step 2: Enter Background
|
| 82 |
gr.Markdown("### Step 2: Enter Your Background")
|
| 83 |
background = gr.Textbox(label="Your background and field/goal")
|
| 84 |
background_btn = gr.Button("Submit")
|
|
@@ -86,13 +143,12 @@ with gr.Blocks() as demo:
|
|
| 86 |
|
| 87 |
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
|
| 88 |
|
| 89 |
-
# Step 3: Interview Chat
|
| 90 |
gr.Markdown("### Step 3: Start Interview")
|
| 91 |
chatbot = gr.Chatbot(label="Interview Bot")
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
msg = gr.Textbox(label="Your message")
|
| 95 |
-
audio_input = gr.Audio(type="filepath", label="
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
send_btn = gr.Button("Send Text")
|
|
@@ -100,8 +156,8 @@ with gr.Blocks() as demo:
|
|
| 100 |
|
| 101 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 102 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 103 |
-
|
| 104 |
audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
| 1 |
+
# imports
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
import random
|
| 5 |
import whisper
|
| 6 |
from pydub import AudioSegment
|
| 7 |
|
| 8 |
+
# models
|
| 9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
whisper_model = whisper.load_model("base")
|
| 11 |
|
| 12 |
+
# whisper audio-to-text function
|
| 13 |
def transcribe_audio(file_path):
|
| 14 |
try:
|
| 15 |
print(f"📂 Processing audio: {file_path}")
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return f"❌ ERROR: {str(e)}"
|
| 23 |
|
| 24 |
+
# setting up the users profile (step 1)
|
| 25 |
def set_type(choice, user_profile):
|
| 26 |
user_profile["interview_type"] = choice
|
| 27 |
return "Great! What’s your background and what field/role are you aiming for?", user_profile
|
| 28 |
|
| 29 |
+
# step 2
|
| 30 |
def save_background(info, user_profile):
|
| 31 |
user_profile["field"] = info
|
| 32 |
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 33 |
|
| 34 |
+
# generate question using LLM
|
| 35 |
+
def generate_question(user_profile):
|
| 36 |
+
system_prompt = f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}. Generate one thoughtful, clear, and concise interview question."
|
| 37 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 38 |
+
response = client.chat_completion(messages, max_tokens=100, stream=False)
|
| 39 |
+
return response.choices[0].message.content.strip()
|
| 40 |
+
|
| 41 |
+
# generate feedback using LLM
|
| 42 |
+
def generate_feedback_llm(user_profile):
|
| 43 |
+
feedback = []
|
| 44 |
+
for i, (question, answer) in enumerate(zip(user_profile.get("questions", []), user_profile.get("user_answers", []))):
|
| 45 |
+
messages = [
|
| 46 |
+
{"role": "system", "content": f"You are a professional interviewer providing feedback for a candidate's response in a {user_profile['interview_type']} interview for a {user_profile['field']} role."},
|
| 47 |
+
{"role": "user", "content": f"Question: {question}\nAnswer: {answer}\nPlease give specific, constructive feedback."}
|
| 48 |
+
]
|
| 49 |
+
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 50 |
+
feedback.append(f"Question {i+1}: {response.choices[0].message.content.strip()}")
|
| 51 |
+
return "\n\n".join(feedback)
|
| 52 |
+
|
| 53 |
+
# step 3: interview loop
|
| 54 |
def respond(message, chat_history, user_profile):
|
| 55 |
+
message_lower = message.strip().lower()
|
| 56 |
+
|
| 57 |
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 58 |
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
| 59 |
chat_history.append((message, bot_msg))
|
| 60 |
return chat_history
|
| 61 |
|
| 62 |
+
if message_lower == 'start':
|
| 63 |
+
user_profile['questions'] = []
|
| 64 |
+
user_profile['user_answers'] = []
|
| 65 |
+
user_profile['current_q'] = 0
|
| 66 |
+
user_profile['interview_in_progress'] = True
|
| 67 |
+
|
| 68 |
+
intro = f"Welcome to your {user_profile['interview_type']} interview for a {user_profile['field']} position. I will ask you up to 10 questions. Type 'stop' anytime to end."
|
| 69 |
+
first_q = generate_question(user_profile)
|
| 70 |
+
user_profile['questions'].append(first_q)
|
| 71 |
+
chat_history.append((message, intro))
|
| 72 |
+
chat_history.append(("", f"First question: {first_q}"))
|
| 73 |
+
return chat_history
|
| 74 |
+
|
| 75 |
+
if message_lower == 'stop' and user_profile.get("interview_in_progress"):
|
| 76 |
+
user_profile['interview_in_progress'] = False
|
| 77 |
+
bot_msg = "Interview stopped. Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
|
| 78 |
+
chat_history.append((message, bot_msg))
|
| 79 |
+
return chat_history
|
| 80 |
+
|
| 81 |
+
if message_lower == 'feedback':
|
| 82 |
+
feedback = generate_feedback_llm(user_profile)
|
| 83 |
+
chat_history.append((message, feedback))
|
| 84 |
+
return chat_history
|
| 85 |
+
|
| 86 |
+
if user_profile.get("interview_in_progress"):
|
| 87 |
+
user_profile['user_answers'].append(message)
|
| 88 |
+
user_profile['current_q'] += 1
|
| 89 |
+
|
| 90 |
+
if user_profile['current_q'] < 10:
|
| 91 |
+
next_q = generate_question(user_profile)
|
| 92 |
+
user_profile['questions'].append(next_q)
|
| 93 |
+
bot_msg = f"Next question: {next_q}"
|
| 94 |
+
else:
|
| 95 |
+
user_profile['interview_in_progress'] = False
|
| 96 |
+
bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
|
| 97 |
+
|
| 98 |
+
chat_history.append((message, bot_msg))
|
| 99 |
+
return chat_history
|
| 100 |
+
|
| 101 |
+
# fallback LLM response
|
| 102 |
messages = [
|
| 103 |
+
{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."},
|
| 104 |
+
{"role": "user", "content": message}
|
| 105 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
response = client.chat_completion(messages, max_tokens=150, stream=False)
|
| 107 |
+
bot_msg = response.choices[0].message.content.strip()
|
| 108 |
chat_history.append((message, bot_msg))
|
| 109 |
return chat_history
|
| 110 |
|
| 111 |
+
# handle audio input
|
| 112 |
def handle_audio(audio_file, chat_history, user_profile):
|
| 113 |
transcribed = transcribe_audio(audio_file)
|
| 114 |
if transcribed.startswith("❌"):
|
|
|
|
| 116 |
return chat_history
|
| 117 |
return respond(transcribed, chat_history, user_profile)
|
| 118 |
|
| 119 |
+
# UI
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
+
user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
|
| 122 |
chat_history = gr.State([])
|
| 123 |
|
| 124 |
+
gr.Markdown("# Welcome to Intervu")
|
| 125 |
+
gr.Image(value="images.JPEG", show_label=False, width=200)
|
| 126 |
|
|
|
|
| 127 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
| 130 |
+
btn1 = gr.Button("Technical")
|
| 131 |
+
btn2 = gr.Button("Competency-Based Interview")
|
| 132 |
+
btn3 = gr.Button("Case")
|
| 133 |
type_output = gr.Textbox(label="Bot response", interactive=False)
|
| 134 |
|
| 135 |
+
btn1.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 136 |
+
btn2.click(set_type, inputs=[gr.Textbox(value="Competency-Based Interview", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 137 |
+
btn3.click(set_type, inputs=[gr.Textbox(value="Case", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 138 |
|
|
|
|
| 139 |
gr.Markdown("### Step 2: Enter Your Background")
|
| 140 |
background = gr.Textbox(label="Your background and field/goal")
|
| 141 |
background_btn = gr.Button("Submit")
|
|
|
|
| 143 |
|
| 144 |
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
|
| 145 |
|
|
|
|
| 146 |
gr.Markdown("### Step 3: Start Interview")
|
| 147 |
chatbot = gr.Chatbot(label="Interview Bot")
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
msg = gr.Textbox(label="Your message")
|
| 151 |
+
audio_input = gr.Audio(type="filepath", label="🎧 Upload or Record your answer")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
send_btn = gr.Button("Send Text")
|
|
|
|
| 156 |
|
| 157 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 158 |
send_btn.click(lambda: "", None, msg, queue=False)
|
|
|
|
| 159 |
audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 160 |
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
demo.launch()
|