Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
|
7 |
|
8 |
def respond(
|
@@ -24,6 +26,7 @@ def respond(
|
|
24 |
messages.append({"role": "user", "content": message})
|
25 |
|
26 |
response = ""
|
|
|
27 |
for message in client.chat_completion(
|
28 |
messages,
|
29 |
max_tokens=max_tokens,
|
@@ -32,124 +35,30 @@ def respond(
|
|
32 |
top_p=top_p,
|
33 |
):
|
34 |
token = message.choices[0].delta.content
|
|
|
35 |
response += token
|
36 |
yield response
|
37 |
|
38 |
|
39 |
-
# Custom HTML and JS for mic button and speech-to-text
|
40 |
-
mic_html = """
|
41 |
-
<div class="mic-container">
|
42 |
-
<button id="mic_button" title="Click to speak">
|
43 |
-
<span class="material-symbols-outlined">mic</span>
|
44 |
-
</button>
|
45 |
-
<input id="user_input" type="text" placeholder="Type your message here..." />
|
46 |
-
</div>
|
47 |
-
<div id="status" class="status"></div>
|
48 |
-
<style>
|
49 |
-
.mic-container {
|
50 |
-
display: flex;
|
51 |
-
align-items: center;
|
52 |
-
max-width: 500px;
|
53 |
-
margin: 20px auto;
|
54 |
-
padding: 10px;
|
55 |
-
background: #f5f5f5;
|
56 |
-
border-radius: 8px;
|
57 |
-
}
|
58 |
-
#mic_button {
|
59 |
-
font-family: 'Material Symbols Outlined';
|
60 |
-
font-size: 24px;
|
61 |
-
color: #555;
|
62 |
-
border: none;
|
63 |
-
background: none;
|
64 |
-
cursor: pointer;
|
65 |
-
padding: 4px;
|
66 |
-
margin-right: 8px;
|
67 |
-
}
|
68 |
-
#user_input {
|
69 |
-
width: 100%;
|
70 |
-
padding: 8px;
|
71 |
-
font-size: 16px;
|
72 |
-
border: 1px solid #ccc;
|
73 |
-
border-radius: 4px;
|
74 |
-
}
|
75 |
-
.status {
|
76 |
-
margin-top: 20px;
|
77 |
-
text-align: center;
|
78 |
-
color: #666;
|
79 |
-
}
|
80 |
-
</style>
|
81 |
-
<script>
|
82 |
-
let micButton = document.getElementById('mic_button');
|
83 |
-
let userInputBox = document.getElementById('user_input');
|
84 |
-
let statusDiv = document.getElementById('status');
|
85 |
-
let recognition = null;
|
86 |
-
|
87 |
-
if ('webkitSpeechRecognition' in window) {
|
88 |
-
recognition = new webkitSpeechRecognition();
|
89 |
-
recognition.continuous = false;
|
90 |
-
recognition.interimResults = false;
|
91 |
-
recognition.lang = 'en-US';
|
92 |
-
|
93 |
-
recognition.onstart = function() {
|
94 |
-
statusDiv.textContent = "Listening...";
|
95 |
-
micButton.style.color = '#4CAF50';
|
96 |
-
};
|
97 |
-
|
98 |
-
recognition.onresult = function(event) {
|
99 |
-
const transcript = event.results[0][0].transcript;
|
100 |
-
userInputBox.value = transcript;
|
101 |
-
statusDiv.textContent = "Done listening.";
|
102 |
-
micButton.style.color = '#555';
|
103 |
-
};
|
104 |
-
|
105 |
-
recognition.onerror = function(event) {
|
106 |
-
console.error('Speech recognition error:', event.error);
|
107 |
-
statusDiv.textContent = "Error: " + event.error;
|
108 |
-
micButton.style.color = '#555';
|
109 |
-
};
|
110 |
-
|
111 |
-
recognition.onspeechend = function() {
|
112 |
-
recognition.stop();
|
113 |
-
statusDiv.textContent = "Stopped listening.";
|
114 |
-
micButton.style.color = '#555';
|
115 |
-
};
|
116 |
-
|
117 |
-
micButton.onclick = function() {
|
118 |
-
try {
|
119 |
-
recognition.start();
|
120 |
-
} catch(err) {
|
121 |
-
console.error('Recognition already started:', err);
|
122 |
-
recognition.stop();
|
123 |
-
}
|
124 |
-
};
|
125 |
-
} else {
|
126 |
-
micButton.onclick = function() {
|
127 |
-
alert("Speech recognition is not supported in this browser.");
|
128 |
-
statusDiv.textContent = "Speech recognition not supported";
|
129 |
-
};
|
130 |
-
statusDiv.textContent = "Speech recognition not available";
|
131 |
-
}
|
132 |
-
</script>
|
133 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
-
# Gradio Chat Interface with Mic Button
|
136 |
-
with gr.Blocks() as demo:
|
137 |
-
gr.HTML(mic_html)
|
138 |
-
chatbot = gr.ChatInterface(
|
139 |
-
fn=respond,
|
140 |
-
additional_inputs=[
|
141 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
142 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
143 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
144 |
-
gr.Slider(
|
145 |
-
minimum=0.1,
|
146 |
-
maximum=1.0,
|
147 |
-
value=0.95,
|
148 |
-
step=0.05,
|
149 |
-
label="Top-p (nucleus sampling)",
|
150 |
-
),
|
151 |
-
],
|
152 |
-
)
|
153 |
|
154 |
if __name__ == "__main__":
|
155 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
8 |
|
9 |
|
10 |
def respond(
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
29 |
+
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
+
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
+
"""
|
46 |
+
demo = gr.ChatInterface(
|
47 |
+
respond,
|
48 |
+
additional_inputs=[
|
49 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
+
gr.Slider(
|
53 |
+
minimum=0.1,
|
54 |
+
maximum=1.0,
|
55 |
+
value=0.95,
|
56 |
+
step=0.05,
|
57 |
+
label="Top-p (nucleus sampling)",
|
58 |
+
),
|
59 |
+
],
|
60 |
+
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|