Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import uvicorn
|
| 4 |
-
from fastapi import FastAPI, Request
|
| 5 |
-
from fastapi.responses import StreamingResponse, JSONResponse
|
| 6 |
import gradio as gr
|
| 7 |
import spaces
|
|
|
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from llama_cpp import Llama
|
| 10 |
|
| 11 |
-
# 1. DUMMY GPU FUNCTION: Satisfies the Hugging Face ZeroGPU checks
|
| 12 |
@spaces.GPU(duration=5)
|
| 13 |
def dummy_gpu():
|
| 14 |
print("ZeroGPU validation satisfied.")
|
| 15 |
return 1
|
| 16 |
|
| 17 |
-
# Automatically execute it once right away
|
| 18 |
dummy_gpu()
|
| 19 |
|
| 20 |
# 2. DOWNLOAD LFM 2.5 8B A1B GGUF MODEL
|
|
@@ -28,55 +26,15 @@ model_path = hf_hub_download(
|
|
| 28 |
print("Loading model into memory via llama-cpp-python...")
|
| 29 |
llm = Llama(
|
| 30 |
model_path=model_path,
|
| 31 |
-
n_ctx=8192,
|
| 32 |
-
n_threads=2,
|
| 33 |
-
chat_format="chatml", # Native format used by LFM2.5
|
| 34 |
verbose=False
|
| 35 |
)
|
| 36 |
print("Model loaded successfully!")
|
| 37 |
|
| 38 |
-
# 4. BUILD
|
| 39 |
-
app = FastAPI(title="LFM2.5-8B API Server")
|
| 40 |
-
|
| 41 |
-
@app.post("/v1/chat/completions")
|
| 42 |
-
async def chat_api(request: Request):
|
| 43 |
-
"""
|
| 44 |
-
OpenAI-compatible API Endpoint!
|
| 45 |
-
You can send JSON payloads here exactly as you would to the OpenAI API.
|
| 46 |
-
"""
|
| 47 |
-
try:
|
| 48 |
-
body = await request.json()
|
| 49 |
-
messages = body.get("messages", [])
|
| 50 |
-
stream = body.get("stream", False)
|
| 51 |
-
temperature = body.get("temperature", 0.7)
|
| 52 |
-
max_tokens = body.get("max_tokens", 1024)
|
| 53 |
-
|
| 54 |
-
if stream:
|
| 55 |
-
def stream_generator():
|
| 56 |
-
for chunk in llm.create_chat_completion(
|
| 57 |
-
messages=messages,
|
| 58 |
-
stream=True,
|
| 59 |
-
temperature=temperature,
|
| 60 |
-
max_tokens=max_tokens
|
| 61 |
-
):
|
| 62 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 63 |
-
yield "data: [DONE]\n\n"
|
| 64 |
-
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 65 |
-
else:
|
| 66 |
-
response = llm.create_chat_completion(
|
| 67 |
-
messages=messages,
|
| 68 |
-
stream=False,
|
| 69 |
-
temperature=temperature,
|
| 70 |
-
max_tokens=max_tokens
|
| 71 |
-
)
|
| 72 |
-
return JSONResponse(content=response)
|
| 73 |
-
except Exception as e:
|
| 74 |
-
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 75 |
-
|
| 76 |
-
# 5. BUILD GRADIO UI (Fallback Web Interface)
|
| 77 |
def chat_with_llama(message, history):
|
| 78 |
messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
|
| 79 |
-
|
| 80 |
for user_msg, assistant_msg in history:
|
| 81 |
messages.append({"role": "user", "content": user_msg})
|
| 82 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
@@ -99,13 +57,13 @@ def chat_with_llama(message, history):
|
|
| 99 |
yield partial_response
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
-
gr.Markdown("# ๐ LiquidAI LFM2.5-8B
|
| 103 |
gr.Markdown("""
|
| 104 |
Running **LFM2.5-8B-A1B** natively via `llama-cpp-python` entirely on CPU! ZeroGPU is bypassed safely.
|
| 105 |
|
| 106 |
### ๐ Developer API Available!
|
| 107 |
-
This Space also silently hosts a real API endpoint
|
| 108 |
-
**Endpoint:** `POST /v1/chat/completions` (Supports Streaming & Non-Streaming)
|
| 109 |
""")
|
| 110 |
|
| 111 |
gr.ChatInterface(
|
|
@@ -113,9 +71,54 @@ with gr.Blocks() as demo:
|
|
| 113 |
examples=["Who are you?", "Write a python script to reverse a string.", "Explain quantum computing."],
|
| 114 |
)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
# This
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
|
|
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
+
from fastapi import Request
|
| 6 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from llama_cpp import Llama
|
| 9 |
|
| 10 |
+
# 1. DUMMY GPU FUNCTION: Satisfies the Hugging Face ZeroGPU checks
|
| 11 |
@spaces.GPU(duration=5)
|
| 12 |
def dummy_gpu():
|
| 13 |
print("ZeroGPU validation satisfied.")
|
| 14 |
return 1
|
| 15 |
|
|
|
|
| 16 |
dummy_gpu()
|
| 17 |
|
| 18 |
# 2. DOWNLOAD LFM 2.5 8B A1B GGUF MODEL
|
|
|
|
| 26 |
print("Loading model into memory via llama-cpp-python...")
|
| 27 |
llm = Llama(
|
| 28 |
model_path=model_path,
|
| 29 |
+
n_ctx=8192,
|
| 30 |
+
n_threads=2,
|
|
|
|
| 31 |
verbose=False
|
| 32 |
)
|
| 33 |
print("Model loaded successfully!")
|
| 34 |
|
| 35 |
+
# 4. BUILD GRADIO UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def chat_with_llama(message, history):
|
| 37 |
messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
|
|
|
|
| 38 |
for user_msg, assistant_msg in history:
|
| 39 |
messages.append({"role": "user", "content": user_msg})
|
| 40 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
| 57 |
yield partial_response
|
| 58 |
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("# ๐ LiquidAI LFM2.5-8B (CPU & API Enabled)")
|
| 61 |
gr.Markdown("""
|
| 62 |
Running **LFM2.5-8B-A1B** natively via `llama-cpp-python` entirely on CPU! ZeroGPU is bypassed safely.
|
| 63 |
|
| 64 |
### ๐ Developer API Available!
|
| 65 |
+
This Space also silently hosts a real API endpoint on the exact same port.
|
| 66 |
+
**Endpoint:** `POST /v1/chat/completions` (Supports Streaming & Non-Streaming OpenAI formats)
|
| 67 |
""")
|
| 68 |
|
| 69 |
gr.ChatInterface(
|
|
|
|
| 71 |
examples=["Who are you?", "Write a python script to reverse a string.", "Explain quantum computing."],
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# 5. INJECT FASTAPI ROUTES DIRECTLY INTO GRADIO
|
| 75 |
+
# This safely bypasses the ZeroGPU port collision by attaching our API
|
| 76 |
+
# directly to the Gradio app before the server boots up.
|
| 77 |
+
import gradio.routes
|
| 78 |
+
original_create_app = gradio.routes.App.create_app
|
| 79 |
+
|
| 80 |
+
def custom_create_app(*args, **kwargs):
|
| 81 |
+
# Let Gradio build its native FastAPI app
|
| 82 |
+
app = original_create_app(*args, **kwargs)
|
| 83 |
+
|
| 84 |
+
# Inject our custom OpenAI-compatible route onto it
|
| 85 |
+
@app.post("/v1/chat/completions")
|
| 86 |
+
async def chat_api(request: Request):
|
| 87 |
+
try:
|
| 88 |
+
body = await request.json()
|
| 89 |
+
messages = body.get("messages", [])
|
| 90 |
+
stream = body.get("stream", False)
|
| 91 |
+
temperature = body.get("temperature", 0.7)
|
| 92 |
+
max_tokens = body.get("max_tokens", 1024)
|
| 93 |
+
|
| 94 |
+
if stream:
|
| 95 |
+
def stream_generator():
|
| 96 |
+
for chunk in llm.create_chat_completion(
|
| 97 |
+
messages=messages,
|
| 98 |
+
stream=True,
|
| 99 |
+
temperature=temperature,
|
| 100 |
+
max_tokens=max_tokens
|
| 101 |
+
):
|
| 102 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
| 103 |
+
yield "data: [DONE]\n\n"
|
| 104 |
+
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 105 |
+
else:
|
| 106 |
+
response = llm.create_chat_completion(
|
| 107 |
+
messages=messages,
|
| 108 |
+
stream=False,
|
| 109 |
+
temperature=temperature,
|
| 110 |
+
max_tokens=max_tokens
|
| 111 |
+
)
|
| 112 |
+
return JSONResponse(content=response)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 115 |
+
|
| 116 |
+
return app
|
| 117 |
+
|
| 118 |
+
# Apply the patch
|
| 119 |
+
gradio.routes.App.create_app = custom_create_app
|
| 120 |
|
| 121 |
+
# 6. LAUNCH GRADIO
|
| 122 |
+
# ZeroGPU will safely intercept this and route our API & UI perfectly!
|
| 123 |
if __name__ == "__main__":
|
| 124 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|