Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
model_name = "mistralai/Mistral-7B-v0.1"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 11 |
+
|
| 12 |
+
# if these 3 lines didn't work use the one above
|
| 13 |
+
from transformers import BitsAndBytesConfig
|
| 14 |
+
quant_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quant_config, device_map="auto")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.post("/generate")
|
| 19 |
+
async def generate_text(prompt: str):
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 21 |
+
outputs = model.generate(**inputs, max_length=200)
|
| 22 |
+
return {"response": tokenizer.decode(outputs[0], skip_special_tokens=True)}
|