Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Create a new
|
5 |
app = FastAPI()
|
6 |
|
7 |
# Initialize the text generation pipeline
|
@@ -11,11 +12,16 @@ pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2")
|
|
11 |
def home():
|
12 |
return {"message": "Hello World"}
|
13 |
|
14 |
-
# Define a function to handle the
|
15 |
-
@app.
|
16 |
-
def generate(
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Create a new FastAPI app instance
|
6 |
app = FastAPI()
|
7 |
|
8 |
# Initialize the text generation pipeline
|
|
|
12 |
def home():
|
13 |
return {"message": "Hello World"}
|
14 |
|
15 |
+
# Define a function to handle the POST request at '/generate'
|
16 |
+
@app.post("/generate")
|
17 |
+
def generate(request: dict):
|
18 |
+
try:
|
19 |
+
text = request.get('text')
|
20 |
+
if not text:
|
21 |
+
raise HTTPException(status_code=400, detail="Text field is required")
|
22 |
+
|
23 |
+
output = pipe(text, max_new_tokens=50)
|
24 |
+
# Return the generated text in JSON response
|
25 |
+
return {"output": output[0]['generated_text']}
|
26 |
+
except Exception as e:
|
27 |
+
raise HTTPException(status_code=500, detail=str(e))
|