cbensimon's picture
cbensimon HF staff
Fix
5c39d5a
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",
)