Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
generator = pipeline('text-generation', model='gpt2')
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# Generate text using the model
|
| 10 |
-
result = generator(input_text, max_length=50, num_return_sequences=1)
|
| 11 |
-
return result[0]['generated_text']
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
inputs="text", # Input type
|
| 17 |
-
outputs="text", # Output type
|
| 18 |
-
title="Text Generation API", # Title of the API
|
| 19 |
-
description="Provide text as input, and the model will generate some text.",
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
| 6 |
|
| 7 |
+
# Load the text generation pipeline from Hugging Face
|
| 8 |
+
generator = pipeline('text-generation', model='gpt2')
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Define the request body
|
| 11 |
+
class TextInput(BaseModel):
|
| 12 |
+
input_text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@app.post("/generate")
|
| 15 |
+
def generate_text(text_input: TextInput):
|
| 16 |
+
# Generate text from the model based on the input
|
| 17 |
+
generated = generator(text_input.input_text, max_length=100, num_return_sequences=1)
|
| 18 |
+
return {"generated_text": generated[0]['generated_text']}
|