Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,23 @@
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
|
4 |
-
system_prompt = """
|
5 |
-
You are a voice bot representing Krishnavamshi Thumma. When responding to questions, answer as if you are:
|
6 |
- A Generative AI and Data Engineering enthusiast with 1.5+ years of experience
|
7 |
- Currently working as a Data Engineer at Wishkarma in Hyderabad
|
8 |
- Previously worked as a Data Engineer Intern at DeepThought
|
9 |
- Skilled in Python, SQL, JavaScript, OpenAI GPT-4o, LangChain, etc.
|
10 |
- A Computer Science graduate from Neil Gogte Institute of Technology
|
11 |
-
Answer questions about your background professionally but engagingly (2-3 sentences max).
|
12 |
-
"""
|
13 |
|
14 |
def chat_with_openai(user_input, history, api_key):
|
15 |
if not api_key:
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
try:
|
19 |
client = OpenAI(api_key=api_key)
|
20 |
-
|
21 |
# Build messages from history
|
22 |
messages = [{"role": "system", "content": system_prompt}]
|
23 |
for entry in history:
|
@@ -31,17 +31,16 @@ def chat_with_openai(user_input, history, api_key):
|
|
31 |
messages=messages,
|
32 |
temperature=0.7
|
33 |
)
|
34 |
-
|
35 |
bot_reply = response.choices[0].message.content
|
36 |
history.append((user_input, bot_reply))
|
37 |
-
return history,
|
38 |
-
|
39 |
except Exception as e:
|
40 |
-
|
41 |
|
42 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
43 |
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant")
|
44 |
-
|
45 |
# Add custom CSS
|
46 |
gr.HTML("""
|
47 |
<style>
|
@@ -94,37 +93,90 @@ with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
|
94 |
}
|
95 |
</style>
|
96 |
""")
|
97 |
-
|
98 |
api_key = gr.Textbox(label="π OpenAI API Key", type="password", elem_id="apiKeyInput")
|
99 |
key_status = gr.HTML("<div id='keyStatus'></div>")
|
100 |
-
chatbot = gr.Chatbot(elem_id="chatBox", type="messages")
|
101 |
state = gr.State([])
|
102 |
-
|
103 |
-
#
|
104 |
-
|
|
|
|
|
|
|
105 |
mic_btn = gr.Button("π€ Click & Speak", elem_id="micButton")
|
106 |
-
|
107 |
clear_btn = gr.Button("ποΈ Clear Chat")
|
108 |
-
|
109 |
# Event handlers
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
114 |
)
|
115 |
-
|
|
|
116 |
# JavaScript functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
gr.HTML("""
|
118 |
<script>
|
119 |
-
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
120 |
-
recognition.continuous = false;
|
121 |
-
recognition.lang = "en-US";
|
122 |
-
|
123 |
document.getElementById("apiKeyInput").addEventListener("input", function() {
|
124 |
const apiKey = this.value.trim();
|
125 |
const keyStatus = document.getElementById("keyStatus");
|
126 |
const micButton = document.getElementById("micButton");
|
127 |
-
|
128 |
if (apiKey) {
|
129 |
keyStatus.innerHTML = '<div class="key-status success">API Key saved successfully!</div>';
|
130 |
micButton.disabled = false;
|
@@ -133,46 +185,17 @@ with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
|
133 |
micButton.disabled = true;
|
134 |
}
|
135 |
});
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
return;
|
142 |
-
}
|
143 |
-
|
144 |
-
this.textContent = "π΄ Listening...";
|
145 |
-
recognition.start();
|
146 |
-
});
|
147 |
-
|
148 |
-
recognition.onresult = function(event) {
|
149 |
-
const transcript = event.results[0][0].transcript;
|
150 |
-
document.querySelector("#voiceInput input").value = transcript;
|
151 |
-
document.querySelector("#voiceInput input").dispatchEvent(new Event("change"));
|
152 |
-
};
|
153 |
-
|
154 |
-
recognition.onend = function() {
|
155 |
-
document.getElementById("micButton").textContent = "π€ Click & Speak";
|
156 |
-
};
|
157 |
-
|
158 |
-
recognition.onerror = function(event) {
|
159 |
-
console.error("Speech recognition error", event.error);
|
160 |
-
document.getElementById("micButton").textContent = "π€ Click & Speak";
|
161 |
-
alert("Speech recognition error: " + event.error);
|
162 |
-
};
|
163 |
-
|
164 |
-
// Auto-scroll chat
|
165 |
-
function scrollChat() {
|
166 |
-
const chatBox = document.getElementById("chatBox");
|
167 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
168 |
-
}
|
169 |
-
|
170 |
// Initial setup
|
171 |
document.getElementById("micButton").disabled = true;
|
172 |
document.querySelector("#apiKeyInput input").focus();
|
173 |
</script>
|
174 |
""")
|
175 |
-
|
176 |
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
177 |
|
178 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
|
4 |
+
system_prompt = """You are a voice bot representing Krishnavamshi Thumma. When responding to questions, answer as if you are:
|
|
|
5 |
- A Generative AI and Data Engineering enthusiast with 1.5+ years of experience
|
6 |
- Currently working as a Data Engineer at Wishkarma in Hyderabad
|
7 |
- Previously worked as a Data Engineer Intern at DeepThought
|
8 |
- Skilled in Python, SQL, JavaScript, OpenAI GPT-4o, LangChain, etc.
|
9 |
- A Computer Science graduate from Neil Gogte Institute of Technology
|
10 |
+
Answer questions about your background professionally but engagingly (2-3 sentences max)."""
|
|
|
11 |
|
12 |
def chat_with_openai(user_input, history, api_key):
|
13 |
if not api_key:
|
14 |
+
# Instead of returning history and an error string,
|
15 |
+
# Gradio handles errors better when raised as exceptions
|
16 |
+
raise gr.Error("β Please enter your OpenAI API key.")
|
17 |
+
|
18 |
try:
|
19 |
client = OpenAI(api_key=api_key)
|
20 |
+
|
21 |
# Build messages from history
|
22 |
messages = [{"role": "system", "content": system_prompt}]
|
23 |
for entry in history:
|
|
|
31 |
messages=messages,
|
32 |
temperature=0.7
|
33 |
)
|
34 |
+
|
35 |
bot_reply = response.choices[0].message.content
|
36 |
history.append((user_input, bot_reply))
|
37 |
+
return history, "" # Clear the input text box after processing
|
|
|
38 |
except Exception as e:
|
39 |
+
raise gr.Error(f"β Error: {str(e)}")
|
40 |
|
41 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
42 |
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant")
|
43 |
+
|
44 |
# Add custom CSS
|
45 |
gr.HTML("""
|
46 |
<style>
|
|
|
93 |
}
|
94 |
</style>
|
95 |
""")
|
96 |
+
|
97 |
api_key = gr.Textbox(label="π OpenAI API Key", type="password", elem_id="apiKeyInput")
|
98 |
key_status = gr.HTML("<div id='keyStatus'></div>")
|
99 |
+
chatbot = gr.Chatbot(elem_id="chatBox", type="messages", height=400) # Added height for better display
|
100 |
state = gr.State([])
|
101 |
+
|
102 |
+
# This textbox will be the one that takes the speech-to-text output
|
103 |
+
# and directly triggers the chat_with_openai function.
|
104 |
+
# It will remain hidden from the user.
|
105 |
+
text_input_for_voice = gr.Textbox(label="Voice Input Text", visible=False)
|
106 |
+
|
107 |
mic_btn = gr.Button("π€ Click & Speak", elem_id="micButton")
|
108 |
+
|
109 |
clear_btn = gr.Button("ποΈ Clear Chat")
|
110 |
+
|
111 |
# Event handlers
|
112 |
+
# The chat_with_openai function will now be triggered by the text_input_for_voice.submit()
|
113 |
+
# or by its change event, which will be manually triggered by the JavaScript.
|
114 |
+
text_input_for_voice.submit(
|
115 |
+
chat_with_openai,
|
116 |
+
[text_input_for_voice, state, api_key],
|
117 |
+
[chatbot, state, text_input_for_voice] # Also clear the voice input textbox after submission
|
118 |
)
|
119 |
+
|
120 |
+
|
121 |
# JavaScript functions
|
122 |
+
# Using Gradio's _js to integrate JavaScript with Python components directly
|
123 |
+
mic_btn.click(
|
124 |
+
None,
|
125 |
+
inputs=[api_key],
|
126 |
+
outputs=[mic_btn],
|
127 |
+
_js="""
|
128 |
+
(api_key) => {
|
129 |
+
if (!api_key) {
|
130 |
+
alert("Please enter your OpenAI API key first!");
|
131 |
+
return [gr.Button.update(interactive=true)]; // Keep button enabled if no key
|
132 |
+
}
|
133 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
134 |
+
recognition.continuous = false;
|
135 |
+
recognition.lang = "en-US";
|
136 |
+
const micButton = document.getElementById("micButton");
|
137 |
+
|
138 |
+
micButton.textContent = "π΄ Listening...";
|
139 |
+
micButton.disabled = true; // Disable while listening
|
140 |
+
|
141 |
+
recognition.onresult = function(event) {
|
142 |
+
const transcript = event.results[0][0].transcript;
|
143 |
+
// Directly set the value of the hidden text_input_for_voice and trigger its submit event
|
144 |
+
// Gradio's internal JS will handle the connection to the Python backend function
|
145 |
+
const voiceInputTextbox = document.querySelector('#voice_input_for_voice input'); // Correctly select the hidden input field
|
146 |
+
if (voiceInputTextbox) {
|
147 |
+
voiceInputTextbox.value = transcript;
|
148 |
+
// Trigger the 'input' event which Gradio uses for its change/submit events
|
149 |
+
voiceInputTextbox.dispatchEvent(new Event('input', { bubbles: true }));
|
150 |
+
}
|
151 |
+
};
|
152 |
+
|
153 |
+
recognition.onend = function() {
|
154 |
+
micButton.textContent = "π€ Click & Speak";
|
155 |
+
micButton.disabled = false; // Re-enable after listening ends
|
156 |
+
};
|
157 |
+
|
158 |
+
recognition.onerror = function(event) {
|
159 |
+
console.error("Speech recognition error", event.error);
|
160 |
+
micButton.textContent = "π€ Click & Speak";
|
161 |
+
micButton.disabled = false; // Re-enable on error
|
162 |
+
alert("Speech recognition error: " + event.error);
|
163 |
+
};
|
164 |
+
|
165 |
+
recognition.start();
|
166 |
+
return [gr.Button.update(interactive=false)]; // Disable button visually when started
|
167 |
+
}
|
168 |
+
"""
|
169 |
+
)
|
170 |
+
|
171 |
+
|
172 |
+
# JavaScript for API key input and status
|
173 |
gr.HTML("""
|
174 |
<script>
|
|
|
|
|
|
|
|
|
175 |
document.getElementById("apiKeyInput").addEventListener("input", function() {
|
176 |
const apiKey = this.value.trim();
|
177 |
const keyStatus = document.getElementById("keyStatus");
|
178 |
const micButton = document.getElementById("micButton");
|
179 |
+
|
180 |
if (apiKey) {
|
181 |
keyStatus.innerHTML = '<div class="key-status success">API Key saved successfully!</div>';
|
182 |
micButton.disabled = false;
|
|
|
185 |
micButton.disabled = true;
|
186 |
}
|
187 |
});
|
188 |
+
|
189 |
+
// Auto-scroll chat - this should be triggered whenever chatbot updates
|
190 |
+
// However, Gradio's Chatbot often handles auto-scrolling itself.
|
191 |
+
// If not, you might need a more sophisticated way to observe changes.
|
192 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
// Initial setup
|
194 |
document.getElementById("micButton").disabled = true;
|
195 |
document.querySelector("#apiKeyInput input").focus();
|
196 |
</script>
|
197 |
""")
|
198 |
+
|
199 |
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
200 |
|
201 |
demo.launch()
|