Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| app = FastAPI() | |
| # Load model and tokenizer | |
| model_name = "mistralai/Mistral-7B-v0.1" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto") | |
| # if these 3 lines didn't work use the one above | |
| from transformers import BitsAndBytesConfig | |
| quant_config = BitsAndBytesConfig(load_in_8bit=True) | |
| model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quant_config, device_map="auto") | |
| async def generate_text(prompt: str): | |
| inputs = tokenizer(prompt, return_tensors="pt").to("cuda") | |
| outputs = model.generate(**inputs, max_length=200) | |
| return {"response": tokenizer.decode(outputs[0], skip_special_tokens=True)} | |