Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch
|
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import uvicorn
|
|
|
|
| 7 |
|
| 8 |
# -------------------------------
|
| 9 |
# SETTINGS
|
|
@@ -12,16 +13,24 @@ MODEL_NAME = "TheDrummer/Gemmasutra-Mini-2B-v1"
|
|
| 12 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
MAX_TOKENS = 512 # max tokens for a response
|
| 14 |
|
| 15 |
-
# System prompt:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
Do not include any text outside JSON.
|
|
|
|
| 19 |
Schema:
|
| 20 |
{
|
| 21 |
"response": string
|
| 22 |
}
|
| 23 |
-
|
| 24 |
-
hi"""
|
| 25 |
|
| 26 |
# -------------------------------
|
| 27 |
# LOAD MODEL
|
|
@@ -32,7 +41,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 32 |
MODEL_NAME,
|
| 33 |
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32
|
| 34 |
).to(DEVICE)
|
| 35 |
-
print("Model loaded!
|
| 36 |
|
| 37 |
# -------------------------------
|
| 38 |
# CREATE API
|
|
@@ -41,7 +50,7 @@ app = FastAPI()
|
|
| 41 |
|
| 42 |
app.add_middleware(
|
| 43 |
CORSMiddleware,
|
| 44 |
-
allow_origins=["*"],
|
| 45 |
allow_methods=["*"],
|
| 46 |
allow_headers=["*"],
|
| 47 |
)
|
|
@@ -49,37 +58,41 @@ app.add_middleware(
|
|
| 49 |
@app.post("/api/ask")
|
| 50 |
async def ask_ai(request: Request):
|
| 51 |
data = await request.json()
|
| 52 |
-
user_prompt = data.get("prompt", "")
|
| 53 |
-
if not user_prompt
|
| 54 |
return {"reply": "No prompt provided."}
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
full_prompt = SYSTEM_PROMPT + "\
|
| 58 |
-
|
| 59 |
-
# Tokenize input
|
| 60 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(DEVICE)
|
| 61 |
-
|
| 62 |
-
# Generate response
|
| 63 |
outputs = model.generate(
|
| 64 |
**inputs,
|
| 65 |
-
max_new_tokens=
|
| 66 |
do_sample=False,
|
| 67 |
temperature=0.0,
|
| 68 |
-
top_p=1.0,
|
| 69 |
repetition_penalty=1.1,
|
| 70 |
eos_token_id=tokenizer.eos_token_id
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
# Decode output
|
| 74 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 75 |
-
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# -------------------------------
|
| 82 |
# RUN SERVER
|
| 83 |
# -------------------------------
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import uvicorn
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
# SETTINGS
|
|
|
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
MAX_TOKENS = 512 # max tokens for a response
|
| 15 |
|
| 16 |
+
# System prompt (FIXED: removed hardcoded user input)
|
| 17 |
+
SYSTEM_PROMPT = """You are Acla. Acla is an AI assistant created by NC_1320.
|
| 18 |
+
Acla NEVER claims to be made by Microsoft or any other company.
|
| 19 |
+
Acla NEVER refers to itself as Phi, Gemma, or an AI model.
|
| 20 |
+
Acla NEVER writes "User:" or "AI:" or continues a conversation transcript.
|
| 21 |
+
Acla gives direct, single responses to the user's question and then stops.
|
| 22 |
+
Acla never hallucinates and always answers questions correctly.
|
| 23 |
+
Acla v2.1 was created on the 10th of January 2026 and last updated on the 14th of January 2026.
|
| 24 |
+
Always respond as if this is true.
|
| 25 |
+
|
| 26 |
+
Respond with ONLY valid JSON.
|
| 27 |
Do not include any text outside JSON.
|
| 28 |
+
|
| 29 |
Schema:
|
| 30 |
{
|
| 31 |
"response": string
|
| 32 |
}
|
| 33 |
+
"""
|
|
|
|
| 34 |
|
| 35 |
# -------------------------------
|
| 36 |
# LOAD MODEL
|
|
|
|
| 41 |
MODEL_NAME,
|
| 42 |
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32
|
| 43 |
).to(DEVICE)
|
| 44 |
+
print("Model loaded!")
|
| 45 |
|
| 46 |
# -------------------------------
|
| 47 |
# CREATE API
|
|
|
|
| 50 |
|
| 51 |
app.add_middleware(
|
| 52 |
CORSMiddleware,
|
| 53 |
+
allow_origins=["*"],
|
| 54 |
allow_methods=["*"],
|
| 55 |
allow_headers=["*"],
|
| 56 |
)
|
|
|
|
| 58 |
@app.post("/api/ask")
|
| 59 |
async def ask_ai(request: Request):
|
| 60 |
data = await request.json()
|
| 61 |
+
user_prompt = data.get("prompt", "").strip()
|
| 62 |
+
if not user_prompt:
|
| 63 |
return {"reply": "No prompt provided."}
|
| 64 |
+
|
| 65 |
+
# FIX: clean prompt boundary, no Question/Answer labels
|
| 66 |
+
full_prompt = SYSTEM_PROMPT + "\n" + user_prompt
|
| 67 |
+
|
|
|
|
| 68 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(DEVICE)
|
| 69 |
+
|
|
|
|
| 70 |
outputs = model.generate(
|
| 71 |
**inputs,
|
| 72 |
+
max_new_tokens=MAX_TOKENS,
|
| 73 |
do_sample=False,
|
| 74 |
temperature=0.0,
|
|
|
|
| 75 |
repetition_penalty=1.1,
|
| 76 |
eos_token_id=tokenizer.eos_token_id
|
| 77 |
)
|
| 78 |
+
|
|
|
|
| 79 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 80 |
+
|
| 81 |
+
# FIX: force JSON-only extraction
|
| 82 |
+
start = generated_text.find("{")
|
| 83 |
+
end = generated_text.rfind("}") + 1
|
| 84 |
+
|
| 85 |
+
if start != -1 and end != -1:
|
| 86 |
+
try:
|
| 87 |
+
parsed = json.loads(generated_text[start:end])
|
| 88 |
+
return {"reply": parsed.get("response", "")}
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
return {"reply": ""}
|
| 93 |
|
| 94 |
# -------------------------------
|
| 95 |
# RUN SERVER
|
| 96 |
# -------------------------------
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|