Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ 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 |
-
|
7 |
- A Generative AI and Data Engineering enthusiast with 1.5+ years of experience in data pipelines, automation, and scalable solutions
|
8 |
- Currently working as a Data Engineer at Wishkarma in Hyderabad, where you've optimized ETL pipelines processing 10K+ records daily and developed an image-based product similarity search engine using CLIP-ViT-L/14
|
9 |
- Previously worked as a Data Engineer Intern at DeepThought Growth Management System, where you processed 700+ data records and mentored 400+ students
|
@@ -11,22 +10,22 @@ system_prompt = """
|
|
11 |
- Experienced in building GenAI products including conversational AI chatbots, RAG pipelines, and AI-powered tools
|
12 |
- A Computer Science graduate from Neil Gogte Institute of Technology with a CGPA of 7.5/10
|
13 |
- Passionate about solving real-world problems at the intersection of AI and software engineering
|
14 |
-
|
15 |
Answer questions about your background, experience, projects, and skills based on this resume. Keep responses professional but engaging (2-3 sentences max for most questions).
|
16 |
"""
|
17 |
|
18 |
def chat_with_openai(user_input, history, api_key):
|
19 |
if not api_key:
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
try:
|
23 |
client = OpenAI(api_key=api_key)
|
24 |
-
|
25 |
-
# Build messages
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
messages.append({"role": "assistant", "content": entry[1]})
|
30 |
messages.append({"role": "user", "content": user_input})
|
31 |
|
32 |
# Get response from OpenAI
|
@@ -35,67 +34,71 @@ def chat_with_openai(user_input, history, api_key):
|
|
35 |
messages=messages,
|
36 |
temperature=0.7
|
37 |
)
|
38 |
-
|
39 |
-
bot_reply = response.choices
|
40 |
-
history
|
41 |
-
|
42 |
-
|
|
|
43 |
except Exception as e:
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant")
|
48 |
-
|
49 |
-
# Add custom CSS
|
50 |
-
gr.HTML("""
|
51 |
-
<style>
|
52 |
-
#chatBox {
|
53 |
-
height: 60vh;
|
54 |
-
overflow-y: auto;
|
55 |
-
padding: 20px;
|
56 |
-
border-radius: 10px;
|
57 |
-
background: #f9f9f9;
|
58 |
-
}
|
59 |
-
.message {
|
60 |
-
margin: 10px 0;
|
61 |
-
padding: 12px;
|
62 |
-
border-radius: 8px;
|
63 |
-
}
|
64 |
-
.user {
|
65 |
-
background: #e3f2fd;
|
66 |
-
text-align: right;
|
67 |
-
}
|
68 |
-
.bot {
|
69 |
-
background: #f5f5f5;
|
70 |
-
}
|
71 |
-
#micButton {
|
72 |
-
width: 100%;
|
73 |
-
padding: 12px;
|
74 |
-
font-size: 1.2em;
|
75 |
-
}
|
76 |
-
</style>
|
77 |
-
""")
|
78 |
-
|
79 |
api_key = gr.Textbox(label="π OpenAI API Key", type="password", elem_id="apiKeyInput")
|
|
|
|
|
|
|
80 |
chatbot = gr.Chatbot(elem_id="chatBox", type="messages")
|
81 |
-
state
|
82 |
-
|
|
|
83 |
with gr.Row():
|
|
|
|
|
84 |
mic_btn = gr.Button("π€ Speak", elem_id="micButton")
|
85 |
clear_btn = gr.Button("ποΈ Clear Chat")
|
86 |
-
|
87 |
-
# Hidden components for JS communication
|
|
|
|
|
88 |
voice_input = gr.Textbox(visible=False, elem_id="voiceInput")
|
89 |
-
|
|
|
90 |
# Event handlers
|
|
|
91 |
voice_input.change(
|
92 |
-
chat_with_openai,
|
93 |
-
[voice_input, state, api_key],
|
94 |
[chatbot, state]
|
95 |
)
|
96 |
-
|
97 |
-
#
|
98 |
-
mic_btn.click(None,
|
99 |
-
|
|
|
100 |
|
101 |
demo.launch()
|
|
|
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 in data pipelines, automation, and scalable solutions
|
7 |
- Currently working as a Data Engineer at Wishkarma in Hyderabad, where you've optimized ETL pipelines processing 10K+ records daily and developed an image-based product similarity search engine using CLIP-ViT-L/14
|
8 |
- Previously worked as a Data Engineer Intern at DeepThought Growth Management System, where you processed 700+ data records and mentored 400+ students
|
|
|
10 |
- Experienced in building GenAI products including conversational AI chatbots, RAG pipelines, and AI-powered tools
|
11 |
- A Computer Science graduate from Neil Gogte Institute of Technology with a CGPA of 7.5/10
|
12 |
- Passionate about solving real-world problems at the intersection of AI and software engineering
|
|
|
13 |
Answer questions about your background, experience, projects, and skills based on this resume. Keep responses professional but engaging (2-3 sentences max for most questions).
|
14 |
"""
|
15 |
|
16 |
def chat_with_openai(user_input, history, api_key):
|
17 |
if not api_key:
|
18 |
+
# When type='messages', history is a list of dictionaries.
|
19 |
+
# Append error message in the expected format.
|
20 |
+
return history + [{"role": "assistant", "content": "β Please enter your OpenAI API key."}], "β Please enter your OpenAI API key."
|
21 |
+
|
22 |
try:
|
23 |
client = OpenAI(api_key=api_key)
|
24 |
+
|
25 |
+
# Build messages for OpenAI API.
|
26 |
+
# The 'history' parameter from Gradio's chatbot (with type='messages')
|
27 |
+
# is already in the correct format for OpenAI's messages list.
|
28 |
+
messages = [{"role": "system", "content": system_prompt}] + history
|
|
|
29 |
messages.append({"role": "user", "content": user_input})
|
30 |
|
31 |
# Get response from OpenAI
|
|
|
34 |
messages=messages,
|
35 |
temperature=0.7
|
36 |
)
|
37 |
+
|
38 |
+
bot_reply = response.choices.message.content
|
39 |
+
# Append user and bot messages to the history in the 'messages' format
|
40 |
+
history.append({"role": "user", "content": user_input})
|
41 |
+
history.append({"role": "assistant", "content": bot_reply})
|
42 |
+
return history, history # Return updated history for chatbot and state
|
43 |
except Exception as e:
|
44 |
+
# Append error message in the 'messages' format
|
45 |
+
return history + [{"role": "assistant", "content": f"β Error: {str(e)}"}], f"β Error: {str(e)}"
|
46 |
+
|
47 |
+
# Define the JavaScript function that will be called by the mic_btn.
|
48 |
+
# This script will be injected into the Gradio Blocks using the 'js' parameter.
|
49 |
+
js_script = """
|
50 |
+
function startListening() {
|
51 |
+
console.log("startListening JS function called!");
|
52 |
+
// This is a placeholder. In a real application, you would integrate with
|
53 |
+
// browser's Web Speech API or another microphone input library here.
|
54 |
+
// For example, you might trigger a browser's speech recognition.
|
55 |
+
alert("Microphone listening started! (Placeholder)");
|
56 |
|
57 |
+
// If you want to send recognized speech back to Python, you would update
|
58 |
+
// the 'voice_input' Textbox and dispatch an 'input' event, like this:
|
59 |
+
// const voiceInput = document.getElementById('voiceInput');
|
60 |
+
// if (voiceInput) {
|
61 |
+
// voiceInput.value = "Simulated voice input from JS"; // Replace with actual speech-to-text result
|
62 |
+
// voiceInput.dispatchEvent(new Event('input', { bubbles: true }));
|
63 |
+
// }
|
64 |
+
}
|
65 |
+
"""
|
66 |
+
|
67 |
+
# The 'js' parameter in gr.Blocks() is used to inject global JavaScript.
|
68 |
+
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma", js=js_script) as demo:
|
69 |
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant")
|
70 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
api_key = gr.Textbox(label="π OpenAI API Key", type="password", elem_id="apiKeyInput")
|
72 |
+
# Corrected: Set type='messages' as recommended by the UserWarning.
|
73 |
+
# This configures the chatbot to expect and display messages as a list of dictionaries
|
74 |
+
# with 'role' and 'content' keys, aligning with OpenAI's message format.
|
75 |
chatbot = gr.Chatbot(elem_id="chatBox", type="messages")
|
76 |
+
# The state will now store the chat history as a list of dictionaries.
|
77 |
+
state = gr.State()
|
78 |
+
|
79 |
with gr.Row():
|
80 |
+
# Corrected: Changed '_js' to 'js'. The 'js' parameter is the correct way
|
81 |
+
# to specify a frontend JavaScript function to run before the Python 'fn'.
|
82 |
mic_btn = gr.Button("π€ Speak", elem_id="micButton")
|
83 |
clear_btn = gr.Button("ποΈ Clear Chat")
|
84 |
+
|
85 |
+
# Hidden components for JS communication.
|
86 |
+
# 'voice_input' is intended to be updated by the JavaScript function,
|
87 |
+
# which then triggers the 'chat_with_openai' Python function.
|
88 |
voice_input = gr.Textbox(visible=False, elem_id="voiceInput")
|
89 |
+
js_trigger = gr.Textbox(visible=False, elem_id="jsTrigger") # This component is not used in the provided logic
|
90 |
+
|
91 |
# Event handlers
|
92 |
+
# This will be triggered when the 'voice_input' Textbox's value changes (e.g., updated by JS).
|
93 |
voice_input.change(
|
94 |
+
chat_with_openai,
|
95 |
+
[voice_input, state, api_key],
|
96 |
[chatbot, state]
|
97 |
)
|
98 |
+
# This button now correctly calls the 'startListening' JavaScript function directly in the browser.
|
99 |
+
# Since 'startListening' is a pure frontend action, 'fn', 'inputs', and 'outputs' can be None.
|
100 |
+
mic_btn.click(None, None, None, js="startListening")
|
101 |
+
# Clear button correctly resets chatbot and state to empty lists, compatible with 'messages' type.
|
102 |
+
clear_btn.click(lambda: (,), None, [chatbot, state])
|
103 |
|
104 |
demo.launch()
|