Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -211,90 +211,58 @@ def main():
|
|
211 |
elem_classes=["chat-area"],
|
212 |
)
|
213 |
|
214 |
-
message
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
submit = gr.Button("Send")
|
223 |
-
clear = gr.Button("Clear")
|
224 |
-
|
225 |
-
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
226 |
-
system_prompt = gr.TextArea(
|
227 |
-
value=DEFAULT_SYSTEM_PROMPT,
|
228 |
-
label="System Prompt",
|
229 |
-
lines=7,
|
230 |
-
)
|
231 |
-
temperature = gr.Slider(
|
232 |
-
minimum=0,
|
233 |
-
maximum=1,
|
234 |
-
step=0.1,
|
235 |
-
value=0,
|
236 |
-
label="Temperature",
|
237 |
-
)
|
238 |
-
max_tokens = gr.Slider(
|
239 |
-
minimum=128,
|
240 |
-
maximum=32000,
|
241 |
-
step=128,
|
242 |
-
value=8192,
|
243 |
-
label="Max Tokens",
|
244 |
)
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
minimum=1,
|
254 |
-
maximum=100,
|
255 |
-
step=1,
|
256 |
-
value=40,
|
257 |
-
label="Top-k",
|
258 |
-
)
|
259 |
-
penalty = gr.Slider(
|
260 |
-
minimum=1.0,
|
261 |
-
maximum=2.0,
|
262 |
-
step=0.1,
|
263 |
-
value=1.2,
|
264 |
-
label="Repetition Penalty",
|
265 |
-
)
|
266 |
-
|
267 |
-
examples = gr.Examples(
|
268 |
-
examples=create_examples(),
|
269 |
-
inputs=[message],
|
270 |
-
outputs=[chat_history, chat_display],
|
271 |
-
fn=process_example,
|
272 |
-
cache_examples=False,
|
273 |
-
)
|
274 |
-
|
275 |
-
# JavaScript to handle Enter key submission and RTL direction
|
276 |
js_handler = """
|
277 |
-
function
|
278 |
-
textArea.
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
|
|
|
|
|
|
|
|
288 |
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
295 |
}
|
|
|
|
|
|
|
|
|
|
|
296 |
"""
|
297 |
-
|
298 |
# Set up event handlers
|
299 |
submit_click = submit.click(
|
300 |
chat_response,
|
@@ -316,8 +284,8 @@ def main():
|
|
316 |
# Clear input after submission
|
317 |
submit_click.then(lambda: "", outputs=message)
|
318 |
|
319 |
-
# Add JavaScript
|
320 |
-
demo.load(None,
|
321 |
|
322 |
clear.click(
|
323 |
lambda: ([], ""),
|
|
|
211 |
elem_classes=["chat-area"],
|
212 |
)
|
213 |
|
214 |
+
# Wrap message input and submit button in a form
|
215 |
+
with gr.Form(elem_id="chat-form"):
|
216 |
+
message = gr.TextArea(
|
217 |
+
placeholder=PLACEHOLDER,
|
218 |
+
label="Your message",
|
219 |
+
lines=3,
|
220 |
+
elem_classes=["message-input"],
|
221 |
+
autofocus=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
)
|
223 |
+
|
224 |
+
with gr.Row():
|
225 |
+
submit = gr.Button("Send", elem_id="submit-btn")
|
226 |
+
clear = gr.Button("Clear")
|
227 |
+
|
228 |
+
# [Rest of the UI components remain the same]
|
229 |
+
|
230 |
+
# Updated JavaScript handler for Enter key and RTL support
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
js_handler = """
|
232 |
+
function setupKeyboardHandler() {
|
233 |
+
const textArea = document.querySelector('.message-input textarea');
|
234 |
+
const form = document.getElementById('chat-form');
|
235 |
+
|
236 |
+
if (textArea && form) {
|
237 |
+
textArea.addEventListener('keydown', function(e) {
|
238 |
+
// Check for Arabic characters
|
239 |
+
const containsArabic = /[\u0600-\u06FF]/.test(this.value);
|
240 |
+
|
241 |
+
// Apply RTL class for Arabic text
|
242 |
+
if (containsArabic) {
|
243 |
+
this.classList.add('arabic-input');
|
244 |
+
} else {
|
245 |
+
this.classList.remove('arabic-input');
|
246 |
+
}
|
247 |
|
248 |
+
// Submit on Enter without Shift
|
249 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
250 |
+
e.preventDefault();
|
251 |
+
const submitBtn = document.getElementById('submit-btn');
|
252 |
+
if (submitBtn) {
|
253 |
+
submitBtn.click();
|
254 |
+
}
|
255 |
+
}
|
256 |
+
});
|
257 |
+
}
|
258 |
}
|
259 |
+
|
260 |
+
// Run setup when the page loads
|
261 |
+
document.addEventListener('DOMContentLoaded', setupKeyboardHandler);
|
262 |
+
// Also run setup after Gradio refreshes the UI
|
263 |
+
document.addEventListener('gradio-loaded', setupKeyboardHandler);
|
264 |
"""
|
265 |
+
|
266 |
# Set up event handlers
|
267 |
submit_click = submit.click(
|
268 |
chat_response,
|
|
|
284 |
# Clear input after submission
|
285 |
submit_click.then(lambda: "", outputs=message)
|
286 |
|
287 |
+
# Add JavaScript handlers
|
288 |
+
demo.load(None, _js=js_handler)
|
289 |
|
290 |
clear.click(
|
291 |
lambda: ([], ""),
|