Update app.py
Browse files
app.py
CHANGED
|
@@ -18,51 +18,63 @@ Answer questions about your background, experience, projects, and skills based o
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
# Chat function that takes a question + API key and keeps history
|
| 21 |
-
def
|
| 22 |
if not api_key:
|
| 23 |
-
return "β Please enter your OpenAI API key."
|
| 24 |
|
| 25 |
try:
|
| 26 |
client = OpenAI(api_key=api_key)
|
| 27 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 28 |
-
|
| 29 |
-
# Append chat history
|
| 30 |
-
for user_msg, bot_msg in history:
|
| 31 |
-
messages.append({"role": "user", "content": user_msg})
|
| 32 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
model="gpt-4",
|
| 38 |
-
|
| 39 |
-
|
| 40 |
)
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
history.append((
|
| 44 |
-
return
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"β Error: {
|
| 48 |
|
| 49 |
-
# Gradio interface
|
| 50 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
| 51 |
-
gr.Markdown("## ποΈ
|
| 52 |
|
| 53 |
-
api_key = gr.Textbox(label="π
|
| 54 |
chatbot = gr.Chatbot(label="π§ Voice Bot", type="messages")
|
| 55 |
-
|
| 56 |
-
clear
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
# Chat function that takes a question + API key and keeps history
|
| 21 |
+
def transcribe_and_chat(audio, history, api_key):
|
| 22 |
if not api_key:
|
| 23 |
+
return history, "β Please enter your OpenAI API key."
|
| 24 |
|
| 25 |
try:
|
| 26 |
client = OpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# 1) Transcribe the audio file with Whisper
|
| 29 |
+
with open(audio, "rb") as f:
|
| 30 |
+
whisper_resp = client.audio.transcriptions.create(
|
| 31 |
+
model="whisper-1",
|
| 32 |
+
file=f
|
| 33 |
+
)
|
| 34 |
+
user_text = whisper_resp.text.strip()
|
| 35 |
+
|
| 36 |
+
# 2) Append user message to history
|
| 37 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 38 |
+
for u, b in history:
|
| 39 |
+
messages += [
|
| 40 |
+
{"role": "user", "content": u},
|
| 41 |
+
{"role": "assistant", "content": b}
|
| 42 |
+
]
|
| 43 |
+
messages.append({"role": "user", "content": user_text})
|
| 44 |
+
|
| 45 |
+
# 3) ChatCompletion
|
| 46 |
+
chat_resp = client.chat.completions.create(
|
| 47 |
model="gpt-4",
|
| 48 |
+
messages=messages,
|
| 49 |
+
temperature=0.7
|
| 50 |
)
|
| 51 |
+
bot_reply = chat_resp.choices[0].message.content.strip()
|
| 52 |
|
| 53 |
+
# 4) Update history
|
| 54 |
+
history.append((user_text, bot_reply))
|
| 55 |
+
return history, history
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
return history, f"β Error: {e}"
|
| 59 |
|
|
|
|
| 60 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
| 61 |
+
gr.Markdown("## ποΈ Speak your question; get a text answer")
|
| 62 |
|
| 63 |
+
api_key = gr.Textbox(label="π OpenAI API Key", type="password")
|
| 64 |
chatbot = gr.Chatbot(label="π§ Voice Bot", type="messages")
|
| 65 |
+
mic = gr.Audio(source="microphone", type="filepath", label="π€ Record your question")
|
| 66 |
+
clear = gr.Button("Clear chat")
|
| 67 |
+
state = gr.State([])
|
| 68 |
+
|
| 69 |
+
# When mic component gets new audio, run transcription + chat
|
| 70 |
+
mic.change(
|
| 71 |
+
fn=transcribe_and_chat,
|
| 72 |
+
inputs=[mic, state, api_key],
|
| 73 |
+
outputs=[chatbot, state]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Clear history
|
| 77 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 78 |
+
|
| 79 |
+
# Note: we donβt need share=True on Spaces
|
| 80 |
+
demo.launch()
|