Spaces:
Sleeping
Sleeping
aryan10022001
commited on
Commit
•
bd8aa05
1
Parent(s):
78df1e4
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,54 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
app=FastAPI()
|
6 |
|
7 |
# Initialize the text generation pipeline
|
8 |
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
9 |
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
return {"message":"Hello World"}
|
14 |
-
|
15 |
-
# Define a function to handle the GET request at `/generate`
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
-
@app.
|
19 |
-
def generate(text:str):
|
20 |
-
|
21 |
-
output=pipe(text)
|
22 |
|
23 |
-
|
24 |
-
return {"output":output[0]['generated_text']}
|
|
|
1 |
+
# from fastapi import FastAPI
|
2 |
+
# from transformers import pipeline
|
3 |
+
|
4 |
+
# ## create a new FASTAPI app instance
|
5 |
+
# app=FastAPI()
|
6 |
+
|
7 |
+
# # Initialize the text generation pipeline
|
8 |
+
# pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
9 |
+
|
10 |
+
|
11 |
+
# @app.get("/")
|
12 |
+
# def home():
|
13 |
+
# return {"message":"Hello World"}
|
14 |
+
|
15 |
+
# # Define a function to handle the GET request at `/generate`
|
16 |
+
|
17 |
+
|
18 |
+
# @app.get("/generate")
|
19 |
+
# def generate(text:str):
|
20 |
+
# ## use the pipeline to generate text from given input text
|
21 |
+
# output=pipe(text)
|
22 |
+
|
23 |
+
# ## return the generate text in Json reposne
|
24 |
+
# return {"output":output[0]['generated_text']}
|
25 |
+
|
26 |
+
from fastapi import FastAPI, Request, Form
|
27 |
+
from fastapi.responses import HTMLResponse
|
28 |
+
from fastapi.staticfiles import StaticFiles
|
29 |
+
from fastapi.templating import Jinja2Templates
|
30 |
from transformers import pipeline
|
31 |
|
32 |
+
# Create a new FastAPI app instance
|
33 |
+
app = FastAPI()
|
34 |
|
35 |
# Initialize the text generation pipeline
|
36 |
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
37 |
|
38 |
+
# Mount the static files directory
|
39 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
40 |
|
41 |
+
# Initialize the templates directory
|
42 |
+
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
|
43 |
|
44 |
+
@app.get("/", response_class=HTMLResponse)
|
45 |
+
async def home(request: Request):
|
46 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
47 |
|
48 |
+
@app.post("/generate", response_class=HTMLResponse)
|
49 |
+
async def generate(request: Request, text: str = Form(...)):
|
50 |
+
# Use the pipeline to generate text from given input text
|
51 |
+
output = pipe(text)
|
52 |
|
53 |
+
# Return the generated text in the template response
|
54 |
+
return templates.TemplateResponse("index.html", {"request": request, "output": output[0]['generated_text']})
|