Spaces:
Sleeping
Sleeping
git.name
commited on
Commit
•
4564178
1
Parent(s):
a5a6052
initial commit
Browse files- Dockerfile +24 -0
- main.py +26 -0
- requirements.txt +7 -0
- static/index.html +36 -0
- static/script.js +17 -0
- static/style.css +0 -0
Dockerfile
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Create a non-root user and switch to it
|
10 |
+
RUN useradd -m -u 1000 user
|
11 |
+
USER user
|
12 |
+
|
13 |
+
# Set environment variables
|
14 |
+
ENV HOME=/home/user \
|
15 |
+
PATH=/home/user/.local/bin:$PATH \
|
16 |
+
HF_HOME=/home/user/.cache/huggingface/transformers
|
17 |
+
|
18 |
+
WORKDIR $HOME/app
|
19 |
+
|
20 |
+
# Ensure the user owns the copied files
|
21 |
+
COPY --chown=user . $HOME/app
|
22 |
+
|
23 |
+
# Command to run the application with Uvicorn
|
24 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
from starlette.responses import FileResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Initialize the pipeline
|
11 |
+
pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
|
12 |
+
|
13 |
+
# Serve static files
|
14 |
+
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
15 |
+
|
16 |
+
# Serve index.html at the root
|
17 |
+
@app.get("/", response_class=HTMLResponse)
|
18 |
+
async def read_index():
|
19 |
+
return FileResponse("static/index.html")
|
20 |
+
|
21 |
+
# Endpoint for text-to-text generation
|
22 |
+
@app.get("/generate-text")
|
23 |
+
async def generate_text(input: str):
|
24 |
+
output = pipe_flan(input)
|
25 |
+
print(output)
|
26 |
+
return {"output": output[0]["generated_text"]}
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
requests==2.27.*
|
3 |
+
sentencepiece==0.1.*
|
4 |
+
torch==1.11.*
|
5 |
+
transformers==4.*
|
6 |
+
uvicorn[standard]==0.17.*
|
7 |
+
|
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>Text Generation Form</title>
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<main>
|
10 |
+
<section id="text-gen">
|
11 |
+
<h2>Text generation using Flan T5</h2>
|
12 |
+
<p>
|
13 |
+
Model:
|
14 |
+
<a
|
15 |
+
href="https://huggingface.co/google/flan-t5-small"
|
16 |
+
rel="noreferrer"
|
17 |
+
target="_blank">google/flan-t5-small
|
18 |
+
</a>
|
19 |
+
</p>
|
20 |
+
<form class="text-gen-form" id="textGenForm">
|
21 |
+
<label for="text-gen-input">Text prompt:</label>
|
22 |
+
<input
|
23 |
+
id="text-gen-input"
|
24 |
+
name="text"
|
25 |
+
type="text"
|
26 |
+
placeholder="Tell me a joke for today"
|
27 |
+
/>
|
28 |
+
<button type="submit" id="text-gen-submit">Submit</button>
|
29 |
+
</form>
|
30 |
+
<p id="text-gen-output"></p>
|
31 |
+
</section>
|
32 |
+
</main>
|
33 |
+
|
34 |
+
<script src="static/script.js"></script>
|
35 |
+
</body>
|
36 |
+
</html>
|
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(`generate-text?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
|