Spaces:
Running
Running
phonefern
commited on
Commit
•
ef9a278
1
Parent(s):
cdd7729
4/10
Browse files- Dockerfile +12 -4
- app.py +21 -0
- static/index.html +24 -0
- static/script.js +17 -0
- static/style.css +0 -0
Dockerfile
CHANGED
@@ -1,13 +1,21 @@
|
|
|
|
|
|
|
|
1 |
FROM python:3.9
|
2 |
|
|
|
|
|
3 |
RUN useradd -m -u 1000 user
|
4 |
-
USER user
|
5 |
-
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
-
|
7 |
WORKDIR /app
|
8 |
|
9 |
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
|
12 |
COPY --chown=user . /app
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
FROM python:3.9
|
5 |
|
6 |
+
# The two following lines are requirements for the Dev Mode to be functional
|
7 |
+
# Learn more about the Dev Mode at https://huggingface.co/dev-mode-explorers
|
8 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
9 |
WORKDIR /app
|
10 |
|
11 |
COPY --chown=user ./requirements.txt requirements.txt
|
12 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
13 |
|
14 |
COPY --chown=user . /app
|
15 |
+
|
16 |
+
USER user
|
17 |
+
|
18 |
+
ENV HOME=/home/user \
|
19 |
+
PATH=/home/user/.local/bin:$PATH
|
20 |
+
|
21 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -5,3 +5,24 @@ app = FastAPI()
|
|
5 |
@app.get("/")
|
6 |
def greet_json():
|
7 |
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def greet_json():
|
7 |
return {"Hello": "World!"}
|
8 |
+
|
9 |
+
from fastapi.responses import FileResponse
|
10 |
+
from fastapi.staticfiles import StaticFiles
|
11 |
+
from transformers import pipeline
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
|
16 |
+
|
17 |
+
|
18 |
+
@app.get("/infer_t5")
|
19 |
+
def t5(input):
|
20 |
+
output = pipe_flan(input)
|
21 |
+
return {"output": output[0]["generated_text"]}
|
22 |
+
|
23 |
+
|
24 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
25 |
+
|
26 |
+
@app.get("/")
|
27 |
+
def index() -> FileResponse:
|
28 |
+
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
|