Spaces:
Paused
Paused
Update ui_layout.py
Browse files- ui_layout.py +16 -10
ui_layout.py
CHANGED
|
@@ -3,32 +3,38 @@ import gradio as gr
|
|
| 3 |
def create_ui(process_fn):
|
| 4 |
"""
|
| 5 |
The AI can rewrite this function to change the UI.
|
| 6 |
-
|
| 7 |
-
It MUST attach the 'process_fn' to the chat interface.
|
| 8 |
"""
|
| 9 |
-
with gr.Blocks(
|
| 10 |
gr.Markdown("# 🛡️ Self-Modifying Isolated AI")
|
| 11 |
gr.Markdown("I run on CPU. I can install tools and change this UI.")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
msg = gr.Textbox(label="Command or Chat")
|
| 17 |
clear = gr.Button("Clear")
|
| 18 |
|
| 19 |
-
# Standard Chat Interface Logic
|
| 20 |
def user(user_message, history):
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
def bot(history):
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
bot_message = process_fn(user_message)
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
return history
|
| 28 |
|
| 29 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 30 |
bot, chatbot, chatbot
|
| 31 |
)
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
return demo
|
|
|
|
| 3 |
def create_ui(process_fn):
|
| 4 |
"""
|
| 5 |
The AI can rewrite this function to change the UI.
|
| 6 |
+
Updated for Gradio 5+ (Messages format).
|
|
|
|
| 7 |
"""
|
| 8 |
+
with gr.Blocks() as demo:
|
| 9 |
gr.Markdown("# 🛡️ Self-Modifying Isolated AI")
|
| 10 |
gr.Markdown("I run on CPU. I can install tools and change this UI.")
|
| 11 |
|
| 12 |
+
# type="messages" is REQUIRED for Gradio 5
|
| 13 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
| 14 |
|
| 15 |
msg = gr.Textbox(label="Command or Chat")
|
| 16 |
clear = gr.Button("Clear")
|
| 17 |
|
|
|
|
| 18 |
def user(user_message, history):
|
| 19 |
+
# Append user message in the new dictionary format
|
| 20 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 21 |
|
| 22 |
def bot(history):
|
| 23 |
+
# Get the content of the last message (User's message)
|
| 24 |
+
user_message = history[-1]["content"]
|
| 25 |
+
|
| 26 |
+
# Call the AI process function
|
| 27 |
bot_message = process_fn(user_message)
|
| 28 |
+
|
| 29 |
+
# Append the assistant's response
|
| 30 |
+
history.append({"role": "assistant", "content": bot_message})
|
| 31 |
return history
|
| 32 |
|
| 33 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 34 |
bot, chatbot, chatbot
|
| 35 |
)
|
| 36 |
+
|
| 37 |
+
# Clear button resets history to an empty list
|
| 38 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
| 39 |
|
| 40 |
return demo
|