Spaces:
Sleeping
Sleeping
Commit
ยท
ef15d8b
1
Parent(s):
f6c62cd
Update demo to get actor_id
Browse files
app.py
CHANGED
|
@@ -1,55 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import os
|
|
|
|
| 4 |
|
| 5 |
-
# Secrets์์ Private Space URL๊ณผ Access Token ์ฝ๊ธฐ
|
| 6 |
PRIVATE_SPACE_URL = os.getenv("PRIVATE_SPACE_URL")
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
def fetch_actor_data():
|
| 10 |
if not PRIVATE_SPACE_URL:
|
| 11 |
-
return "Error:
|
| 12 |
if not HF_TOKEN:
|
| 13 |
-
return "Error:
|
| 14 |
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {HF_TOKEN}"
|
| 17 |
}
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
url = f"{PRIVATE_SPACE_URL}/api/actor"
|
| 22 |
-
response = requests.get(url, headers=headers)
|
| 23 |
|
| 24 |
if response.status_code == 200:
|
| 25 |
-
#
|
| 26 |
data = response.json()
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
actor_ids = []
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
if "actor_id" in item:
|
| 35 |
actor_ids.append(item["actor_id"])
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
-
return f"Error {response.status_code}: {response.text}"
|
| 41 |
except Exception as e:
|
| 42 |
return f"Request failed: {str(e)}"
|
| 43 |
|
| 44 |
-
# Gradio ์ธํฐํ์ด์ค ์ ์
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=fetch_actor_data,
|
| 47 |
-
inputs=None,
|
| 48 |
-
outputs="text",
|
| 49 |
title="Fetch Actor Data",
|
| 50 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# ์ฑ ์คํ
|
| 54 |
if __name__ == "__main__":
|
| 55 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
|
|
|
|
| 6 |
PRIVATE_SPACE_URL = os.getenv("PRIVATE_SPACE_URL")
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
def fetch_actor_data():
|
| 10 |
if not PRIVATE_SPACE_URL:
|
| 11 |
+
return "Error: PRIVATE_SPACE_URL not found. Check Secrets."
|
| 12 |
if not HF_TOKEN:
|
| 13 |
+
return "Error: HF_TOKEN not found. Check Secrets."
|
| 14 |
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {HF_TOKEN}"
|
| 17 |
}
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
response = requests.get(f"{PRIVATE_SPACE_URL}/api/actor", headers=headers)
|
|
|
|
|
|
|
| 21 |
|
| 22 |
if response.status_code == 200:
|
| 23 |
+
# ์๋ต JSON ํ์ฑ
|
| 24 |
data = response.json()
|
| 25 |
+
|
| 26 |
+
# ๊ธฐ๋ ๊ตฌ์กฐ: data["data"]["result"] => list of actor items
|
| 27 |
actor_ids = []
|
| 28 |
+
if (
|
| 29 |
+
"data" in data
|
| 30 |
+
and isinstance(data["data"], dict)
|
| 31 |
+
and "result" in data["data"]
|
| 32 |
+
and isinstance(data["data"]["result"], list)
|
| 33 |
+
):
|
| 34 |
+
for item in data["data"]["result"]:
|
| 35 |
if "actor_id" in item:
|
| 36 |
actor_ids.append(item["actor_id"])
|
| 37 |
+
if len(actor_ids) >= 10:
|
| 38 |
+
break
|
| 39 |
|
| 40 |
+
if actor_ids:
|
| 41 |
+
return f"Found actor_ids: {actor_ids}"
|
| 42 |
+
else:
|
| 43 |
+
# actor_id๊ฐ ์์ ๊ฒฝ์ฐ ๊ตฌ์กฐ ํ์
์ฉ ๋ฐํ
|
| 44 |
+
top_level_keys = list(data["data"].keys()) if "data" in data and isinstance(data["data"], dict) else []
|
| 45 |
+
first_item = data["data"]["result"][0] if "data" in data and "result" in data["data"] and len(data["data"]["result"]) > 0 else {}
|
| 46 |
+
return (
|
| 47 |
+
"No actor_id found.\n\n"
|
| 48 |
+
f"Keys in data['data']: {top_level_keys}\n\n"
|
| 49 |
+
"First item in data['data']['result']:\n" +
|
| 50 |
+
json.dumps(first_item, indent=2, ensure_ascii=False)
|
| 51 |
+
)
|
| 52 |
else:
|
| 53 |
+
return f"Error {response.status_code}\nResponse Text:\n{response.text}"
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Request failed: {str(e)}"
|
| 56 |
|
|
|
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=fetch_actor_data,
|
| 59 |
+
inputs=None,
|
| 60 |
+
outputs="text",
|
| 61 |
title="Fetch Actor Data",
|
| 62 |
+
description=(
|
| 63 |
+
"Fetches actor data from the private Hugging Face Space.\n"
|
| 64 |
+
"Assumes structure: {status_code:..., data:{count:..., result:[{actor_id:...}]}}\n"
|
| 65 |
+
"If no actor_id found, shows partial keys for debugging."
|
| 66 |
+
)
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
iface.launch()
|