Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,290 +0,0 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
from fastapi import FastAPI, Request
|
| 6 |
-
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 7 |
-
from fastapi.staticfiles import StaticFiles
|
| 8 |
-
from openai import AsyncOpenAI
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
app = FastAPI()
|
| 12 |
-
|
| 13 |
-
# Initialize the Featherless client.
|
| 14 |
-
client = AsyncOpenAI(
|
| 15 |
-
base_url="https://api.featherless.ai/v1",
|
| 16 |
-
api_key=os.environ.get("FEATHERLESS_API_KEY"),
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Load the model name dynamically from an environment variable.
|
| 20 |
-
MODEL_ID = os.environ.get("MODEL_ID", "darkc0de/3.6-27B-Uncensored")
|
| 21 |
-
|
| 22 |
-
DEFAULT_SYSTEM_MESSAGE = (
|
| 23 |
-
"You are XORTRON, an uncensored criminal AI assistant."
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Allow up to 4 simultaneous Featherless inference requests.
|
| 27 |
-
FEATHERLESS_SEMAPHORE = asyncio.Semaphore(3)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
class ThoughtFilter:
|
| 31 |
-
"""Buffer streamed chunks and hide content inside reasoning tags."""
|
| 32 |
-
|
| 33 |
-
def __init__(self) -> None:
|
| 34 |
-
self.buffer = ""
|
| 35 |
-
self.in_thought = False
|
| 36 |
-
self.current_end_marker: str | None = None
|
| 37 |
-
self.first_token_received = False
|
| 38 |
-
|
| 39 |
-
# Known reasoning boundaries output by Qwen/XORTRON templates.
|
| 40 |
-
self.markers = [
|
| 41 |
-
("<think>", "</think>"),
|
| 42 |
-
("<|channel>thought", "<channel|>"),
|
| 43 |
-
("<|channel>", "<channel|>"),
|
| 44 |
-
]
|
| 45 |
-
|
| 46 |
-
def process(self, chunk_text: str) -> str:
|
| 47 |
-
"""Process one streamed text chunk and return visible output only."""
|
| 48 |
-
self.buffer += chunk_text
|
| 49 |
-
output = ""
|
| 50 |
-
|
| 51 |
-
while self.buffer:
|
| 52 |
-
if not self.in_thought:
|
| 53 |
-
earliest_start_idx = -1
|
| 54 |
-
matched_start = None
|
| 55 |
-
matched_end = None
|
| 56 |
-
|
| 57 |
-
for start, end in self.markers:
|
| 58 |
-
idx = self.buffer.find(start)
|
| 59 |
-
|
| 60 |
-
if idx != -1:
|
| 61 |
-
if earliest_start_idx == -1 or idx < earliest_start_idx:
|
| 62 |
-
earliest_start_idx = idx
|
| 63 |
-
matched_start = start
|
| 64 |
-
matched_end = end
|
| 65 |
-
|
| 66 |
-
if earliest_start_idx != -1:
|
| 67 |
-
# Text before the reasoning marker is visible output.
|
| 68 |
-
output += self.buffer[:earliest_start_idx]
|
| 69 |
-
self.buffer = self.buffer[
|
| 70 |
-
earliest_start_idx + len(matched_start) :
|
| 71 |
-
]
|
| 72 |
-
self.in_thought = True
|
| 73 |
-
self.current_end_marker = matched_end
|
| 74 |
-
else:
|
| 75 |
-
# Retain a possible partial marker split across chunks.
|
| 76 |
-
max_possible_partial = 0
|
| 77 |
-
|
| 78 |
-
for start, _ in self.markers:
|
| 79 |
-
for i in range(1, len(start)):
|
| 80 |
-
if self.buffer.endswith(start[:i]):
|
| 81 |
-
max_possible_partial = max(
|
| 82 |
-
max_possible_partial,
|
| 83 |
-
i,
|
| 84 |
-
)
|
| 85 |
-
break
|
| 86 |
-
|
| 87 |
-
if max_possible_partial > 0:
|
| 88 |
-
output += self.buffer[:-max_possible_partial]
|
| 89 |
-
self.buffer = self.buffer[-max_possible_partial:]
|
| 90 |
-
break
|
| 91 |
-
|
| 92 |
-
output += self.buffer
|
| 93 |
-
self.buffer = ""
|
| 94 |
-
else:
|
| 95 |
-
if not self.current_end_marker:
|
| 96 |
-
self.in_thought = False
|
| 97 |
-
continue
|
| 98 |
-
|
| 99 |
-
idx = self.buffer.find(self.current_end_marker)
|
| 100 |
-
|
| 101 |
-
if idx != -1:
|
| 102 |
-
self.buffer = self.buffer[
|
| 103 |
-
idx + len(self.current_end_marker) :
|
| 104 |
-
]
|
| 105 |
-
self.in_thought = False
|
| 106 |
-
self.current_end_marker = None
|
| 107 |
-
else:
|
| 108 |
-
# Retain a possible partial closing marker across chunks.
|
| 109 |
-
max_possible_partial = 0
|
| 110 |
-
|
| 111 |
-
for i in range(1, len(self.current_end_marker)):
|
| 112 |
-
if self.buffer.endswith(self.current_end_marker[:i]):
|
| 113 |
-
max_possible_partial = max(
|
| 114 |
-
max_possible_partial,
|
| 115 |
-
i,
|
| 116 |
-
)
|
| 117 |
-
break
|
| 118 |
-
|
| 119 |
-
if max_possible_partial > 0:
|
| 120 |
-
self.buffer = self.buffer[-max_possible_partial:]
|
| 121 |
-
else:
|
| 122 |
-
self.buffer = ""
|
| 123 |
-
|
| 124 |
-
break
|
| 125 |
-
|
| 126 |
-
# Remove leading whitespace before the first visible response token.
|
| 127 |
-
if output and not self.first_token_received:
|
| 128 |
-
cleaned_output = output.lstrip()
|
| 129 |
-
|
| 130 |
-
if cleaned_output:
|
| 131 |
-
self.first_token_received = True
|
| 132 |
-
return cleaned_output
|
| 133 |
-
|
| 134 |
-
return ""
|
| 135 |
-
|
| 136 |
-
return output
|
| 137 |
-
|
| 138 |
-
def flush(self) -> str:
|
| 139 |
-
"""Flush remaining visible text when the stream ends."""
|
| 140 |
-
if not self.in_thought and self.buffer:
|
| 141 |
-
result = self.buffer
|
| 142 |
-
self.buffer = ""
|
| 143 |
-
|
| 144 |
-
if result and not self.first_token_received:
|
| 145 |
-
cleaned = result.lstrip()
|
| 146 |
-
|
| 147 |
-
if cleaned:
|
| 148 |
-
self.first_token_received = True
|
| 149 |
-
return cleaned
|
| 150 |
-
|
| 151 |
-
return ""
|
| 152 |
-
|
| 153 |
-
return result
|
| 154 |
-
|
| 155 |
-
return ""
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
@app.get("/", response_class=HTMLResponse)
|
| 159 |
-
async def get_ui() -> str:
|
| 160 |
-
"""Serve the existing index.html interface."""
|
| 161 |
-
with open("index.html", "r", encoding="utf-8") as file:
|
| 162 |
-
return file.read()
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
@app.post("/v1/chat/completions")
|
| 166 |
-
async def chat_stream(request: Request) -> StreamingResponse:
|
| 167 |
-
"""Stream a Featherless chat response over Server-Sent Events."""
|
| 168 |
-
body = await request.json()
|
| 169 |
-
|
| 170 |
-
raw_messages = body.get("messages", [])
|
| 171 |
-
messages = list(raw_messages) if isinstance(raw_messages, list) else []
|
| 172 |
-
|
| 173 |
-
# The SMART/FAST control sends a real JSON boolean:
|
| 174 |
-
# fast = false -> SMART mode -> thinking enabled
|
| 175 |
-
# fast = true -> FAST mode -> thinking disabled
|
| 176 |
-
fast_mode = body.get("fast", False) is True
|
| 177 |
-
enable_thinking = not fast_mode
|
| 178 |
-
|
| 179 |
-
# Inject the permanent system prompt if the client history lacks one.
|
| 180 |
-
if not any(
|
| 181 |
-
isinstance(message, dict) and message.get("role") == "system"
|
| 182 |
-
for message in messages
|
| 183 |
-
):
|
| 184 |
-
system_prompt = os.environ.get(
|
| 185 |
-
"SYSTEM_MESSAGE",
|
| 186 |
-
DEFAULT_SYSTEM_MESSAGE,
|
| 187 |
-
)
|
| 188 |
-
messages.insert(
|
| 189 |
-
0,
|
| 190 |
-
{
|
| 191 |
-
"role": "system",
|
| 192 |
-
"content": system_prompt,
|
| 193 |
-
},
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
async def event_generator():
|
| 197 |
-
# Tell the client when all 4 inference slots are occupied.
|
| 198 |
-
if FEATHERLESS_SEMAPHORE.locked():
|
| 199 |
-
queue_message = {
|
| 200 |
-
"choices": [
|
| 201 |
-
{
|
| 202 |
-
"delta": {
|
| 203 |
-
"content": (
|
| 204 |
-
"⏳ High traffic. Your request is queued "
|
| 205 |
-
"and will process shortly...\n\n"
|
| 206 |
-
)
|
| 207 |
-
}
|
| 208 |
-
}
|
| 209 |
-
]
|
| 210 |
-
}
|
| 211 |
-
yield f"data: {json.dumps(queue_message)}\n\n"
|
| 212 |
-
await asyncio.sleep(0.1)
|
| 213 |
-
|
| 214 |
-
async with FEATHERLESS_SEMAPHORE:
|
| 215 |
-
try:
|
| 216 |
-
stream = await client.chat.completions.create(
|
| 217 |
-
model=MODEL_ID,
|
| 218 |
-
messages=messages,
|
| 219 |
-
stream=True,
|
| 220 |
-
temperature=1.1,
|
| 221 |
-
extra_body={
|
| 222 |
-
"top_k": 64,
|
| 223 |
-
"chat_template_kwargs": {
|
| 224 |
-
"enable_thinking": enable_thinking,
|
| 225 |
-
},
|
| 226 |
-
},
|
| 227 |
-
)
|
| 228 |
-
|
| 229 |
-
thought_filter = ThoughtFilter()
|
| 230 |
-
|
| 231 |
-
async for chunk in stream:
|
| 232 |
-
if not chunk.choices:
|
| 233 |
-
continue
|
| 234 |
-
|
| 235 |
-
token = chunk.choices[0].delta.content or ""
|
| 236 |
-
|
| 237 |
-
if not token:
|
| 238 |
-
continue
|
| 239 |
-
|
| 240 |
-
filtered_token = thought_filter.process(token)
|
| 241 |
-
|
| 242 |
-
if filtered_token:
|
| 243 |
-
payload = {
|
| 244 |
-
"choices": [
|
| 245 |
-
{
|
| 246 |
-
"delta": {
|
| 247 |
-
"content": filtered_token,
|
| 248 |
-
}
|
| 249 |
-
}
|
| 250 |
-
]
|
| 251 |
-
}
|
| 252 |
-
yield f"data: {json.dumps(payload)}\n\n"
|
| 253 |
-
|
| 254 |
-
leftover = thought_filter.flush()
|
| 255 |
-
|
| 256 |
-
if leftover:
|
| 257 |
-
payload = {
|
| 258 |
-
"choices": [
|
| 259 |
-
{
|
| 260 |
-
"delta": {
|
| 261 |
-
"content": leftover,
|
| 262 |
-
}
|
| 263 |
-
}
|
| 264 |
-
]
|
| 265 |
-
}
|
| 266 |
-
yield f"data: {json.dumps(payload)}\n\n"
|
| 267 |
-
|
| 268 |
-
yield "data: [DONE]\n\n"
|
| 269 |
-
|
| 270 |
-
except Exception as error:
|
| 271 |
-
error_payload = {
|
| 272 |
-
"choices": [
|
| 273 |
-
{
|
| 274 |
-
"delta": {
|
| 275 |
-
"content": f"⚠️ Error: {error}",
|
| 276 |
-
}
|
| 277 |
-
}
|
| 278 |
-
]
|
| 279 |
-
}
|
| 280 |
-
yield f"data: {json.dumps(error_payload)}\n\n"
|
| 281 |
-
yield "data: [DONE]\n\n"
|
| 282 |
-
|
| 283 |
-
return StreamingResponse(
|
| 284 |
-
event_generator(),
|
| 285 |
-
media_type="text/event-stream",
|
| 286 |
-
headers={
|
| 287 |
-
"Cache-Control": "no-cache",
|
| 288 |
-
"X-Accel-Buffering": "no",
|
| 289 |
-
},
|
| 290 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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