File size: 2,393 Bytes
abf9047
 
 
 
 
 
61d63d5
 
abf9047
 
 
61d63d5
 
abf9047
 
 
 
 
 
 
 
61d63d5
 
 
 
abf9047
 
 
 
 
61d63d5
abf9047
 
 
 
 
 
 
 
 
 
61d63d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abf9047
61d63d5
abf9047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
import requests
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, urlparse

from inference import infer_t5
from dataset import query_emotion

# https://huggingface.co/settings/tokens
# https://huggingface.co/spaces/{username}/{space}/settings
API_TOKEN = os.getenv("BIG_GAN_TOKEN")


class RequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/":
            self.path = "index.html"

            return SimpleHTTPRequestHandler.do_GET(self)

        if self.path.startswith("/infer_biggan"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            input = query.get("input", None)[0]

            output = requests.request(
                "POST",
                "https://api-inference.huggingface.co/models/osanseviero/BigGAN-deep-128",
                headers={"Authorization": f"Bearer {API_TOKEN}"},
                data=json.dumps(input),
            )

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(output.content)

            return SimpleHTTPRequestHandler

        elif self.path.startswith("/infer_t5"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            input = query.get("input", None)[0]

            output = infer_t5(input)

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(json.dumps({"output": output}).encode("utf-8"))

            return SimpleHTTPRequestHandler

        elif self.path.startswith("/query_emotion"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            start = int(query.get("start", None)[0])
            end = int(query.get("end", None)[0])

            output = query_emotion(start, end)

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(json.dumps({"output": output}).encode("utf-8"))

            return SimpleHTTPRequestHandler

        else:
            return SimpleHTTPRequestHandler.do_GET(self)


server = ThreadingHTTPServer(("", 7860), RequestHandler)

server.serve_forever()