Spaces:
Runtime error
Runtime error
File size: 1,504 Bytes
5c19521 21b7e63 a3e7329 5c19521 a3e7329 5c19521 a3e7329 5c19521 a3e7329 5c19521 a3e7329 |
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 |
from fastapi import FastAPI
from transformers import AutoModelForCausalLM
# Wrap problematic imports in try-except blocks
try:
from peft import PeftModel, PeftConfig
except ImportError as e:
print(f"Error importing from peft: {e}")
raise
try:
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
except ImportError as e:
print(f"Error importing from mistral_common: {e}")
raise
# Initialize FastAPI app
app = FastAPI()
# Load PEFT model configuration and base model
try:
config = PeftConfig.from_pretrained("frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
model = PeftModel.from_pretrained(base_model, "frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval")
# Load recommended tokenizer
tokenizer = MistralTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
# Create the pipeline
from transformers import pipeline
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
except Exception as e:
print(f"Error loading model or creating pipeline: {e}")
raise
@app.get("/")
def home():
return {"message": "Hello World"}
@app.get("/generate")
def generate(text: str):
try:
output = pipe(text)
return {"output": output[0]['generated_text']}
except Exception as e:
return {"error": str(e)} |