Spaces:
Sleeping
Sleeping
Update ui.py
Browse files
ui.py
CHANGED
|
@@ -1,66 +1,37 @@
|
|
| 1 |
-
# ui.py - FINAL FIXED VERSION
|
| 2 |
import gradio as gr
|
| 3 |
-
import asyncio
|
| 4 |
|
| 5 |
def create_ui(generate_fn):
|
| 6 |
"""
|
| 7 |
-
Build a Blocks-based chat UI.
|
| 8 |
-
|
|
|
|
| 9 |
"""
|
| 10 |
with gr.Blocks(css="""
|
| 11 |
body { background-color: #0b0f19; color: #e0e0e0; }
|
| 12 |
.gradio-container { font-family: 'Inter', sans-serif; max-width: 900px !important; margin: auto; }
|
| 13 |
footer { visibility: hidden; display: none; }
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
gr.Markdown("## Nexari AI — Research Backend")
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
chatbot = gr.Chatbot(
|
| 20 |
-
elem_id="nexari_chatbot",
|
| 21 |
-
label="Nexari G1",
|
| 22 |
-
height=600,
|
| 23 |
-
bubble_full_width=False
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
txt = gr.Textbox(
|
| 27 |
-
show_label=False,
|
| 28 |
-
placeholder="Type your message and press Enter...",
|
| 29 |
-
container=False
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
# State to hold conversation history
|
| 33 |
state = gr.State([])
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
yield history, ""
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
# Prepare messages list for the backend
|
| 41 |
messages = []
|
| 42 |
for human, ai in history:
|
| 43 |
messages.append({"role": "user", "content": human})
|
| 44 |
messages.append({"role": "assistant", "content": ai})
|
| 45 |
messages.append({"role": "user", "content": message})
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Consume the generator from app.py
|
| 52 |
-
# FIX: Used async for loop correctly without illegal return
|
| 53 |
-
partial_response = ""
|
| 54 |
-
async for chunk in generate_fn(messages):
|
| 55 |
-
partial_response = chunk
|
| 56 |
-
history[-1] = (message, partial_response)
|
| 57 |
-
yield history, ""
|
| 58 |
-
|
| 59 |
-
# Generator ends naturally here. No return statement needed with value.
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
send = gr.Button("Send")
|
| 64 |
-
send.click(submit, [txt,
|
| 65 |
-
|
| 66 |
return demo
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
def create_ui(generate_fn):
|
| 4 |
"""
|
| 5 |
+
Build a Blocks-based chat UI that returns a gr.Blocks object.
|
| 6 |
+
generate_fn receives a list of messages (user/assistant pairs as dicts)
|
| 7 |
+
and should return a reply string (this wrapper uses it as a placeholder).
|
| 8 |
"""
|
| 9 |
with gr.Blocks(css="""
|
| 10 |
body { background-color: #0b0f19; color: #e0e0e0; }
|
| 11 |
.gradio-container { font-family: 'Inter', sans-serif; max-width: 900px !important; margin: auto; }
|
| 12 |
footer { visibility: hidden; display: none; }
|
| 13 |
+
""") as demo:
|
|
|
|
| 14 |
gr.Markdown("## Nexari AI — Research Backend")
|
| 15 |
+
chatbot = gr.Chatbot(elem_id="nexari_chatbot", label="Nexari").style(height=500)
|
| 16 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message and press Enter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
state = gr.State([])
|
| 18 |
|
| 19 |
+
def submit(message, history):
|
| 20 |
+
# history is list of (human, ai) pairs in Gradio; convert to messages
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
messages = []
|
| 22 |
for human, ai in history:
|
| 23 |
messages.append({"role": "user", "content": human})
|
| 24 |
messages.append({"role": "assistant", "content": ai})
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
+
# call generator wrapper (synchronous placeholder)
|
| 27 |
+
reply = generate_fn(messages)
|
| 28 |
+
# append to history
|
| 29 |
+
history = history + [(message, reply)]
|
| 30 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
txt.submit(submit, [txt, state], [chatbot, txt], queue=False)
|
| 33 |
+
# Also allow clicking a send button
|
| 34 |
send = gr.Button("Send")
|
| 35 |
+
send.click(submit, [txt, state], [chatbot, txt], queue=False)
|
| 36 |
+
|
| 37 |
return demo
|