File size: 1,400 Bytes
39cc8a5
823c760
70f94ec
a5b1f33
1aba7f3
97917f4
70f94ec
 
 
 
 
383a904
6d3fbf5
 
70f94ec
 
 
6d3fbf5
383a904
6d3fbf5
 
e9ad359
4545ff6
39cc8a5
 
 
 
 
 
a5b1f33
c6cb00e
65f1222
1aba7f3
 
 
97917f4
 
e9ad359
 
 
97917f4
 
 
8a5a310
 
823c760
73fcf85
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from fastapi import FastAPI
import uvicorn
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

model_name = "Tap-M/Luna-AI-Llama2-Uncensored"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,  # Enable 4-bit quantization
    bnb_4bit_compute_dtype=torch.float16
)

model = AutoModelForCausalLM.from_pretrained(
    model_name,  # Example model
    device_map="auto",            # Auto-distribute across GPU/CPU
    quantization_config=bnb_config,
    offload_folder="./offload",  # Temporary directory
    low_cpu_mem_usage=True,      # Reduces CPU memory spikes
    trust_remote_code=True        # Required for some models
)

# load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token

app = FastAPI()

@app.get("/")
def greet_json():
    return {"Hello": "World!"}

@app.get("/message")
async def message(input: str):
    prompt = "USER:" + input + "\nASSISTANT:"
    
    inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
    
    output = model.generate(
        input_ids=inputs["input_ids"],
        attention_mask=inputs["attention_mask"], 
        max_new_tokens=100,
    )
    
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    
    return response

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)