jclyo1 commited on
Commit
ad4fcaa
0 Parent(s):

Duplicate from jclyo1/docker-sandbox

Browse files
Files changed (8) hide show
  1. .gitattributes +35 -0
  2. Dockerfile +47 -0
  3. README.md +11 -0
  4. main.py +63 -0
  5. requirements.txt +6 -0
  6. static/index.html +31 -0
  7. static/script.js +45 -0
  8. static/style.css +45 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #FROM python:3.9
2
+
3
+ # Using the Ubuntu image (our OS)
4
+ FROM ubuntu:latest
5
+ # Update package manager (apt-get)
6
+ # and install (with the yes flag `-y`)
7
+ # Python and Pip
8
+ RUN apt-get update && apt-get install -y \
9
+ python3-pip \
10
+ wget
11
+
12
+ WORKDIR /code
13
+
14
+ COPY ./requirements.txt /code/requirements.txt
15
+
16
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
17
+
18
+ RUN pip install diffusers transformers accelerate scipy safetensors
19
+
20
+
21
+
22
+ # Set up a new user named "user" with user ID 1000
23
+ RUN useradd -m -u 1000 user
24
+
25
+ # Switch to the "user" user
26
+ USER user
27
+ # Set home to the user's home directory
28
+ ENV HOME=/home/user \
29
+ PATH=/home/user/.local/bin:$PATH
30
+
31
+ # Set the working directory to the user's home directory
32
+ WORKDIR $HOME/app
33
+
34
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
35
+ COPY --chown=user . $HOME/app
36
+
37
+ # Download and uzip truepic-size from Google Drive
38
+ RUN pip install gdown
39
+ RUN gdown --id 1MdXMu8xSuG8WClY3JSgtL3Vy1IWTmWZO
40
+ RUN tar -xf truepic-sign-v0.1.0-ubuntu-latest.tar.gz
41
+ RUN chmod +x truepic-sign
42
+
43
+ WORKDIR $HOME/app
44
+
45
+ # COPY . .
46
+
47
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Docker Sandbox
3
+ emoji: 🚀
4
+ colorFrom: pink
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ duplicated_from: jclyo1/docker-sandbox
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.staticfiles import StaticFiles
3
+ from fastapi.responses import FileResponse
4
+
5
+ import platform
6
+ import subprocess
7
+ import logging
8
+ import urllib.request
9
+ import os
10
+ import json
11
+
12
+ import torch
13
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
14
+
15
+
16
+ app = FastAPI()
17
+
18
+ @app.get("/generate")
19
+ def generate_image(prompt):
20
+ model_id = "stabilityai/stable-diffusion-2-1" #CompVis/stable-diffusion-v1-4
21
+
22
+ # Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
23
+ #pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
24
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
25
+ #pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
26
+ #pipe = pipe.to("cuda")
27
+
28
+ #prompt = "a photo of an astronaut riding a horse on mars"
29
+
30
+ image = pipe(prompt).images[0]
31
+
32
+ image.save("static/astronaut_rides_horse.png")
33
+
34
+ @app.get("/generate-picsum")
35
+ def generate_picsum(prompt):
36
+ local_filename, headers = urllib.request.urlretrieve(('https://picsum.photos/id/' + prompt + '/800/800'))
37
+
38
+ # Data to be written
39
+ assertion = {
40
+ "assertions": [
41
+ {
42
+ "label": "com.truepic.custom.ai",
43
+ "data": {
44
+ "model_name": "Picsum",
45
+ "model_version": "1.0",
46
+ "prompt": prompt
47
+ }
48
+ }
49
+ ]
50
+ }
51
+
52
+ json_object = json.dumps(assertion, indent=4)
53
+ with open("assertion.json", "w") as outfile:
54
+ outfile.write(json_object)
55
+
56
+ subprocess.check_output(['./truepic-sign', 'sign', local_filename, '--profile', 'demo', '--assertions', 'assertion.json', '--output', (os.getcwd() + '/static/output.jpg')])
57
+ return {"response": "success"}
58
+
59
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
60
+
61
+ @app.get("/")
62
+ def index() -> FileResponse:
63
+ 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,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ <script type="module" src="https://display.truepic.com/truepic_display.es.js"></script>
10
+ </head>
11
+ <body>
12
+ <main>
13
+ <section id="text-gen">
14
+ <h1><span style="text-decoration: line-through;">Text to Image</span> Picsum Images with Signed Attribution</h1>
15
+ <truepic-display><img src="/output.jpg" /></truepic-display>
16
+ <form class="text-gen-form" style="padding:40px 0;">
17
+ <label for="text-gen-input">Enter number between 0 and 999: </label>
18
+ <input
19
+ id="text-gen-input"
20
+ type="text"
21
+ value=""
22
+ style="margin:40px 0;"
23
+ />
24
+ <button id="text-gen-submit">Submit</button>
25
+ </form>
26
+
27
+ <form method="get" id="redirect-form" action="/"></form>
28
+ </section>
29
+ </main>
30
+ </body>
31
+ </html>
static/script.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const textGenForm = document.querySelector('.text-gen-form');
2
+
3
+ /*
4
+ const translateText = async (text) => {
5
+ const inferResponse = await fetch(`infer_t5?input=${text}`);
6
+ const inferJson = await inferResponse.json();
7
+
8
+ return inferJson.output;
9
+ };
10
+
11
+ textGenForm.addEventListener('submit', async (event) => {
12
+ event.preventDefault();
13
+
14
+ const textGenInput = document.getElementById('text-gen-input');
15
+ const textGenParagraph = document.querySelector('.text-gen-output');
16
+
17
+ try {
18
+ textGenParagraph.textContent = await translateText(textGenInput.value);
19
+ } catch (err) {
20
+ console.error(err);
21
+ }
22
+ });
23
+
24
+ */
25
+
26
+ const generateImage = async (text) => {
27
+ const inferResponse = await fetch(`generate-picsum?prompt=${text}`);
28
+ const inferJson = await inferResponse.json();
29
+
30
+ return inferJson.output;
31
+ };
32
+
33
+
34
+ textGenForm.addEventListener('submit', async (event) => {
35
+ event.preventDefault();
36
+
37
+ const textGenInput = document.getElementById('text-gen-input');
38
+
39
+ try {
40
+ const resp = await generateImage(textGenInput.value);
41
+ document.getElementById("redirect-form").submit();
42
+ } catch (err) {
43
+ console.error(err);
44
+ }
45
+ });
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
+ }