Saketh-Reddy commited on
Commit
51e9b93
1 Parent(s): b91808f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -12
main.py CHANGED
@@ -4,22 +4,22 @@ from typing import Optional
4
  from huggingface_hub import snapshot_download
5
  from fastapi import FastAPI, Header, HTTPException, Request, Response
6
  from huggingface_hub.hf_api import HfApi
7
-
8
- import admin
9
-
10
- if not admin.isUserAdmin():
11
- admin.runAsAdmin()
12
 
13
  app = FastAPI()
14
 
15
  api = HfApi()
16
- directory = os.getcwd()
17
 
18
- print(directory)
 
19
  @app.get("/")
20
  def read_root():
21
  return {"Hello": "World!"}
22
 
 
 
23
  @app.post("/webhook")
24
  async def webhook(request: Request):
25
  if request.method == "POST":
@@ -27,9 +27,26 @@ async def webhook(request: Request):
27
  return Response("Invalid secret", status_code=401)
28
  data = await request.json()
29
  if(data["event"]["action"]=="update" and data["event"]["scope"]=="repo.content" and data["repo"]["type"]=="model"):
30
- snapshot_download(repo_id="SakethTest/ThirdParty",local_dir="/code/ThirdParty")
31
- api.upload_folder(folder_path="/code/ThirdParty",repo_id="shellplc/ThirdParty",repo_type="model",commit_message="uploaded third party model",token="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")
32
- return {"processed": True}
33
- else:
34
- return {"processed": False}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
 
4
  from huggingface_hub import snapshot_download
5
  from fastapi import FastAPI, Header, HTTPException, Request, Response
6
  from huggingface_hub.hf_api import HfApi
7
+ from huggingface_hub import whoami
8
+ from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
9
+ from huggingface_hub import delete_repo
 
 
10
 
11
  app = FastAPI()
12
 
13
  api = HfApi()
 
14
 
15
+ token = "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"
16
+
17
  @app.get("/")
18
  def read_root():
19
  return {"Hello": "World!"}
20
 
21
+
22
+
23
  @app.post("/webhook")
24
  async def webhook(request: Request):
25
  if request.method == "POST":
 
27
  return Response("Invalid secret", status_code=401)
28
  data = await request.json()
29
  if(data["event"]["action"]=="update" and data["event"]["scope"]=="repo.content" and data["repo"]["type"]=="model"):
30
+ try:
31
+ _ = whoami(token)
32
+ # ^ this will throw if token is invalid
33
+
34
+ delete_repo(repo_id="shellplc/ThirdParty", token = token, repo_type="model",missing_ok=True)
35
+
36
+ r = requests.post(
37
+ f"{ENDPOINT}/api/models/SakethTest/ThirdParty/duplicate",
38
+ headers=build_hf_headers(token=token),
39
+ json={"repository": "shellplc/ThirdParty"},
40
+ )
41
+ hf_raise_for_status(r)
42
+
43
+ return {"processed": True}
44
+
45
+ except Exception as e:
46
+ return (
47
+ f"Error {e}",
48
+ None,
49
+ )
50
+
51
+ return {"processed": False}
52