Krishnavamshithumma commited on
Commit
5ef46b2
Β·
verified Β·
1 Parent(s): 7d4d42e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
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 chat(user_input, history, api_key):
22
  if not api_key:
23
- return "❌ Please enter your OpenAI API key.", history
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
- messages.append({"role": "user", "content": user_input})
35
-
36
- response = client.chat.completions.create(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  model="gpt-4",
38
- temperature=0.7,
39
- messages=messages
40
  )
 
41
 
42
- bot_reply = response.choices[0].message.content.strip()
43
- history.append((user_input, bot_reply))
44
- return bot_reply, history
45
 
46
  except Exception as e:
47
- return f"❌ Error: {str(e)}", history
48
 
49
- # Gradio interface
50
  with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
51
- gr.Markdown("## πŸŽ™οΈ Ask me about my experience, projects, or skills")
52
 
53
- api_key = gr.Textbox(label="πŸ” Enter your OpenAI API Key", type="password")
54
  chatbot = gr.Chatbot(label="🧠 Voice Bot", type="messages")
55
- msg = gr.Textbox(label="πŸ’¬ Ask a question")
56
- clear = gr.Button("Clear")
57
-
58
- history = gr.State([])
59
-
60
- def respond(message, history, key):
61
- reply, updated = chat(message, history, key)
62
- return updated, updated
63
-
64
- msg.submit(respond, [msg, history, api_key], [chatbot, history])
65
- clear.click(lambda: ([], []), None, [chatbot, history])
66
-
67
- # Launch the Gradio app
68
- demo.launch()
 
 
 
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()