devray11's picture
Update main.py
8da5546 verified
Raw
History Blame
1.66 kB
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import os
app = FastAPI()
# Enable CORS for your React/Lovable frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Configuration
REPO_ID = "devray11/Aevis-Medical-SLM"
MODEL_FILENAME = "DeepSeek-R1-Distill-Llama-8B.Q4_K_M.gguf"
print(f"πŸš€ Initializing Aevis Medical SLM...")
# Download model from Hugging Face Hub
try:
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
print(f"βœ… Model downloaded to: {model_path}")
except Exception as e:
print(f"❌ Error downloading model: {e}")
model_path = None
# Initialize Model with optimized CPU settings
# n_ctx=1024 (Saves RAM), n_threads=2 (Matches HF Free Tier)
llm = Llama(
model_path=model_path,
n_ctx=1024,
n_threads=2,
n_batch=512,
verbose=True
) if model_path else None
class ChatRequest(BaseModel):
prompt: str
@app.post("/generate")
async def generate(request: ChatRequest):
if not llm:
return {"error": "Model not loaded properly"}
# Standard instruction format for fine-tuned medical models
prompt = f"### Instruction:\n{request.prompt}\n\n### Response:\n"
response = llm(
prompt,
max_tokens=512,
stop=["###", "</s>"],
echo=False
)
return {"response": response["choices"][0]["text"].strip()}
@app.get("/")
def home():
return {"message": "Aevis Medical SLM API is Online", "model": REPO_ID}