from fastapi import FastAPI, Form from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from transformers import AutoModelForCausalLM # Model loading llm = AutoModelForCausalLM.from_pretrained( "zephyr-7b-beta.Q4_K_S.gguf", model_type='mistral', max_new_tokens=1096, threads=3, ) # Pydantic object class Validation(BaseModel): prompt: str # Fast API app = FastAPI() # Set up CORS origins = [ "http://localhost", # Replace with the address of your Flutter web app "http://localhost:55345", # Add the port used by your Flutter web app ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Zephyr completion @app.post("/llm_on_cpu") async def stream(item: Validation): system_prompt = 'Below is an instruction that describes a task. Write a response that appropriately completes the request.' E_INST = "" user, assistant = "", "" prompt = f"{system_prompt}{E_INST}\n{user}\n{item.prompt.strip()}{E_INST}\n{assistant}\n" return llm(prompt)