cbensimon's picture
cbensimon HF staff
Sync
3b04c28
raw
history blame
1.53 kB
from huggingface_hub import HfApi
from huggingface_hub import hf_hub_download
import huggingface_hub
from huggingface_hub import get_repo_discussions
from bs4 import BeautifulSoup
import json
import re
from fastapi import FastAPI
from fastapi import Request
repo_id = "dalle-mini/dalle-mini"
repo_id = "stabilityai/stable-diffusion"
dataset_repo_id = "triple-t/dummy"
path = "image_cache"
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(request: Request):
global items
payload = request.json()
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=path, 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",
)