LouiSum commited on
Commit
e11f4ef
1 Parent(s): d00f0b5
Files changed (5) hide show
  1. Dockerfile +25 -0
  2. main.py +15 -0
  3. requirements.txt +6 -0
  4. static/index.html +24 -0
  5. static/script.js +17 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ # Set up a new user named "user" with user ID 1000
10
+ RUN useradd -m -u 1000 user
11
+
12
+ # Switch to the "user" user
13
+ USER user
14
+
15
+ # Set home to the user's home directory
16
+ ENV HOME=/home/user \
17
+ PATH=/home/user/.local/bin:$PATH
18
+
19
+ # Set the working directory to the user's home directory
20
+ WORKDIR $HOME/app
21
+
22
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
23
+ COPY --chown=user . $HOME/app
24
+
25
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from transformers import pipeline
3
+
4
+ pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
5
+
6
+ app = FastAPI()
7
+
8
+ @app.get("/infer_t5")
9
+ def t5(input):
10
+ output = pipe_flan(input)
11
+ return {"output": output[0]["generated_text"]}
12
+
13
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
14
+ def index() -> FileResponse:
15
+ return FileResponse(path="/app/static/index.html", media_type="text/html")
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ sentencepiece==0.1.*
4
+ torch==1.11.*
5
+ transformers==4.*
6
+ uvicorn[standard]==0.17.*
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
+ });