taigasan commited on
Commit
44eb8f9
·
verified ·
1 Parent(s): 976f0d9

Add e621 API test app

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gradio as gr
3
+ import json
4
+
5
+ USER_AGENT = "e621-hf-test/1.0 (by lain-studio on e621)"
6
+
7
+ def test_e621(rating: str, tags: str, count: int):
8
+ query_tags = f"rating:{rating}"
9
+ if tags.strip():
10
+ query_tags += "+" + tags.strip().replace(" ", "+")
11
+ query_tags += "+order:random"
12
+
13
+ url = "https://e621.net/posts.json"
14
+ params = {"limit": count, "tags": query_tags}
15
+ headers = {"User-Agent": USER_AGENT}
16
+
17
+ try:
18
+ resp = requests.get(url, params=params, headers=headers, timeout=10)
19
+ resp.raise_for_status()
20
+ posts = resp.json().get("posts", [])
21
+ results = []
22
+ for p in posts:
23
+ f = p["file"]
24
+ results.append({
25
+ "id": p["id"],
26
+ "rating": p["rating"],
27
+ "md5": f["md5"],
28
+ "url": f["url"],
29
+ "ext": f["ext"],
30
+ })
31
+ return json.dumps({"status": "ok", "ip_used": "space", "count": len(results), "posts": results}, indent=2)
32
+ except Exception as e:
33
+ return json.dumps({"status": "error", "error": str(e)})
34
+
35
+ demo = gr.Interface(
36
+ fn=test_e621,
37
+ inputs=[
38
+ gr.Textbox(value="s", label="Rating (s/q/e)"),
39
+ gr.Textbox(value="", label="Extra tags (optional)"),
40
+ gr.Slider(1, 5, value=2, step=1, label="Count"),
41
+ ],
42
+ outputs=gr.Textbox(label="Result", lines=20),
43
+ title="e621 API Test",
44
+ )
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch()