Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def fetch_tags(image_url):
|
| 7 |
+
# Extract image ID from the provided URL
|
| 8 |
+
try:
|
| 9 |
+
image_id = image_url.split('/')[-1].split('.')[0]
|
| 10 |
+
if not image_id.isdigit():
|
| 11 |
+
return '[ERROR]: Invalid image URL format.'
|
| 12 |
+
except IndexError:
|
| 13 |
+
return '[ERROR]: Invalid image URL format.'
|
| 14 |
+
|
| 15 |
+
base_url = 'https://danbooru.donmai.us/posts'
|
| 16 |
+
response = requests.get(f'{base_url}/{image_id}.json')
|
| 17 |
+
|
| 18 |
+
if response.status_code != 200:
|
| 19 |
+
return f'[ERROR]: {response.status_code} - Failed to retrieve data.'
|
| 20 |
+
|
| 21 |
+
data = json.loads(response.text)
|
| 22 |
+
|
| 23 |
+
# Extract required fields
|
| 24 |
+
character = data.get('tag_string_character', 'N/A')
|
| 25 |
+
origin = data.get('tag_string_copyright', 'N/A')
|
| 26 |
+
tags = data.get('tag_string_general', '')
|
| 27 |
+
|
| 28 |
+
# Prepare prompt and formatted tags
|
| 29 |
+
prompt = f'{character} {origin} {tags}'
|
| 30 |
+
formatted_tags = tags.replace(" ", ", ")
|
| 31 |
+
formatted_prompt = prompt.replace(" ", ", ")
|
| 32 |
+
|
| 33 |
+
# Display the results
|
| 34 |
+
return {
|
| 35 |
+
"Character": character,
|
| 36 |
+
"Origin": origin,
|
| 37 |
+
"Tags": formatted_tags,
|
| 38 |
+
"Prompt": formatted_prompt
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Create a Gradio interface
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=fetch_tags,
|
| 45 |
+
inputs=gr.Textbox(label="Danbooru Image URL"),
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Textbox(label="Character"),
|
| 48 |
+
gr.Textbox(label="Origin"),
|
| 49 |
+
gr.Textbox(label="Tags (comma-separated)"),
|
| 50 |
+
gr.Textbox(label="Prompt (comma-separated)")
|
| 51 |
+
],
|
| 52 |
+
title="Danbooru Tag Fetcher",
|
| 53 |
+
description="Enter a Danbooru image URL to fetch character, origin, tags, and prompt information."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Launch the interface
|
| 57 |
+
iface.launch()
|