ultrin commited on
Commit
d8a0eb6
1 Parent(s): b3be359

flan t5 model test

Browse files
Files changed (5) hide show
  1. Dockerfile +10 -1
  2. main.py +15 -2
  3. static/index.html +24 -0
  4. static/script.js +17 -0
  5. static/style.css +0 -0
Dockerfile CHANGED
@@ -9,6 +9,15 @@ COPY ./requirements.txt /code/requirements.txt
9
 
10
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
 
12
- COPY . .
 
 
 
 
 
 
 
 
 
13
 
14
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
9
 
10
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
 
12
+ RUN useradd -m -u 1000 user
13
+
14
+ USER user
15
+
16
+ ENV HOME=/home/user \
17
+ PATH=/home/user/.local/bin:$PATH
18
+
19
+ WORKDIR $HOME/app
20
+
21
+ COPY --chown=user . $HOME/app
22
 
23
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py CHANGED
@@ -1,7 +1,20 @@
1
  from fastapi import FastAPI
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def read_root():
7
- return {"Hello": "World!"}
 
1
  from fastapi import FastAPI
2
+ from fastapi.staticfiles import StaticFiles
3
+ from fastapi.responses import FileResponse
4
+
5
+ from transformers import pipeline
6
 
7
  app = FastAPI()
8
 
9
+ pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
10
+
11
+ @app.get("/infer_t5")
12
+ def t5(input):
13
+ output = pipe_flan(input)
14
+ return {"output": output[0]["generated_text"]}
15
+
16
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
17
+
18
  @app.get("/")
19
+ def index() -> FileResponse:
20
+ return FileResponse(path="/app/static/index.html", media_type="text/html")
static/index.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <main>
2
+ <section id="text-gen">
3
+ <h2>Text generation using Flan T5</h2>
4
+ <p>
5
+ Model:
6
+ <a
7
+ href="https://huggingface.co/google/flan-t5-small"
8
+ rel="noreferrer"
9
+ target="_blank"
10
+ >google/flan-t5-small
11
+ </a>
12
+ </p>
13
+ <form class="text-gen-form">
14
+ <label for="text-gen-input">Text prompt</label>
15
+ <input
16
+ id="text-gen-input"
17
+ type="text"
18
+ value="German: There are many ducks"
19
+ />
20
+ <button id="text-gen-submit">Submit</button>
21
+ <p class="text-gen-output"></p>
22
+ </form>
23
+ </section>
24
+ </main>
static/script.js ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const textGenForm = document.querySelector(".text-gen-form");
2
+
3
+ const translateText = async (text) => {
4
+ const inferResponse = await fetch(`infer_t5?input=${text}`);
5
+ const inferJson = await inferResponse.json();
6
+
7
+ return inferJson.output;
8
+ };
9
+
10
+ textGenForm.addEventListener("submit", async (event) => {
11
+ event.preventDefault();
12
+
13
+ const textGenInput = document.getElementById("text-gen-input");
14
+ const textGenParagraph = document.querySelector(".text-gen-output");
15
+
16
+ textGenParagraph.textContent = await translateText(textGenInput.value);
17
+ });
static/style.css ADDED
File without changes