Spaces:
Paused
Paused
Swift 1.5 Flash Next chat front end (orange, same template as Swift Bonsai 2)
Browse files- README.md +18 -7
- app.py +184 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title: Swift Flash Next
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.28.0
|
| 8 |
-
python_version:
|
| 9 |
app_file: app.py
|
| 10 |
-
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Swift 1.5 Flash Next
|
| 3 |
+
emoji: 🔥
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.28.0
|
| 8 |
+
python_version: "3.12"
|
| 9 |
app_file: app.py
|
| 10 |
+
short_description: Qwen3.8-Flash-Next that thinks 63% less, within 1 point
|
| 11 |
+
license: other
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Swift 1.5 Flash Next
|
| 15 |
+
|
| 16 |
+
Chat demo for Swift 1.5 Qwen3.8-Flash-Next, UkisAI's reasoning-efficient
|
| 17 |
+
fine-tune of [Qwen3.8-Flash-Next](https://huggingface.co/Qwen/Qwen3.8-Flash-Next). It uses 63.4% fewer thinking tokens while staying
|
| 18 |
+
within 1 point of the base.
|
| 19 |
+
|
| 20 |
+
The model runs in full BF16 on a UkisAI GPU server (8× H100, vLLM); this Space is the chat front end, with streaming reasoning
|
| 21 |
+
and adjustable reasoning effort.
|
| 22 |
+
|
| 23 |
+
- All Flash Next builds: [Swift Flash Next collection](https://huggingface.co/collections/ukisai/swift-flash-next-6ab272f5ea792e8e85728dfb)
|
| 24 |
+
- API: OpenAI-compatible at `https://ukisai.com/api/flash-next/v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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import httpx
|
| 6 |
+
from openai import APIConnectionError, APIStatusError, OpenAI
|
| 7 |
+
|
| 8 |
+
# The model runs on UkisAI's GPU server (vLLM, 8x H100), reached through the public
|
| 9 |
+
# ukisai.com/api/flash-next proxy; this Space is the chat front end.
|
| 10 |
+
MODEL = "flash-next"
|
| 11 |
+
CONCURRENCY = 8
|
| 12 |
+
|
| 13 |
+
client = OpenAI(
|
| 14 |
+
base_url=os.environ.get("FLASH_NEXT_API_URL", "https://ukisai.com/api/flash-next/v1"),
|
| 15 |
+
api_key=os.environ.get("FLASH_NEXT_API_KEY") or "none", # the public proxy needs no key
|
| 16 |
+
http_client=httpx.Client(timeout=httpx.Timeout(900, connect=10)),
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def text_of(content):
|
| 21 |
+
"""Pull the plain text out of any Gradio message content shape."""
|
| 22 |
+
if content is None:
|
| 23 |
+
return ""
|
| 24 |
+
if isinstance(content, str):
|
| 25 |
+
return content
|
| 26 |
+
if isinstance(content, (list, tuple)):
|
| 27 |
+
return "\n".join(filter(None, (text_of(item) for item in content)))
|
| 28 |
+
if isinstance(content, dict):
|
| 29 |
+
return content.get("text") or ""
|
| 30 |
+
return ""
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def build_messages(message, history, system_prompt=""):
|
| 34 |
+
"""Rebuild the conversation, leaving out earlier reasoning bubbles."""
|
| 35 |
+
messages = [{"role": "system", "content": system_prompt.strip()}] if system_prompt.strip() else []
|
| 36 |
+
for turn in history:
|
| 37 |
+
if (turn.get("metadata") or {}).get("title"):
|
| 38 |
+
continue # a "Thought for …" bubble, not part of the reply
|
| 39 |
+
text = text_of(turn.get("content")).strip()
|
| 40 |
+
if not text:
|
| 41 |
+
continue
|
| 42 |
+
if messages and messages[-1]["role"] == turn["role"]:
|
| 43 |
+
messages[-1]["content"] += "\n\n" + text
|
| 44 |
+
else:
|
| 45 |
+
messages.append({"role": turn["role"], "content": text})
|
| 46 |
+
messages.append({"role": "user", "content": text_of(message)})
|
| 47 |
+
return messages
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def chat(message, history, reasoning_effort, max_new_tokens, temperature, top_p, top_k, min_p, system_prompt):
|
| 51 |
+
thinking = reasoning_effort != "off"
|
| 52 |
+
template_kwargs = {"enable_thinking": True, "reasoning_effort": reasoning_effort} if thinking else {"enable_thinking": False}
|
| 53 |
+
try:
|
| 54 |
+
stream = client.chat.completions.create(
|
| 55 |
+
model=MODEL,
|
| 56 |
+
messages=build_messages(message, history, system_prompt),
|
| 57 |
+
stream=True,
|
| 58 |
+
max_tokens=int(max_new_tokens),
|
| 59 |
+
temperature=float(temperature),
|
| 60 |
+
top_p=float(top_p),
|
| 61 |
+
# continuous_usage_stats: vLLM reports the running token count on every chunk.
|
| 62 |
+
stream_options={"include_usage": True, "continuous_usage_stats": True},
|
| 63 |
+
extra_body={"top_k": int(top_k), "min_p": float(min_p), "chat_template_kwargs": template_kwargs},
|
| 64 |
+
)
|
| 65 |
+
started = time.perf_counter()
|
| 66 |
+
reasoning, answer, think_tokens, think_seconds, finish_reason = "", "", 0, None, None
|
| 67 |
+
for chunk in stream:
|
| 68 |
+
if not chunk.choices:
|
| 69 |
+
continue
|
| 70 |
+
choice = chunk.choices[0]
|
| 71 |
+
finish_reason = choice.finish_reason or finish_reason
|
| 72 |
+
reasoning_delta = getattr(choice.delta, "reasoning", None) or getattr(choice.delta, "reasoning_content", None)
|
| 73 |
+
if reasoning_delta:
|
| 74 |
+
reasoning += reasoning_delta
|
| 75 |
+
if chunk.usage:
|
| 76 |
+
think_tokens = chunk.usage.completion_tokens # MTP can emit several tokens per chunk
|
| 77 |
+
if choice.delta.content:
|
| 78 |
+
if think_seconds is None:
|
| 79 |
+
think_seconds = time.perf_counter() - started
|
| 80 |
+
answer += choice.delta.content
|
| 81 |
+
if not thinking:
|
| 82 |
+
yield answer
|
| 83 |
+
elif reasoning or answer:
|
| 84 |
+
yield thinking_reply(reasoning, answer, think_tokens, think_seconds)
|
| 85 |
+
except APIConnectionError:
|
| 86 |
+
raise gr.Error("The Swift Flash Next server is offline right now. Please try again later.")
|
| 87 |
+
except APIStatusError as error:
|
| 88 |
+
if error.status_code == 429:
|
| 89 |
+
raise gr.Error("Swift Flash Next is busy right now. Please wait a minute and try again.")
|
| 90 |
+
raise gr.Error(f"The Swift Flash Next server returned an error ({error.status_code}). Please try again.")
|
| 91 |
+
|
| 92 |
+
if thinking and finish_reason == "length" and not answer.strip():
|
| 93 |
+
yield thinking_reply(
|
| 94 |
+
reasoning,
|
| 95 |
+
"_Swift hit the output limit while still thinking. "
|
| 96 |
+
"Raise **Max new tokens** or lower **Reasoning effort** and try again._",
|
| 97 |
+
think_tokens,
|
| 98 |
+
time.perf_counter() - started,
|
| 99 |
+
cut_off=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def thinking_reply(reasoning, answer, think_tokens, think_seconds, cut_off=False):
|
| 104 |
+
if think_seconds is None:
|
| 105 |
+
metadata = {"title": "Thinking…", "status": "pending"}
|
| 106 |
+
else:
|
| 107 |
+
verb = "Stopped thinking after" if cut_off else "Thought for"
|
| 108 |
+
metadata = {"title": f"{verb} {think_tokens:,} tokens", "status": "done", "duration": think_seconds}
|
| 109 |
+
reply = [gr.ChatMessage(role="assistant", content=reasoning.strip(), metadata=metadata)]
|
| 110 |
+
if answer.strip():
|
| 111 |
+
reply.append(gr.ChatMessage(role="assistant", content=answer.strip()))
|
| 112 |
+
return reply
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
HEADER = """
|
| 116 |
+
<div class="flash-header">
|
| 117 |
+
<h1>Swift 1.5 Flash Next</h1>
|
| 118 |
+
<p>UkisAI's reasoning-efficient <a href="https://huggingface.co/Qwen/Qwen3.8-Flash-Next">Qwen3.8-Flash-Next</a> fine-tune:
|
| 119 |
+
<strong>63% fewer thinking tokens</strong>, within 1 point of the base. This demo runs the full BF16 weights.</p>
|
| 120 |
+
<p class="links">
|
| 121 |
+
<a href="https://huggingface.co/collections/ukisai/swift-flash-next-6ab272f5ea792e8e85728dfb">Swift Flash Next collection</a> ·
|
| 122 |
+
<a href="https://ukisai.com/products/swift">ukisai.com</a>
|
| 123 |
+
</p>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
FOOTER = """
|
| 128 |
+
<p class="flash-footer">
|
| 129 |
+
Also served free for research through an OpenAI-compatible API at <code>https://ukisai.com/api/flash-next/v1</code> (model <code>flash-next</code>).
|
| 130 |
+
Weights are under the Swift Open License v1.0,
|
| 131 |
+
built on Qwen3.8-Flash-Next under the Qwen Community License.
|
| 132 |
+
</p>
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
CSS = """
|
| 136 |
+
.flash-header { text-align: center; max-width: 720px; margin: 0 auto; }
|
| 137 |
+
.flash-header h1 { margin-bottom: 0.25em; }
|
| 138 |
+
.flash-header .links, .flash-footer { font-size: 0.9em; opacity: 0.8; }
|
| 139 |
+
.flash-footer { text-align: center; margin: 0.5em auto 0; max-width: 720px; }
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
with gr.Blocks(title="Swift 1.5 Flash Next", fill_height=True) as demo:
|
| 143 |
+
gr.HTML(HEADER)
|
| 144 |
+
gr.ChatInterface(
|
| 145 |
+
chat,
|
| 146 |
+
textbox=gr.Textbox(placeholder="Ask Swift anything…"),
|
| 147 |
+
chatbot=gr.Chatbot(height=560, placeholder="Swift thinks before it answers. Its reasoning shows up in a collapsible bubble."),
|
| 148 |
+
# render=False: created inside gr.Blocks they would otherwise render above the chat
|
| 149 |
+
# instead of inside the Settings accordion.
|
| 150 |
+
additional_inputs=[
|
| 151 |
+
gr.Radio(
|
| 152 |
+
["xhigh", "medium", "low", "off"],
|
| 153 |
+
value="xhigh",
|
| 154 |
+
label="Reasoning effort",
|
| 155 |
+
info="xhigh is what the published benchmarks use. off skips thinking entirely.",
|
| 156 |
+
render=False,
|
| 157 |
+
),
|
| 158 |
+
gr.Slider(512, 65536, value=32768, step=512, label="Max new tokens", info="Up to 64k for reasoning plus answer.", render=False),
|
| 159 |
+
gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="Temperature", info="1.0 is the recommended setting. Lower is more deterministic.", render=False),
|
| 160 |
+
gr.Slider(0.05, 1.0, value=0.95, step=0.01, label="Top-p", render=False),
|
| 161 |
+
gr.Slider(0, 100, value=20, step=1, label="Top-k", info="0 turns it off.", render=False),
|
| 162 |
+
gr.Slider(0.0, 0.5, value=0.0, step=0.01, label="Min-p", render=False),
|
| 163 |
+
gr.Textbox(
|
| 164 |
+
value="",
|
| 165 |
+
label="System prompt",
|
| 166 |
+
placeholder="Optional, e.g. \"You are a patient math tutor. Show your steps.\"",
|
| 167 |
+
lines=2,
|
| 168 |
+
render=False,
|
| 169 |
+
),
|
| 170 |
+
],
|
| 171 |
+
additional_inputs_accordion="Settings",
|
| 172 |
+
examples=[
|
| 173 |
+
["A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost?"],
|
| 174 |
+
["Write a Python function that returns the longest palindromic substring of a string, then explain its complexity."],
|
| 175 |
+
["How many positive integers below 1000 are divisible by 7 but not by 11?"],
|
| 176 |
+
["Explain the difference between correlation and causation in three short paragraphs."],
|
| 177 |
+
],
|
| 178 |
+
cache_examples=False,
|
| 179 |
+
fill_height=True,
|
| 180 |
+
)
|
| 181 |
+
gr.HTML(FOOTER)
|
| 182 |
+
|
| 183 |
+
if __name__ == "__main__":
|
| 184 |
+
demo.queue(default_concurrency_limit=CONCURRENCY).launch(theme=gr.themes.Soft(primary_hue="orange"), css=CSS)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai>=1.40
|
| 2 |
+
httpx
|