Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,178 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import subprocess
|
| 5 |
+
import tarfile
|
| 6 |
+
import time
|
| 7 |
+
import urllib.request
|
| 8 |
+
import zipfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
from fastapi import Request
|
| 11 |
+
from fastapi.responses import StreamingResponse
|
| 12 |
+
from gradio import Server
|
| 13 |
+
import httpx
|
| 14 |
+
|
| 15 |
+
app = Server()
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
import spaces
|
| 19 |
+
HAS_SPACES = True
|
| 20 |
+
except ImportError:
|
| 21 |
+
HAS_SPACES = False
|
| 22 |
+
|
| 23 |
+
@spaces.GPU if HAS_SPACES else lambda f: f
|
| 24 |
+
def whygpu():
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
def build_latest_llama_server():
|
| 28 |
+
binary_path = os.path.abspath("llama.cpp/build/bin/llama-server")
|
| 29 |
+
if os.path.exists(binary_path):
|
| 30 |
+
return binary_path
|
| 31 |
+
|
| 32 |
+
print("Building latest llama.cpp from master source...")
|
| 33 |
+
|
| 34 |
+
if not os.path.exists("llama.cpp"):
|
| 35 |
+
subprocess.run(["git", "clone", "https://github.com/unslothai/llama.cpp.git"], check=True)
|
| 36 |
+
else:
|
| 37 |
+
subprocess.run(["git", "pull"], cwd="llama.cpp", check=True)
|
| 38 |
+
|
| 39 |
+
subprocess.run(["git", "fetch", "origin", "pull/144/head:mtp"], cwd="llama.cpp", check=True)
|
| 40 |
+
subprocess.run(["git", "checkout", "mtp"], cwd="llama.cpp", check=True)
|
| 41 |
+
|
| 42 |
+
env = os.environ.copy()
|
| 43 |
+
if "/usr/local/cuda/bin" not in env.get("PATH", ""):
|
| 44 |
+
env["PATH"] = f"/usr/local/cuda/bin:{env.get('PATH', '')}"
|
| 45 |
+
|
| 46 |
+
cmake_cmd = [
|
| 47 |
+
"cmake",
|
| 48 |
+
"-B",
|
| 49 |
+
"build",
|
| 50 |
+
"-DCMAKE_BUILD_TYPE=Release",
|
| 51 |
+
"-DGGML_NATIVE=ON",
|
| 52 |
+
"-DGGML_OPENMP=ON",
|
| 53 |
+
"-DGGML_AVX512=ON",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
subprocess.run(cmake_cmd, cwd="llama.cpp", env=env, check=True)
|
| 57 |
+
subprocess.run(
|
| 58 |
+
["cmake", "--build", "build", "--config", "Release", "-j", str(os.cpu_count() or 4), "--target", "llama-server"],
|
| 59 |
+
cwd="llama.cpp",
|
| 60 |
+
env=env,
|
| 61 |
+
check=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if not os.path.exists(binary_path):
|
| 65 |
+
raise RuntimeError("Failed to build llama-server binary!")
|
| 66 |
+
|
| 67 |
+
return binary_path
|
| 68 |
+
|
| 69 |
+
def apathy_exe():
|
| 70 |
+
model_path = hf_hub_download(
|
| 71 |
+
repo_id="Qwen/Qwen3.8-Flash-Next",
|
| 72 |
+
filename="q.gguf",
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def start_llama_server():
|
| 76 |
+
binary_path = build_latest_llama_server()
|
| 77 |
+
|
| 78 |
+
from huggingface_hub import hf_hub_download
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
model_path = None
|
| 82 |
+
for i in range(1,34):
|
| 83 |
+
mp = hf_hub_download(
|
| 84 |
+
repo_id="AtomicChat/Qwen3.8-Flash-Next-GGUF",
|
| 85 |
+
filename="Qwen3.8-Flash-Next-AD-4.27bpw-Q4_K_M-M64/Qwen3.8-Flash-Next-AD-4.27bpw-Q4_K_M-M64-{}-of-00033.gguf".format(str(i).zfill(5))
|
| 86 |
+
)
|
| 87 |
+
if not model_path:
|
| 88 |
+
model_path = mp
|
| 89 |
+
|
| 90 |
+
mmproj_path = hf_hub_download(
|
| 91 |
+
repo_id="AtomicChat/Qwen3.8-Flash-Next-GGUF",
|
| 92 |
+
filename="mmproj-Qwen3.8-Flash-Next-F16.gguf",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
binary_dir = os.path.dirname(os.path.abspath(binary_path))
|
| 98 |
+
env = os.environ.copy()
|
| 99 |
+
env["LD_LIBRARY_PATH"] = f"{binary_dir}:{env.get('LD_LIBRARY_PATH', '')}"
|
| 100 |
+
|
| 101 |
+
cmd = [
|
| 102 |
+
binary_path,
|
| 103 |
+
"-m", model_path,
|
| 104 |
+
"-mm", mmproj_path,
|
| 105 |
+
#"-md", draft_path,
|
| 106 |
+
"--port", "8000",
|
| 107 |
+
"--host", "127.0.0.1",
|
| 108 |
+
"-t", "16",
|
| 109 |
+
"-tb", "16",
|
| 110 |
+
"-fa", "on",
|
| 111 |
+
"--parallel", "1",
|
| 112 |
+
"--load-mode", "dio",
|
| 113 |
+
"--cache-type-k", "q4_0",
|
| 114 |
+
"--cache-type-v", "q4_0",
|
| 115 |
+
"--jinja",
|
| 116 |
+
"--temp", "1.0",
|
| 117 |
+
"--top-p", "0.95",
|
| 118 |
+
"--top-k", "20",
|
| 119 |
+
"--min-p", "0.0",
|
| 120 |
+
"--presence-penalty", "0.0",
|
| 121 |
+
"--repeat-penalty", "1.0",
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
process = subprocess.Popen(cmd, env=env)
|
| 125 |
+
time.sleep(5)
|
| 126 |
+
return process
|
| 127 |
+
|
| 128 |
+
@app.middleware("http")
|
| 129 |
+
async def proxy_middleware(request: Request, call_next):
|
| 130 |
+
path = request.url.path
|
| 131 |
+
if path.startswith("/gradio_api"):
|
| 132 |
+
return await call_next(request)
|
| 133 |
+
|
| 134 |
+
url = f"http://127.0.0.1:8000{path}"
|
| 135 |
+
|
| 136 |
+
headers = {
|
| 137 |
+
k: v for k, v in request.headers.items()
|
| 138 |
+
if k.lower() not in ("host", "content-length", "accept-encoding")
|
| 139 |
+
}
|
| 140 |
+
body = await request.body()
|
| 141 |
+
|
| 142 |
+
client = httpx.AsyncClient(timeout=httpx.Timeout(600.0, connect=120.0))
|
| 143 |
+
req = client.build_request(
|
| 144 |
+
method=request.method,
|
| 145 |
+
url=url,
|
| 146 |
+
headers=headers,
|
| 147 |
+
params=request.query_params,
|
| 148 |
+
content=body,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
response = await client.send(req, stream=True)
|
| 153 |
+
except Exception as e:
|
| 154 |
+
await client.aclose()
|
| 155 |
+
return StreamingResponse(iter([f"Proxy Error: {e}".encode()]), status_code=502)
|
| 156 |
+
|
| 157 |
+
async def stream_and_close():
|
| 158 |
+
try:
|
| 159 |
+
async for chunk in response.aiter_raw():
|
| 160 |
+
yield chunk
|
| 161 |
+
finally:
|
| 162 |
+
await response.aclose()
|
| 163 |
+
await client.aclose()
|
| 164 |
+
|
| 165 |
+
res_headers = {
|
| 166 |
+
k: v for k, v in response.headers.items()
|
| 167 |
+
if k.lower() not in ("content-length", "transfer-encoding", "connection")
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
return StreamingResponse(
|
| 171 |
+
stream_and_close(),
|
| 172 |
+
status_code=response.status_code,
|
| 173 |
+
headers=res_headers
|
| 174 |
+
)
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
+
start_llama_server()
|
| 178 |
+
app.launch(show_error=True)
|