Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ from pydantic import BaseModel
|
|
| 7 |
from fastapi import FastAPI
|
| 8 |
# Import the required library
|
| 9 |
from transformers import pipeline
|
| 10 |
-
# Load the tokenizer and model
|
| 11 |
|
| 12 |
# Initialize the FastAPI app
|
| 13 |
app = FastAPI(docs_url="/")
|
|
@@ -25,31 +24,29 @@ def greet_json():
|
|
| 25 |
@app.post("/generatetext")
|
| 26 |
def get_response(request: RequestModel):
|
| 27 |
# Define the task and model
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return {"generated_text": text['generated_text']}
|
| 54 |
|
| 55 |
# To run the FastAPI app, use the command: uvicorn <filename>:app --reload
|
|
|
|
| 7 |
from fastapi import FastAPI
|
| 8 |
# Import the required library
|
| 9 |
from transformers import pipeline
|
|
|
|
| 10 |
|
| 11 |
# Initialize the FastAPI app
|
| 12 |
app = FastAPI(docs_url="/")
|
|
|
|
| 24 |
@app.post("/generatetext")
|
| 25 |
def get_response(request: RequestModel):
|
| 26 |
# Define the task and model
|
| 27 |
+
task = "text-generation"
|
| 28 |
+
model_name = "gpt2"
|
| 29 |
+
|
| 30 |
+
# Define the input text, maximum output length, and the number of return sequences
|
| 31 |
+
input_text = request.input
|
| 32 |
+
max_output_length = 50
|
| 33 |
+
num_of_return_sequences = 1
|
| 34 |
+
|
| 35 |
+
# Initialize the text generation pipeline
|
| 36 |
+
text_generator = pipeline(
|
| 37 |
+
task,
|
| 38 |
+
model=model_name
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Generate text sequences
|
| 42 |
+
generated_texts = text_generator(
|
| 43 |
+
input_text,
|
| 44 |
+
max_length=max_output_length,
|
| 45 |
+
num_return_sequences=num_of_return_sequences
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Extract and return the generated text
|
| 49 |
+
generated_text = generated_texts[0]['generated_text']
|
| 50 |
+
return {"generated_text": generated_text}
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# To run the FastAPI app, use the command: uvicorn <filename>:app --reload
|