File size: 1,442 Bytes
d760545
 
 
a18602d
 
c2f31df
c0e7ebe
c2f31df
d760545
 
 
 
 
c2f31df
 
 
0cb9be7
a18602d
 
 
c2f31df
3b04c28
0cb9be7
0d68e6c
 
c0e7ebe
0cb9be7
654fde1
2636cc8
 
 
 
654fde1
0cb9be7
39c1111
d760545
 
 
5c39d5a
 
 
 
 
 
d760545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from huggingface_hub import HfApi
from huggingface_hub import hf_hub_download
import json
import re

from fastapi import FastAPI
from fastapi import Body

repo_id = "dalle-mini/dalle-mini"
repo_id = "stabilityai/stable-diffusion"
dataset_repo_id = "triple-t/dummy"
file_name = "_".join(repo_id.split("/")) + ".json"
api = HfApi()

app = FastAPI()

items = []

url_re = re.compile(r'https://s3\.amazonaws\.com/moonup/production/uploads/\d+-noauth\.jpeg')

@app.get("/")
def get_events():
    return items

@app.post("/webhook")
def webhook(payload: dict = Body(...)):
    global items
    event = payload["event"]
    if event["action"] != "create":
        return
    if event["scope"] != "discussion":
        return
    content = payload["comment"]["content"]
    title = payload["discussion"]["title"]
    image_urls = url_re.findall(content)

    data_dict = {"data": {"images": image_urls, "prompt": title}, "discussion_num": 1000}

    path = hf_hub_download(
        repo_id=dataset_repo_id,
        filename=file_name,
        cache_dir="/home/user/app/image_cache",
        repo_type="dataset",
    )

    with open(path, "r") as f:
        data = json.load(f)

    data.append(data_dict)

    with open(path, "w") as f:
        f.write(json.dumps(data, sort_keys=True, indent=4))

    api.upload_file(
        path_or_fileobj=path,
        path_in_repo=file_name,
        repo_id=dataset_repo_id,
        repo_type="dataset",
    )