Rsnarsna commited on
Commit
ba92c64
·
verified ·
1 Parent(s): b661091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -12
app.py CHANGED
@@ -1,24 +1,67 @@
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']}
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse
3
  from transformers import pipeline
4
 
5
+ # Initialize the FastAPI app
6
+ app = FastAPI()
7
 
8
  # Initialize the text generation pipeline
9
  pipe = pipeline("text2text-generation", model="google/flan-t5-small")
10
 
11
 
12
+ @app.get("/", response_class=HTMLResponse)
13
  def home():
14
+ """
15
+ Renders a simple HTML page with a text input field and a submit button.
16
+ """
17
+ return """
18
+ <!DOCTYPE html>
19
+ <html lang="en">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
23
+ <title>Text Generation</title>
24
+ </head>
25
+ <body>
26
+ <h1>Text Generation with FastAPI and Hugging Face Transformers</h1>
27
+ <form action="/generate" method="get">
28
+ <label for="text">Enter Text:</label><br><br>
29
+ <input type="text" id="text" name="text" required><br><br>
30
+ <button type="submit">Generate</button>
31
+ </form>
32
+ <div id="result"></div>
33
+ </body>
34
+ </html>
35
+ """
36
 
 
37
 
38
+ @app.get("/generate", response_class=HTMLResponse)
39
+ def generate(text: str):
40
+ """
41
+ Handles the text generation and returns the generated output in HTML format.
42
+ """
43
+ # Use the pipeline to generate text from the given input text
44
+ output = pipe(text)
45
 
46
+ # Prepare the result as an HTML response
47
+ generated_text = output[0]['generated_text']
48
+ return f"""
49
+ <!DOCTYPE html>
50
+ <html lang="en">
51
+ <head>
52
+ <meta charset="UTF-8">
53
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
54
+ <title>Generated Output</title>
55
+ </head>
56
+ <body>
57
+ <h1>Generated Text</h1>
58
+ <p><strong>Input:</strong> {text}</p>
59
+ <p><strong>Output:</strong> {generated_text}</p>
60
+ <br>
61
+ <a href="/">Go Back</a>
62
+ </body>
63
+ </html>
64
+ """
65
 
66
+
67
+ # Run the app with: uvicorn your_filename:app --reload