Spaces:
Runtime error
Runtime error
| import os | |
| from fastapi import FastAPI | |
| from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer | |
| from peft import PeftModel, PeftConfig | |
| from huggingface_hub import login | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Get the Hugging Face token from the environment variable | |
| huggingface_token = os.getenv("HUGGING_FACE_TOKEN") | |
| if huggingface_token is None: | |
| raise ValueError("HUGGING_FACE_TOKEN environment variable is not set") | |
| # Login to Hugging Face Hub | |
| login(token=huggingface_token) | |
| # Initialize FastAPI app | |
| app = FastAPI() | |
| # Load PEFT model configuration and base model | |
| 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", use_auth_token=True) | |
| model = PeftModel.from_pretrained(base_model, "frankmorales2020/Mistral-7B-text-to-sql-flash-attention-2-dataeval") | |
| # Load tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3", use_auth_token=True) | |
| # Create the pipeline | |
| pipe = pipeline("text2sql", model=model, tokenizer=tokenizer) | |
| def home(): | |
| return {"message": "Hello World"} | |
| def generate(text: str): | |
| output = pipe(text) | |
| return {"output": output[0]['generated_text']} | |