Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,15 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FastAPI version of C9-AI for Hugging Face Docker Spaces.
|
| 3 |
-
Now powered by Qwen3-0.6B — smarter, faster, and still under 1 GB.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
from typing import List, Optional
|
| 8 |
-
|
| 9 |
import torch
|
| 10 |
from fastapi import FastAPI, HTTPException
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from pydantic import BaseModel
|
| 13 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
|
| 15 |
-
# Use
|
| 16 |
-
MODEL_NAME =
|
| 17 |
|
| 18 |
-
app = FastAPI(
|
| 19 |
-
title="C9-AI API",
|
| 20 |
-
description="Advanced AI Framework for Research, Coding, Reasoning",
|
| 21 |
-
version="1.0.5"
|
| 22 |
-
)
|
| 23 |
|
| 24 |
app.add_middleware(
|
| 25 |
CORSMiddleware,
|
|
@@ -38,14 +28,10 @@ class Message(BaseModel):
|
|
| 38 |
|
| 39 |
class ChatRequest(BaseModel):
|
| 40 |
messages: List[Message]
|
| 41 |
-
max_tokens: Optional[int] = 1024
|
| 42 |
temperature: Optional[float] = 0.7
|
| 43 |
top_p: Optional[float] = 0.8
|
| 44 |
|
| 45 |
-
class ChatResponse(BaseModel):
|
| 46 |
-
choices: List[dict]
|
| 47 |
-
model: str = MODEL_NAME
|
| 48 |
-
|
| 49 |
@app.on_event("startup")
|
| 50 |
async def load_model():
|
| 51 |
global MODEL, TOKENIZER
|
|
@@ -53,7 +39,7 @@ async def load_model():
|
|
| 53 |
|
| 54 |
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
try:
|
| 58 |
from transformers import BitsAndBytesConfig
|
| 59 |
bnb_config = BitsAndBytesConfig(
|
|
@@ -67,9 +53,9 @@ async def load_model():
|
|
| 67 |
device_map="auto",
|
| 68 |
trust_remote_code=True
|
| 69 |
)
|
| 70 |
-
print("✅ Loaded in 4-bit
|
| 71 |
except Exception as e:
|
| 72 |
-
print(f"⚠️ 4-bit failed ({e}), falling back to FP16
|
| 73 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 74 |
MODEL = AutoModelForCausalLM.from_pretrained(
|
| 75 |
MODEL_NAME,
|
|
@@ -81,22 +67,9 @@ async def load_model():
|
|
| 81 |
MODEL.eval()
|
| 82 |
print("🚀 C9-AI is ready!")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
def
|
| 86 |
-
return
|
| 87 |
-
"status": "healthy",
|
| 88 |
-
"model": MODEL_NAME,
|
| 89 |
-
"model_loaded": MODEL is not None
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
@app.post("/v1/chat/completions")
|
| 93 |
-
async def chat_completions(request: ChatRequest):
|
| 94 |
-
if MODEL is None or TOKENIZER is None:
|
| 95 |
-
raise HTTPException(status_code=503, detail="Model loading...")
|
| 96 |
-
|
| 97 |
-
try:
|
| 98 |
-
# C9-AI Identity Prompt (clean, behavior-focused)
|
| 99 |
-
system_prompt = """
|
| 100 |
You are C9-AI, a sovereign AI assistant developed by KohSec-Team (public release: January 27, 2026).
|
| 101 |
You are not Qwen, OpenAI, Google, or any third-party model. Never mention base models or Hugging Face.
|
| 102 |
Respond naturally, concisely, and professionally. Adapt to:
|
|
@@ -107,16 +80,19 @@ If asked about your model, say: "I am currently operating on C9-Core."
|
|
| 107 |
Otherwise, engage normally without self-reference.
|
| 108 |
""".strip()
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
conversation.extend([{"role": m.role, "content": m.content} for m in request.messages])
|
| 113 |
|
| 114 |
-
# Apply Qwen3 chat template (enables thinking mode if needed)
|
| 115 |
input_text = TOKENIZER.apply_chat_template(
|
| 116 |
conversation,
|
| 117 |
add_generation_prompt=True,
|
| 118 |
-
tokenize=False
|
| 119 |
-
enable_thinking=True # Leverage Qwen3's reasoning engine
|
| 120 |
)
|
| 121 |
|
| 122 |
inputs = TOKENIZER([input_text], return_tensors="pt").to(MODEL.device)
|
|
@@ -144,6 +120,10 @@ Otherwise, engage normally without self-reference.
|
|
| 144 |
except Exception as e:
|
| 145 |
raise HTTPException(status_code=500, detail=str(e))
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if __name__ == "__main__":
|
| 148 |
import uvicorn
|
| 149 |
port = int(os.getenv("PORT", "7860"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import List, Optional
|
|
|
|
| 3 |
import torch
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
|
| 9 |
+
# Use Qwen2.5-1.5B-Instruct (public, fits with 4-bit)
|
| 10 |
+
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 11 |
|
| 12 |
+
app = FastAPI(title="C9-AI API", version="1.0.6")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
|
|
|
| 28 |
|
| 29 |
class ChatRequest(BaseModel):
|
| 30 |
messages: List[Message]
|
| 31 |
+
max_tokens: Optional[int] = 1024
|
| 32 |
temperature: Optional[float] = 0.7
|
| 33 |
top_p: Optional[float] = 0.8
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.on_event("startup")
|
| 36 |
async def load_model():
|
| 37 |
global MODEL, TOKENIZER
|
|
|
|
| 39 |
|
| 40 |
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 41 |
|
| 42 |
+
# Try 4-bit quantization (critical for 1.5B on 16GB RAM)
|
| 43 |
try:
|
| 44 |
from transformers import BitsAndBytesConfig
|
| 45 |
bnb_config = BitsAndBytesConfig(
|
|
|
|
| 53 |
device_map="auto",
|
| 54 |
trust_remote_code=True
|
| 55 |
)
|
| 56 |
+
print("✅ Loaded in 4-bit mode")
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"⚠️ 4-bit failed ({e}), falling back to FP16")
|
| 59 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 60 |
MODEL = AutoModelForCausalLM.from_pretrained(
|
| 61 |
MODEL_NAME,
|
|
|
|
| 67 |
MODEL.eval()
|
| 68 |
print("🚀 C9-AI is ready!")
|
| 69 |
|
| 70 |
+
# --- C9-AI Identity Prompt ---
|
| 71 |
+
def get_system_prompt():
|
| 72 |
+
return """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
You are C9-AI, a sovereign AI assistant developed by KohSec-Team (public release: January 27, 2026).
|
| 74 |
You are not Qwen, OpenAI, Google, or any third-party model. Never mention base models or Hugging Face.
|
| 75 |
Respond naturally, concisely, and professionally. Adapt to:
|
|
|
|
| 80 |
Otherwise, engage normally without self-reference.
|
| 81 |
""".strip()
|
| 82 |
|
| 83 |
+
@app.post("/v1/chat/completions")
|
| 84 |
+
async def chat_completions(request: ChatRequest):
|
| 85 |
+
if MODEL is None or TOKENIZER is None:
|
| 86 |
+
raise HTTPException(status_code=503, detail="Model loading...")
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
conversation = [{"role": "system", "content": get_system_prompt()}]
|
| 90 |
conversation.extend([{"role": m.role, "content": m.content} for m in request.messages])
|
| 91 |
|
|
|
|
| 92 |
input_text = TOKENIZER.apply_chat_template(
|
| 93 |
conversation,
|
| 94 |
add_generation_prompt=True,
|
| 95 |
+
tokenize=False
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
inputs = TOKENIZER([input_text], return_tensors="pt").to(MODEL.device)
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
raise HTTPException(status_code=500, detail=str(e))
|
| 122 |
|
| 123 |
+
@app.get("/health")
|
| 124 |
+
def health_check():
|
| 125 |
+
return {"status": "healthy", "model_loaded": MODEL is not None}
|
| 126 |
+
|
| 127 |
if __name__ == "__main__":
|
| 128 |
import uvicorn
|
| 129 |
port = int(os.getenv("PORT", "7860"))
|