Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
elem_id = "chat_input"
|
3 |
+
js_script = f"""
|
4 |
+
function() {{
|
5 |
+
const ta = document.querySelector(`#{elem_id} > label > textarea`);
|
6 |
+
if (ta) {{
|
7 |
+
ta.addEventListener('keydown', (e) => {{
|
8 |
+
if (e.key === 'Enter' && !e.shiftKey) {{
|
9 |
+
e.preventDefault();
|
10 |
+
}}
|
11 |
+
}});
|
12 |
+
}}
|
13 |
+
}}
|
14 |
+
"""
|
15 |
+
with gr.Blocks() as demo:
|
16 |
+
chatbot = gr.Chatbot(label="Chatbot")
|
17 |
+
|
18 |
+
with gr.Row():
|
19 |
+
msg = gr.MultimodalTextbox(
|
20 |
+
submit_btn=False,
|
21 |
+
elem_id=elem_id,
|
22 |
+
)
|
23 |
+
submit_button = gr.Button("Submit")
|
24 |
+
|
25 |
+
# A single, robust function to handle both user and bot turns
|
26 |
+
def respond(user_message, chat_history):
|
27 |
+
|
28 |
+
return {"text": "", "files": []}, chat_history
|
29 |
+
|
30 |
+
def newline(msg):
|
31 |
+
msg["text"] = msg.get("text", "") + "\n"
|
32 |
+
return msg
|
33 |
+
|
34 |
+
msg.submit(newline, inputs=msg, outputs=msg)
|
35 |
+
submit_button.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
36 |
+
demo.load(js=js_script)
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|