Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def set_type(choice, user_profile):
|
| 7 |
user_profile["interview_type"] = choice
|
| 8 |
return "Great! What’s your background and what field/role are you aiming for?", user_profile
|
| 9 |
|
|
|
|
| 10 |
def save_background(info, user_profile):
|
| 11 |
user_profile["field"] = info
|
| 12 |
return "Awesome! Type 'start' below to begin your interview.", user_profile
|
| 13 |
|
|
|
|
| 14 |
def respond(message, chat_history, user_profile):
|
| 15 |
if not user_profile.get("interview_type") or not user_profile.get("field"):
|
| 16 |
bot_msg = "Please finish steps 1 and 2 before starting the interview."
|
|
@@ -30,13 +49,22 @@ def respond(message, chat_history, user_profile):
|
|
| 30 |
chat_history.append((message, bot_msg))
|
| 31 |
return chat_history
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
user_profile = gr.State({"interview_type": "", "field": ""})
|
| 35 |
chat_history = gr.State([])
|
| 36 |
|
| 37 |
gr.Markdown("# 🎤 Welcome to Intervu")
|
| 38 |
|
| 39 |
-
# Step 1
|
| 40 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 41 |
with gr.Row():
|
| 42 |
with gr.Column():
|
|
@@ -49,7 +77,7 @@ with gr.Blocks() as demo:
|
|
| 49 |
btn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 50 |
btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 51 |
|
| 52 |
-
# Step 2
|
| 53 |
gr.Markdown("### Step 2: Enter Your Background")
|
| 54 |
background = gr.Textbox(label="Your background and field/goal")
|
| 55 |
background_btn = gr.Button("Submit")
|
|
@@ -57,13 +85,22 @@ with gr.Blocks() as demo:
|
|
| 57 |
|
| 58 |
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
|
| 59 |
|
| 60 |
-
# Step 3
|
| 61 |
gr.Markdown("### Step 3: Start Interview")
|
| 62 |
chatbot = gr.Chatbot(label="Interview Bot")
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 67 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import whisper
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
|
| 6 |
+
# Load models
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
+
whisper_model = whisper.load_model("base")
|
| 9 |
|
| 10 |
+
# ---------- Whisper Audio-to-Text Function ----------
|
| 11 |
+
def transcribe_audio(file_path):
|
| 12 |
+
try:
|
| 13 |
+
print(f"📂 Processing audio: {file_path}")
|
| 14 |
+
audio = AudioSegment.from_file(file_path)
|
| 15 |
+
converted_path = "converted.wav"
|
| 16 |
+
audio.export(converted_path, format="wav")
|
| 17 |
+
result = whisper_model.transcribe(converted_path, fp16=False)
|
| 18 |
+
return result["text"]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"❌ ERROR: {str(e)}"
|
| 21 |
+
|
| 22 |
+
# ---------- Step 1 ----------
|
| 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 |
+
# ---------- Step 2 ----------
|
| 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 |
+
# ---------- Step 3 ----------
|
| 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."
|
|
|
|
| 49 |
chat_history.append((message, bot_msg))
|
| 50 |
return chat_history
|
| 51 |
|
| 52 |
+
# ---------- Handle Audio Input ----------
|
| 53 |
+
def handle_audio(audio_file, chat_history, user_profile):
|
| 54 |
+
transcribed = transcribe_audio(audio_file)
|
| 55 |
+
if transcribed.startswith("❌"):
|
| 56 |
+
chat_history.append(("Audio input", transcribed))
|
| 57 |
+
return chat_history
|
| 58 |
+
return respond(transcribed, chat_history, user_profile)
|
| 59 |
+
|
| 60 |
+
# ---------- Gradio UI ----------
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
user_profile = gr.State({"interview_type": "", "field": ""})
|
| 63 |
chat_history = gr.State([])
|
| 64 |
|
| 65 |
gr.Markdown("# 🎤 Welcome to Intervu")
|
| 66 |
|
| 67 |
+
# Step 1: Choose Interview Type
|
| 68 |
gr.Markdown("### Step 1: Choose Interview Type")
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column():
|
|
|
|
| 77 |
btn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 78 |
btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])
|
| 79 |
|
| 80 |
+
# Step 2: Enter Background
|
| 81 |
gr.Markdown("### Step 2: Enter Your Background")
|
| 82 |
background = gr.Textbox(label="Your background and field/goal")
|
| 83 |
background_btn = gr.Button("Submit")
|
|
|
|
| 85 |
|
| 86 |
background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
|
| 87 |
|
| 88 |
+
# Step 3: Interview Chat
|
| 89 |
gr.Markdown("### Step 3: Start Interview")
|
| 90 |
chatbot = gr.Chatbot(label="Interview Bot")
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
msg = gr.Textbox(label="Your message")
|
| 94 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="🎙️ Or record your answer")
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
send_btn = gr.Button("Send Text")
|
| 98 |
+
audio_btn = gr.Button("Send Audio")
|
| 99 |
|
| 100 |
send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 101 |
send_btn.click(lambda: "", None, msg, queue=False)
|
| 102 |
|
| 103 |
+
audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
|
| 104 |
+
|
| 105 |
+
# ---------- Launch App ----------
|
| 106 |
demo.launch()
|