Spaces:
Build error
Build error
File size: 1,475 Bytes
39cc8a5 823c760 6d3fbf5 a5b1f33 e9ad359 97917f4 6d3fbf5 383a904 6d3fbf5 383a904 6d3fbf5 e9ad359 4545ff6 39cc8a5 a5b1f33 c6cb00e 65f1222 e9ad359 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 = "TheBloke/Wizard-Vicuna-13B-Uncensored-HF"
# Configure 4-bit quantization
bnb_config = BitsAndBytesConfig(
load_in_4bit=True, # Enable 4-bit quantization
bnb_4bit_quant_type="nf4", # Use 4-bit NormalFloat (optimal)
bnb_4bit_compute_dtype="float16", # Faster computation with float16
bnb_4bit_use_double_quant=True # Extra compression
)
# Load model with quantization
model = AutoModelForCausalLM.from_pretrained(
model_name, # Example model
quantization_config=bnb_config,
device_map="auto", # Auto-distribute across GPU/CPU
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):
inputs = tokenizer(input, 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) |