Spaces:
Runtime error
Runtime error
deepumanju
commited on
Commit
•
7d81b02
1
Parent(s):
b9c43aa
Upload main.py
Browse files
main.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Load the text-to-text generation pipeline
|
8 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
9 |
+
|
10 |
+
# Define the input data model
|
11 |
+
class GenerateRequest(BaseModel):
|
12 |
+
text: str
|
13 |
+
|
14 |
+
@app.get("/")
|
15 |
+
def welcome():
|
16 |
+
"""
|
17 |
+
Welcoming page.
|
18 |
+
"""
|
19 |
+
return {"message": "Welcome to the Text-to-Text Generation API! Use the /generate endpoint to transform text."}
|
20 |
+
|
21 |
+
@app.post("/generate")
|
22 |
+
def generate_text(data: GenerateRequest):
|
23 |
+
"""
|
24 |
+
Generate text from the input text using the text-to-text generation model.
|
25 |
+
"""
|
26 |
+
try:
|
27 |
+
# Perform text-to-text generation
|
28 |
+
result = pipe(data.text, max_length=50, num_return_sequences=1)
|
29 |
+
return {"input": data.text, "generated_text": result[0]["generated_text"]}
|
30 |
+
except Exception as e:
|
31 |
+
raise HTTPException(status_code=500, detail=str(e))
|