Spaces:
Sleeping
Sleeping
File size: 3,128 Bytes
a482109 ea38d67 c1d6bf9 ea38d67 ee477df 7be3f2e bb2559f 56ce194 ea38d67 ee477df ea38d67 ee477df 56ce194 a482109 bb2559f c1d6bf9 bb2559f 00d2b86 21fb350 0dc1c29 c1d6bf9 d9bffba c1d6bf9 d9bffba c1d6bf9 56ce194 a482109 ee477df a482109 bb2559f c1d6bf9 6cdbae5 c1d6bf9 bb2559f 00d2b86 c1d6bf9 |
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 81 |
import json
import os
import subprocess
import tempfile
from fastapi import FastAPI, HTTPException, Request
from huggingface_hub import HfApi, login
import glob
app = FastAPI()
KEY = os.environ.get("WEBHOOK_SECRET")
HF_READ = os.environ.get("HF_READ")
api = HfApi(token=HF_READ)
login(HF_READ)
LATEST_PAYLOAD = "latest_payload.json"
def look_up_tflite(repo_id):
with tempfile.TemporaryDirectory() as tmpdir:
repo_url = f"https://huggingface.co/{repo_id}"
subprocess.run(["git", "clone", repo_url, tmpdir], check=True)
result = subprocess.run(["ls", "-l", tmpdir], capture_output=True, text=True)
tflite_files = glob.glob(os.path.join(tmpdir, "*.tflite"))
tflite_message = ""
for tflite_path in tflite_files:
tflite_file = os.path.basename(tflite_path)
print(f"Found tflite file: {tflite_file}")
tflite_message += f" ⚙️ Found tflite file **{tflite_file}** - Running cycle estimation\n"
return f"```\n{result.stdout}\n```\n{tflite_message}\n(This is an automated message)"
def post_discussion_comment(repo_id, discussion_num, message):
api.comment_discussion(
repo_id=repo_id,
discussion_num=discussion_num,
comment=message
)
@app.get("/")
def greet_json():
try:
with open(LATEST_PAYLOAD, "r") as file:
payload = json.load(file)
except FileNotFoundError:
payload = {"message": "No payload received yet"}
except json.JSONDecodeError:
payload = {"message": "Error reading payload file"}
return payload
@app.post("/benchmark")
async def handle_webhook(request: Request):
secret = request.headers.get("X-Webhook-Secret")
if secret != KEY:
raise HTTPException(status_code=403, detail="Invalid webhook secret")
try:
payload = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON payload")
with open(LATEST_PAYLOAD, "w") as file:
json.dump(payload, file)
# Check for "automatic model conversion succeeded" in the message
message = payload.get("comment", {}).get("content", "")
if "an automatic model conversion and evaluation has been performed and suceeded" in message:
# Extract repo and discussion info from payload
repo_id = payload.get("repo", {}).get("name", "gbahlnxp/mobilenetv1")
if not repo_id:
raise HTTPException(status_code=400, detail="Repository name missing in payload")
discussion_num = payload.get("discussion", {}).get("num")
if not discussion_num:
raise HTTPException(status_code=400, detail="Discussion num missing in payload")
try:
ls_result = look_up_tflite(repo_id)
post_discussion_comment(repo_id, discussion_num, f"❗A conversion was successful for Repo **{repo_id}** \n Starting cycle estimation \n Cloning the repo \n {ls_result}")
except Exception as e:
post_discussion_comment(repo_id, discussion_num, f"Error running ls: {e}")
return {"status": "success", "received": payload} |