Files changed (5) hide show
  1. Dockerfile +13 -1
  2. main.py +17 -2
  3. static/index.html +36 -0
  4. static/script.js +24 -0
  5. static/style.css +46 -0
Dockerfile CHANGED
@@ -1,3 +1,4 @@
 
1
  FROM python:3.9
2
 
3
  WORKDIR /code
@@ -6,6 +7,17 @@ COPY ./requirements.txt /code/requirements.txt
6
 
7
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
- COPY . .
 
 
 
 
 
 
 
 
 
10
 
11
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
1
+
2
  FROM python:3.9
3
 
4
  WORKDIR /code
 
7
 
8
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
9
 
10
+ RUN useradd -m -u 1000 user
11
+
12
+ USER user
13
+
14
+ ENV HOME=/home/user \
15
+ PATH=/home/user/.local/bin:$PATH
16
+
17
+ WORKDIR $HOME/app
18
+
19
+ COPY --chown=user . $HOME/app
20
 
21
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
22
+
23
+
main.py CHANGED
@@ -1,7 +1,22 @@
 
1
  from fastapi import FastAPI
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def read_root():
7
- return {"Hello": "World!"}
 
 
1
+
2
  from fastapi import FastAPI
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.responses import FileResponse
5
+
6
+ from transformers import pipeline
7
 
8
  app = FastAPI()
9
 
10
+ pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
11
+
12
+ @app.get("/infer_t5")
13
+ def t5(input):
14
+ output = pipe_flan(input)
15
+ return {"output": output[0]["generated_text"]}
16
+
17
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
18
+
19
  @app.get("/")
20
+ def index() -> FileResponse:
21
+ return FileResponse(path="/app/static/index.html", media_type="text/html")
22
+
static/index.html ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Fast API 🤗 Space served with Uvicorn</title>
7
+ <link rel="stylesheet" href="style.css" />
8
+ <script type="module" src="script.js"></script>
9
+ </head>
10
+ <body>
11
+ <main>
12
+ <section id="text-gen">
13
+ <h1>Text generation using Flan T5</h1>
14
+ <p>
15
+ Model:
16
+ <a
17
+ href="https://huggingface.co/google/flan-t5-small"
18
+ rel="noreferrer"
19
+ target="_blank"
20
+ >google/flan-t5-small</a
21
+ >
22
+ </p>
23
+ <form class="text-gen-form">
24
+ <label for="text-gen-input">Text prompt</label>
25
+ <input
26
+ id="text-gen-input"
27
+ type="text"
28
+ value="English: Translate There are many ducks. German:"
29
+ />
30
+ <button id="text-gen-submit">Submit</button>
31
+ <p class="text-gen-output"></p>
32
+ </form>
33
+ </section>
34
+ </main>
35
+ </body>
36
+ </html>
static/script.js ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ const textGenForm = document.querySelector('.text-gen-form');
3
+
4
+ const translateText = async (text) => {
5
+ const inferResponse = await fetch(`infer_t5?input=${text}`);
6
+ const inferJson = await inferResponse.json();
7
+
8
+ return inferJson.output;
9
+ };
10
+
11
+ textGenForm.addEventListener('submit', async (event) => {
12
+ event.preventDefault();
13
+
14
+ const textGenInput = document.getElementById('text-gen-input');
15
+ const textGenParagraph = document.querySelector('.text-gen-output');
16
+
17
+ try {
18
+ textGenParagraph.textContent = await translateText(textGenInput.value);
19
+ } catch (err) {
20
+ console.error(err);
21
+ }
22
+ });
23
+
24
+
static/style.css ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ --text: hsl(0 0% 15%);
3
+ padding: 2.5rem;
4
+ font-family: sans-serif;
5
+ color: var(--text);
6
+ }
7
+
8
+ body.dark-theme {
9
+ --text: hsl(0 0% 90%);
10
+ background-color: hsl(223 39% 7%);
11
+ }
12
+
13
+ main {
14
+ max-width: 80rem;
15
+ text-align: center;
16
+ }
17
+
18
+ section {
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ }
23
+
24
+ a {
25
+ color: var(--text);
26
+ }
27
+
28
+ form {
29
+ width: 30rem;
30
+ margin: 0 auto;
31
+ }
32
+
33
+ input {
34
+ width: 100%;
35
+ }
36
+
37
+ button {
38
+ cursor: pointer;
39
+ }
40
+
41
+ .text-gen-output {
42
+ min-height: 1.2rem;
43
+ margin: 1rem;
44
+ border: 0.5px solid grey;
45
+ }
46
+