ekinnk commited on
Commit
0d8b303
0 Parent(s):

Duplicate from ekinnk/demo1

Browse files
Files changed (8) hide show
  1. .gitattributes +34 -0
  2. Dockerfile +27 -0
  3. README.md +11 -0
  4. main.py +23 -0
  5. requirements.txt +6 -0
  6. static/index.html +36 -0
  7. static/script.js +21 -0
  8. static/style.css +45 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.9 image
2
+ FROM python:3.9
3
+
4
+ # Set the working directory to /code
5
+ WORKDIR /code
6
+
7
+ # Copy the current directory contents into the container at /code
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ # Install requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+ # Set up a new user named "user" with user ID 1000
14
+ RUN useradd -m -u 1000 user
15
+ # Switch to the "user" user
16
+ USER user
17
+ # Set home to the user's home directory
18
+ ENV HOME=/home/user \
19
+ PATH=/home/user/.local/bin:$PATH
20
+
21
+ # Set the working directory to the user's home directory
22
+ WORKDIR $HOME/app
23
+
24
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
25
+ COPY --chown=user . $HOME/app
26
+
27
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Demo1
3
+ emoji: 📊
4
+ colorFrom: gray
5
+ colorTo: indigo
6
+ sdk: docker
7
+ pinned: false
8
+ duplicated_from: ekinnk/demo1
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = gr.Interface.load("models/gpt2").launch()
10
+
11
+ pipe_flan = pipeline("text2text-generation", model="gpt2")
12
+
13
+ @app.get("/infer_gpt2")
14
+ def gpt2(input):
15
+ output = pipe_flan(input)
16
+ #return {"output": output[0]["generated_text"]}
17
+ return {"output": output["generated_text"]}
18
+
19
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
20
+
21
+ @app.get("/")
22
+ def index() -> FileResponse:
23
+ 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,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 GPT2</h1>
14
+ <p>
15
+ Model:
16
+ <a
17
+ href="https://huggingface.co/google/flan-t5-small"
18
+ rel="noreferrer"
19
+ target="_blank"
20
+ >gpt2</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,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const textGenForm = document.querySelector('.text-gen-form');
2
+
3
+ const translateText = async (text) => {
4
+ const inferResponse = await fetch(`infer_gpt2?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
+ try {
17
+ textGenParagraph.textContent = await translateText(textGenInput.value);
18
+ } catch (err) {
19
+ console.error(err);
20
+ }
21
+ });
static/style.css ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }