Update app.py
Browse files
app.py
CHANGED
|
@@ -79,24 +79,28 @@ def files_to_prompt(files, current: str) -> str:
|
|
| 79 |
# ββ Chat ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
|
| 81 |
def _bot_stream(history: list, system_prompt: str, max_tokens: int):
|
| 82 |
-
"""Generator: yields updated history list with streaming assistant response.
|
|
|
|
|
|
|
| 83 |
if not history:
|
| 84 |
return
|
| 85 |
|
| 86 |
if not HF_TOKEN:
|
| 87 |
-
history[-1][
|
| 88 |
yield history
|
| 89 |
return
|
| 90 |
|
| 91 |
-
|
| 92 |
-
msgs = [{"role": "system", "content":
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
client = InferenceClient(provider="novita", api_key=HF_TOKEN)
|
|
|
|
| 100 |
try:
|
| 101 |
for chunk in client.chat.completions.create(
|
| 102 |
model=MODEL_ID,
|
|
@@ -105,19 +109,16 @@ def _bot_stream(history: list, system_prompt: str, max_tokens: int):
|
|
| 105 |
stream=True,
|
| 106 |
):
|
| 107 |
delta = chunk.choices[0].delta.content or ""
|
| 108 |
-
history[-1][
|
| 109 |
yield history
|
| 110 |
except Exception as e:
|
| 111 |
-
history[-1][
|
| 112 |
yield history
|
| 113 |
|
| 114 |
|
| 115 |
def _user_submit(msg: str, history: list):
|
| 116 |
"""Add user message + empty assistant placeholder to history."""
|
| 117 |
-
history = history + [
|
| 118 |
-
{"role": "user", "content": msg},
|
| 119 |
-
{"role": "assistant", "content": ""},
|
| 120 |
-
]
|
| 121 |
return "", history
|
| 122 |
|
| 123 |
# ββ Activate Agent ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -548,8 +549,7 @@ button.secondary:hover {
|
|
| 548 |
.message-bubble-border { border-color: var(--border2) !important; }
|
| 549 |
|
| 550 |
/* ββ Accordion ββ */
|
| 551 |
-
.accordion > .label-wrap {
|
| 552 |
-
background: var(--card2) !important;
|
| 553 |
border-color: var(--border) !important;
|
| 554 |
color: var(--text) !important;
|
| 555 |
font-family: var(--font-head) !important;
|
|
@@ -590,7 +590,7 @@ footer { display: none !important; }
|
|
| 590 |
|
| 591 |
# ββ Build UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 592 |
|
| 593 |
-
with gr.Blocks(title="AgentForge β‘"
|
| 594 |
|
| 595 |
# ββ Global state ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 596 |
system_prompt_state = gr.State("")
|
|
@@ -680,16 +680,8 @@ with gr.Blocks(title="AgentForge β‘", head=JS_HEAD, theme=gr.themes.Base()) as
|
|
| 680 |
|
| 681 |
chatbot = gr.Chatbot(
|
| 682 |
height=430,
|
| 683 |
-
type="messages",
|
| 684 |
show_label=False,
|
| 685 |
-
|
| 686 |
-
"<div style='text-align:center;color:#444;padding:40px;font-family:\"Chakra Petch\",sans-serif;'>"
|
| 687 |
-
"<div style='font-size:2.5rem;margin-bottom:12px;'>β‘</div>"
|
| 688 |
-
"<div style='font-weight:700;font-size:.9rem;color:#666;margin-bottom:6px;'>"
|
| 689 |
-
"Forge your agent, then start chatting.</div>"
|
| 690 |
-
"<div style='font-size:.78rem;color:#444;'>"
|
| 691 |
-
"Drop a .md or .skill file β activate β chat.</div></div>"
|
| 692 |
-
),
|
| 693 |
)
|
| 694 |
|
| 695 |
with gr.Row():
|
|
@@ -781,10 +773,7 @@ from community-forged prompts.
|
|
| 781 |
def _submit(msg, hist):
|
| 782 |
if not msg.strip():
|
| 783 |
return gr.update(), hist
|
| 784 |
-
return "", hist + [
|
| 785 |
-
{"role": "user", "content": msg},
|
| 786 |
-
{"role": "assistant", "content": ""},
|
| 787 |
-
]
|
| 788 |
|
| 789 |
msg_box.submit(
|
| 790 |
fn=_submit,
|
|
@@ -824,4 +813,7 @@ from community-forged prompts.
|
|
| 824 |
|
| 825 |
|
| 826 |
if __name__ == "__main__":
|
| 827 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# ββ Chat ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
|
| 81 |
def _bot_stream(history: list, system_prompt: str, max_tokens: int):
|
| 82 |
+
"""Generator: yields updated history list with streaming assistant response.
|
| 83 |
+
History format: [[user_msg, bot_msg], ...] β Gradio 6 tuple style.
|
| 84 |
+
"""
|
| 85 |
if not history:
|
| 86 |
return
|
| 87 |
|
| 88 |
if not HF_TOKEN:
|
| 89 |
+
history[-1][1] = "β οΈ No HF_TOKEN found. Add it in Settings β Secrets."
|
| 90 |
yield history
|
| 91 |
return
|
| 92 |
|
| 93 |
+
sys_prompt = system_prompt.strip() or "You are a helpful assistant."
|
| 94 |
+
msgs = [{"role": "system", "content": sys_prompt}]
|
| 95 |
+
# Build message list from all complete turns + current user message
|
| 96 |
+
for user_msg, bot_msg in history[:-1]:
|
| 97 |
+
msgs.append({"role": "user", "content": user_msg or ""})
|
| 98 |
+
msgs.append({"role": "assistant", "content": bot_msg or ""})
|
| 99 |
+
# Add the pending user message (bot slot is still None)
|
| 100 |
+
msgs.append({"role": "user", "content": history[-1][0] or ""})
|
| 101 |
|
| 102 |
client = InferenceClient(provider="novita", api_key=HF_TOKEN)
|
| 103 |
+
history[-1][1] = ""
|
| 104 |
try:
|
| 105 |
for chunk in client.chat.completions.create(
|
| 106 |
model=MODEL_ID,
|
|
|
|
| 109 |
stream=True,
|
| 110 |
):
|
| 111 |
delta = chunk.choices[0].delta.content or ""
|
| 112 |
+
history[-1][1] += delta
|
| 113 |
yield history
|
| 114 |
except Exception as e:
|
| 115 |
+
history[-1][1] += f"\n\nβ οΈ Error: {e}"
|
| 116 |
yield history
|
| 117 |
|
| 118 |
|
| 119 |
def _user_submit(msg: str, history: list):
|
| 120 |
"""Add user message + empty assistant placeholder to history."""
|
| 121 |
+
history = history + [[msg, None]]
|
|
|
|
|
|
|
|
|
|
| 122 |
return "", history
|
| 123 |
|
| 124 |
# ββ Activate Agent ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 549 |
.message-bubble-border { border-color: var(--border2) !important; }
|
| 550 |
|
| 551 |
/* ββ Accordion ββ */
|
| 552 |
+
.accordion > .label-wrap {Acard2) !important;
|
|
|
|
| 553 |
border-color: var(--border) !important;
|
| 554 |
color: var(--text) !important;
|
| 555 |
font-family: var(--font-head) !important;
|
|
|
|
| 590 |
|
| 591 |
# ββ Build UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 592 |
|
| 593 |
+
with gr.Blocks(title="AgentForge β‘") as demo:
|
| 594 |
|
| 595 |
# ββ Global state ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 596 |
system_prompt_state = gr.State("")
|
|
|
|
| 680 |
|
| 681 |
chatbot = gr.Chatbot(
|
| 682 |
height=430,
|
|
|
|
| 683 |
show_label=False,
|
| 684 |
+
bubble_full_width=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
)
|
| 686 |
|
| 687 |
with gr.Row():
|
|
|
|
| 773 |
def _submit(msg, hist):
|
| 774 |
if not msg.strip():
|
| 775 |
return gr.update(), hist
|
| 776 |
+
return "", hist + [[msg, None]]
|
|
|
|
|
|
|
|
|
|
| 777 |
|
| 778 |
msg_box.submit(
|
| 779 |
fn=_submit,
|
|
|
|
| 813 |
|
| 814 |
|
| 815 |
if __name__ == "__main__":
|
| 816 |
+
demo.launch(
|
| 817 |
+
css=CSS,
|
| 818 |
+
head=JS_HEAD,
|
| 819 |
+
)
|