File size: 871 Bytes
95b255d
 
6f92cbb
 
95b255d
6f92cbb
 
 
 
 
 
 
 
 
95b255d
 
 
 
 
 
 
 
 
 
 
 
 
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
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))