Ron Au commited on
Commit
abf9047
β€’
1 Parent(s): 8f75358

Initial Commit

Browse files
Files changed (7) hide show
  1. README.md +2 -6
  2. app.py +57 -0
  3. index.html +0 -0
  4. index.js +48 -0
  5. inference.py +10 -0
  6. requirements.txt +1 -0
  7. style.css +48 -0
README.md CHANGED
@@ -1,12 +1,8 @@
1
  ---
2
- title: Python Http Server
3
- emoji: πŸ‘€
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 2.9.0
8
  app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
1
  ---
2
+ title: Python HTTP Server
3
+ emoji: 🐍
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
 
7
  app_file: app.py
 
8
  ---
 
 
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
5
+ from urllib.parse import parse_qs, urlparse
6
+
7
+ from inference import t5_infer
8
+
9
+ # https://huggingface.co/settings/tokens
10
+ # https://huggingface.co/spaces/{username}/{space}/settings
11
+ API_TOKEN = os.getenv('BIG_GAN_TOKEN')
12
+
13
+ class RequestHandler(SimpleHTTPRequestHandler):
14
+ def do_GET(self):
15
+ if self.path == "/":
16
+ self.path = "index.html"
17
+
18
+ return SimpleHTTPRequestHandler.do_GET(self)
19
+
20
+ if self.path.startswith("/biggan_infer"):
21
+ input = parse_qs(urlparse(self.path).query).get("input", None)[0]
22
+
23
+ output = requests.request(
24
+ "POST",
25
+ "https://api-inference.huggingface.co/models/osanseviero/BigGAN-deep-128",
26
+ headers={"Authorization": f"Bearer {API_TOKEN}"},
27
+ data=json.dumps(input)
28
+ )
29
+
30
+ self.send_response(200)
31
+ self.send_header("Content-Type", "application/json")
32
+ self.end_headers()
33
+
34
+ self.wfile.write(output.content)
35
+
36
+ return SimpleHTTPRequestHandler
37
+
38
+ elif self.path.startswith("/t5_infer"):
39
+ input = parse_qs(urlparse(self.path).query).get("input", None)
40
+
41
+ output = t5_infer(input)
42
+
43
+ self.send_response(200)
44
+ self.send_header("Content-Type", "application/json")
45
+ self.end_headers()
46
+
47
+ self.wfile.write(json.dumps({"output": output}).encode("utf-8"))
48
+
49
+ return SimpleHTTPRequestHandler
50
+
51
+ else:
52
+ return SimpleHTTPRequestHandler.do_GET(self)
53
+
54
+
55
+ server = ThreadingHTTPServer(("", 7860), RequestHandler)
56
+
57
+ server.serve_forever()
index.html ADDED
The diff for this file is too large to render. See raw diff
 
index.js ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ if (document.location.search.includes('dark-theme=true')) {
2
+ document.body.classList.add('dark-theme');
3
+ }
4
+
5
+ const textToImage = async (text) => {
6
+ const inferenceResponse = await fetch(`/biggan_infer?input=${text}`);
7
+ const inferenceBlob = await inferenceResponse.blob();
8
+
9
+ return URL.createObjectURL(inferenceBlob);
10
+ };
11
+
12
+ const translateText = async (text) => {
13
+ const inferResponse = await fetch(`/t5_infer?input=${text}`);
14
+ const inferJson = await inferResponse.json();
15
+
16
+ return inferJson.output;
17
+ };
18
+
19
+ const imageGenSelect = document.getElementById('image-gen-input');
20
+ const imageGenImage = document.querySelector('.image-gen-output');
21
+ const textGenForm = document.querySelector('.text-gen-form');
22
+
23
+ imageGenSelect.addEventListener('change', async (event) => {
24
+ const value = event.target.value;
25
+
26
+ try {
27
+ imageGenImage.src = await textToImage(value);
28
+ } catch (err) {
29
+ console.error(err);
30
+ }
31
+ });
32
+
33
+ textGenForm.addEventListener('submit', async (event) => {
34
+ event.preventDefault();
35
+
36
+ const textGenInput = document.getElementById('text-gen-input');
37
+ const textGenParagraph = document.querySelector('.text-gen-output');
38
+
39
+ try {
40
+ textGenParagraph.textContent = await translateText(textGenInput.value);
41
+ } catch (err) {
42
+ console.error(err);
43
+ }
44
+ });
45
+
46
+ textToImage(imageGenSelect.value)
47
+ .then((image) => (imageGenImage.src = image))
48
+ .catch(console.error);
inference.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
2
+
3
+ tokenizer = T5Tokenizer.from_pretrained("t5-small")
4
+ model = T5ForConditionalGeneration.from_pretrained("t5-small")
5
+
6
+ def t5_infer(input):
7
+ input_ids = tokenizer(input, return_tensors="pt").input_ids
8
+ outputs = model.generate(input_ids)
9
+
10
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ sentencepiece==0.1
style.css ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ body {
4
+ --text: hsl(0 0% 15%);
5
+ font-family: sans-serif;
6
+ background-color: hsl(0 0% 96%);
7
+ color: var(--text);
8
+ }
9
+ body.dark-theme {
10
+ --text: hsl(0 0% 90%);
11
+ background-color: hsl(223 39% 7%);
12
+ }
13
+
14
+ main {
15
+ display: flex;
16
+ flex-direction: column;
17
+ align-items: center;
18
+ max-width: 80rem;
19
+ text-align: center;
20
+ }
21
+
22
+ a {
23
+ color: var(--text);
24
+ }
25
+
26
+ select, input, button, .text-gen-output {
27
+ padding: 0.5rem 1rem;
28
+ }
29
+
30
+ select, img, input {
31
+ margin: 0.5rem auto 1rem;
32
+ }
33
+
34
+ form {
35
+ width: 25rem;
36
+ margin: 0 auto;
37
+ }
38
+
39
+ input {
40
+ width: 70%;
41
+ }
42
+
43
+ .text-gen-output {
44
+ min-height: 1rem;
45
+ margin: 0;
46
+ align-self: start;
47
+ border: 2px solid var(--text);
48
+ }