Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 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
|
|
@@ -9,6 +11,21 @@ system_prompt = """You are a voice bot representing Krishnavamshi Thumma. When r
|
|
| 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 |
raise gr.Error("β Please enter your OpenAI API key.")
|
|
@@ -31,104 +48,27 @@ def chat_with_openai(user_input, history, api_key):
|
|
| 31 |
raise gr.Error(f"β Error: {str(e)}")
|
| 32 |
|
| 33 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
| 34 |
-
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant")
|
| 35 |
-
|
| 36 |
-
gr.HTML("""
|
| 37 |
-
<style>
|
| 38 |
-
#chatBox { height: 60vh; overflow-y: auto; padding: 20px; border-radius: 10px; background: #f9f9f9; margin-bottom: 20px; }
|
| 39 |
-
.message { margin: 10px 0; padding: 12px; border-radius: 8px; }
|
| 40 |
-
.user { background: #e3f2fd; text-align: right; }
|
| 41 |
-
.bot { background: #f5f5f5; }
|
| 42 |
-
#micButton { width: 100%; padding: 12px; font-size: 1.2em; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; }
|
| 43 |
-
#micButton:disabled { background: #cccccc; cursor: not-allowed; }
|
| 44 |
-
.key-status { padding: 5px; margin-top: 5px; border-radius: 4px; }
|
| 45 |
-
.success { background: #d4edda; color: #155724; }
|
| 46 |
-
.error { background: #f8d7da; color: #721c24; }
|
| 47 |
-
</style>
|
| 48 |
-
""")
|
| 49 |
|
| 50 |
-
api_key = gr.Textbox(label="π OpenAI API Key", type="password"
|
| 51 |
-
|
| 52 |
-
chatbot = gr.Chatbot(elem_id="chatBox", type="messages", height=400)
|
| 53 |
state = gr.State([])
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
chat_with_openai,
|
| 62 |
-
[
|
| 63 |
-
[chatbot, state,
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 68 |
|
| 69 |
-
# JavaScript: mic + api key handling
|
| 70 |
-
gr.HTML("""
|
| 71 |
-
<script>
|
| 72 |
-
const micButton = document.getElementById("micButton");
|
| 73 |
-
const apiInput = document.querySelector("#apiKeyInput input");
|
| 74 |
-
const voiceInput = document.querySelector("#voice_input_for_voice input");
|
| 75 |
-
const keyStatus = document.getElementById("keyStatus");
|
| 76 |
-
|
| 77 |
-
// Disable mic button initially
|
| 78 |
-
micButton.disabled = true;
|
| 79 |
-
|
| 80 |
-
// Enable mic if API key is valid
|
| 81 |
-
apiInput.addEventListener("input", () => {
|
| 82 |
-
const apiKey = apiInput.value.trim();
|
| 83 |
-
if (apiKey) {
|
| 84 |
-
keyStatus.innerHTML = '<div class="key-status success">API Key saved successfully!</div>';
|
| 85 |
-
micButton.disabled = false;
|
| 86 |
-
} else {
|
| 87 |
-
keyStatus.innerHTML = '<div class="key-status error">Please enter a valid API key</div>';
|
| 88 |
-
micButton.disabled = true;
|
| 89 |
-
}
|
| 90 |
-
});
|
| 91 |
-
|
| 92 |
-
micButton.addEventListener("click", () => {
|
| 93 |
-
const apiKey = apiInput.value.trim();
|
| 94 |
-
if (!apiKey) {
|
| 95 |
-
alert("Please enter your OpenAI API key first!");
|
| 96 |
-
return;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 100 |
-
recognition.continuous = false;
|
| 101 |
-
recognition.lang = "en-US";
|
| 102 |
-
|
| 103 |
-
micButton.textContent = "π΄ Listening...";
|
| 104 |
-
micButton.disabled = true;
|
| 105 |
-
|
| 106 |
-
recognition.onresult = (event) => {
|
| 107 |
-
const transcript = event.results[0][0].transcript;
|
| 108 |
-
if (voiceInput) {
|
| 109 |
-
voiceInput.value = transcript;
|
| 110 |
-
voiceInput.dispatchEvent(new Event('input', { bubbles: true }));
|
| 111 |
-
}
|
| 112 |
-
};
|
| 113 |
-
|
| 114 |
-
recognition.onerror = (event) => {
|
| 115 |
-
console.error("Speech recognition error:", event.error);
|
| 116 |
-
alert("Speech recognition error: " + event.error);
|
| 117 |
-
micButton.textContent = "π€ Click & Speak";
|
| 118 |
-
micButton.disabled = false;
|
| 119 |
-
};
|
| 120 |
-
|
| 121 |
-
recognition.onend = () => {
|
| 122 |
-
micButton.textContent = "π€ Click & Speak";
|
| 123 |
-
micButton.disabled = false;
|
| 124 |
-
};
|
| 125 |
-
|
| 126 |
-
recognition.start();
|
| 127 |
-
});
|
| 128 |
-
|
| 129 |
-
// Auto-focus on API key input
|
| 130 |
-
apiInput.focus();
|
| 131 |
-
</script>
|
| 132 |
-
""")
|
| 133 |
-
|
| 134 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
import tempfile
|
| 5 |
|
| 6 |
system_prompt = """You are a voice bot representing Krishnavamshi Thumma. When responding to questions, answer as if you are:
|
| 7 |
- A Generative AI and Data Engineering enthusiast with 1.5+ years of experience
|
|
|
|
| 11 |
- A Computer Science graduate from Neil Gogte Institute of Technology
|
| 12 |
Answer questions about your background professionally but engagingly (2-3 sentences max)."""
|
| 13 |
|
| 14 |
+
def speech_to_text(audio):
|
| 15 |
+
recognizer = sr.Recognizer()
|
| 16 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 17 |
+
tmp_file.write(audio)
|
| 18 |
+
tmp_file.flush()
|
| 19 |
+
with sr.AudioFile(tmp_file.name) as source:
|
| 20 |
+
audio_data = recognizer.record(source)
|
| 21 |
+
try:
|
| 22 |
+
text = recognizer.recognize_google(audio_data)
|
| 23 |
+
return text
|
| 24 |
+
except sr.UnknownValueError:
|
| 25 |
+
return "β Could not understand the audio"
|
| 26 |
+
except sr.RequestError as e:
|
| 27 |
+
return f"β Speech recognition error: {e}"
|
| 28 |
+
|
| 29 |
def chat_with_openai(user_input, history, api_key):
|
| 30 |
if not api_key:
|
| 31 |
raise gr.Error("β Please enter your OpenAI API key.")
|
|
|
|
| 48 |
raise gr.Error(f"β Error: {str(e)}")
|
| 49 |
|
| 50 |
with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
|
| 51 |
+
gr.Markdown("## ποΈ Krishnavamshi Thumma - Voice Assistant (No JavaScript)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
api_key = gr.Textbox(label="π OpenAI API Key", type="password")
|
| 54 |
+
chatbot = gr.Chatbot(height=400)
|
|
|
|
| 55 |
state = gr.State([])
|
| 56 |
|
| 57 |
+
with gr.Row():
|
| 58 |
+
voice_input = gr.Audio(source="microphone", type="filepath", label="π€ Speak here")
|
| 59 |
+
transcribed_text = gr.Textbox(label="Transcribed Text")
|
| 60 |
+
|
| 61 |
+
# When audio is submitted, convert to text
|
| 62 |
+
voice_input.change(speech_to_text, voice_input, transcribed_text)
|
| 63 |
|
| 64 |
+
# When transcribed text is ready, send to OpenAI
|
| 65 |
+
transcribed_text.submit(
|
| 66 |
chat_with_openai,
|
| 67 |
+
[transcribed_text, state, api_key],
|
| 68 |
+
[chatbot, state, transcribed_text]
|
| 69 |
)
|
| 70 |
|
| 71 |
+
clear_btn = gr.Button("ποΈ Clear Chat")
|
| 72 |
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
demo.launch()
|