from fastapi import FastAPI, HTTPException from pydantic import BaseModel from transformers import pipeline # Create a new FastAPI app instance app = FastAPI() # Initialize the text generation pipeline pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2") @app.get("/") def home(): return {"message": "Hello World"} # Define a function to handle the POST request at '/generate' @app.post("/generate") def generate(request: dict): try: text = request.get('text') if not text: raise HTTPException(status_code=400, detail="Text field is required") output = pipe(text, max_new_tokens=50) # Return the generated text in JSON response return {"output": output[0]['generated_text']} except Exception as e: raise HTTPException(status_code=500, detail=str(e))