Spaces:
Runtime error
Runtime error
Maxwell commited on
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,152 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
import uuid
|
| 5 |
-
import asyncio
|
| 6 |
-
import gradio as gr
|
| 7 |
-
from fastapi import FastAPI, Request
|
| 8 |
-
from fastapi.responses import StreamingResponse, JSONResponse
|
| 9 |
-
from huggingface_hub import hf_hub_download
|
| 10 |
-
from llama_cpp import Llama
|
| 11 |
-
|
| 12 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
-
BASE_PATH = "/tmp/base.gguf"
|
| 14 |
-
LORA_PATH = "/tmp/lumen-adapter.gguf"
|
| 15 |
-
|
| 16 |
-
SYSTEM_PROMPT = (
|
| 17 |
-
"You are Lumen, an AI assistant made by Axion Labs. "
|
| 18 |
-
"Give precise, helpful answers to any question the user asks."
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
llm = None # loaded in background on startup
|
| 22 |
-
|
| 23 |
-
def _load_model():
|
| 24 |
-
global llm
|
| 25 |
-
if not os.path.exists(BASE_PATH):
|
| 26 |
-
print("Downloading base model...")
|
| 27 |
-
hf_hub_download(
|
| 28 |
-
repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
| 29 |
-
filename="Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
| 30 |
-
local_dir="/tmp",
|
| 31 |
-
)
|
| 32 |
-
os.rename("/tmp/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf", BASE_PATH)
|
| 33 |
-
|
| 34 |
-
if not os.path.exists(LORA_PATH):
|
| 35 |
-
print("Downloading adapter...")
|
| 36 |
-
hf_hub_download(
|
| 37 |
-
repo_id="RavikxxBGamin/Lumen",
|
| 38 |
-
filename="lumen-adapter.gguf",
|
| 39 |
-
token=HF_TOKEN,
|
| 40 |
-
local_dir="/tmp",
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
print("Loading model...")
|
| 44 |
-
llm = Llama(
|
| 45 |
-
model_path=BASE_PATH,
|
| 46 |
-
lora_path=LORA_PATH,
|
| 47 |
-
n_ctx=2048,
|
| 48 |
-
n_threads=2,
|
| 49 |
-
verbose=False,
|
| 50 |
-
)
|
| 51 |
-
print("Model ready.")
|
| 52 |
-
|
| 53 |
-
# ── FastAPI app ───────────────────────────────────────────────────────────────
|
| 54 |
-
|
| 55 |
-
fastapi_app = FastAPI()
|
| 56 |
-
|
| 57 |
-
@fastapi_app.on_event("startup")
|
| 58 |
-
async def startup():
|
| 59 |
-
loop = asyncio.get_event_loop()
|
| 60 |
-
loop.run_in_executor(None, _load_model)
|
| 61 |
-
|
| 62 |
-
@fastapi_app.get("/health")
|
| 63 |
-
def health():
|
| 64 |
-
return {"status": "ready" if llm is not None else "loading"}
|
| 65 |
-
|
| 66 |
-
@fastapi_app.post("/v1/chat/completions")
|
| 67 |
-
async def chat_completions(request: Request):
|
| 68 |
-
if llm is None:
|
| 69 |
-
return JSONResponse({"error": "Model is still loading, try again in a moment."}, status_code=503)
|
| 70 |
-
|
| 71 |
-
body = await request.json()
|
| 72 |
-
messages = body.get("messages", [])
|
| 73 |
-
max_tokens = int(body.get("max_tokens", 512))
|
| 74 |
-
temperature = float(body.get("temperature", 0.7))
|
| 75 |
-
stream = body.get("stream", False)
|
| 76 |
-
model_id = body.get("model", "lumen")
|
| 77 |
-
|
| 78 |
-
if not any(m.get("role") == "system" for m in messages):
|
| 79 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
|
| 80 |
-
|
| 81 |
-
if stream:
|
| 82 |
-
async def event_stream():
|
| 83 |
-
resp_id = "chatcmpl-" + uuid.uuid4().hex
|
| 84 |
-
created = int(time.time())
|
| 85 |
-
for chunk in llm.create_chat_completion(
|
| 86 |
-
messages=messages,
|
| 87 |
-
max_tokens=max_tokens,
|
| 88 |
-
temperature=temperature,
|
| 89 |
-
stream=True,
|
| 90 |
-
):
|
| 91 |
-
delta = chunk["choices"][0]["delta"]
|
| 92 |
-
finish = chunk["choices"][0].get("finish_reason")
|
| 93 |
-
data = {
|
| 94 |
-
"id": resp_id,
|
| 95 |
-
"object": "chat.completion.chunk",
|
| 96 |
-
"created": created,
|
| 97 |
-
"model": model_id,
|
| 98 |
-
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
|
| 99 |
-
}
|
| 100 |
-
yield f"data: {json.dumps(data)}\n\n"
|
| 101 |
-
await asyncio.sleep(0)
|
| 102 |
-
yield "data: [DONE]\n\n"
|
| 103 |
-
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 104 |
-
|
| 105 |
-
result = llm.create_chat_completion(
|
| 106 |
-
messages=messages,
|
| 107 |
-
max_tokens=max_tokens,
|
| 108 |
-
temperature=temperature,
|
| 109 |
-
stream=False,
|
| 110 |
-
)
|
| 111 |
-
return JSONResponse(result)
|
| 112 |
-
|
| 113 |
-
# ── Gradio UI ─────────────────────────────────────────────────────────────────
|
| 114 |
-
|
| 115 |
-
def respond(message, history, temperature, max_tokens):
|
| 116 |
-
if llm is None:
|
| 117 |
-
yield "Model is still loading, please wait a moment and try again."
|
| 118 |
-
return
|
| 119 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 120 |
-
for item in history:
|
| 121 |
-
if isinstance(item, dict):
|
| 122 |
-
messages.append({"role": item["role"], "content": item["content"]})
|
| 123 |
-
else:
|
| 124 |
-
user_msg, assistant_msg = item
|
| 125 |
-
messages.append({"role": "user", "content": user_msg})
|
| 126 |
-
if assistant_msg:
|
| 127 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 128 |
-
messages.append({"role": "user", "content": message})
|
| 129 |
-
|
| 130 |
-
response = ""
|
| 131 |
-
for chunk in llm.create_chat_completion(
|
| 132 |
-
messages=messages,
|
| 133 |
-
max_tokens=int(max_tokens),
|
| 134 |
-
temperature=temperature,
|
| 135 |
-
stream=True,
|
| 136 |
-
):
|
| 137 |
-
delta = chunk["choices"][0]["delta"].get("content", "")
|
| 138 |
-
response += delta
|
| 139 |
-
yield response
|
| 140 |
-
|
| 141 |
-
demo = gr.ChatInterface(
|
| 142 |
-
fn=respond,
|
| 143 |
-
title="Lumen",
|
| 144 |
-
description="by Axion Labs",
|
| 145 |
-
additional_inputs=[
|
| 146 |
-
gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature"),
|
| 147 |
-
gr.Slider(64, 1024, value=512, step=64, label="Max tokens"),
|
| 148 |
-
],
|
| 149 |
-
additional_inputs_accordion=gr.Accordion("Settings", open=False),
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|