File size: 1,555 Bytes
ce7702f
 
 
4118efa
ce7702f
 
 
4118efa
 
 
 
 
 
ce7702f
 
 
 
 
a5548af
ce7702f
 
 
 
 
 
 
 
 
 
a5548af
ce7702f
a5548af
ce7702f
 
 
 
 
 
 
 
 
 
 
 
a5548af
 
ce7702f
 
 
 
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
import requests
import json
import gradio as gr
import re


def fetch_tags(image_url):
    # Try to extract the image ID from the URL
    match = re.search(r'https://danbooru.donmai.us/posts/(\d+)', image_url)
    if not match:
        return '[ERROR]: Invalid image URL format. Please use a valid Danbooru URL.', '', '', ''

    image_id = match.group(1)  # Extract image ID from URL

    base_url = 'https://danbooru.donmai.us/posts'
    response = requests.get(f'{base_url}/{image_id}.json')

    if response.status_code != 200:
        return f'[ERROR]: {response.status_code} - Failed to retrieve data.', '', '', ''

    data = json.loads(response.text)

    # Extract required fields
    character = data.get('tag_string_character', 'N/A')
    origin = data.get('tag_string_copyright', 'N/A')
    tags = data.get('tag_string_general', '')

    # Prepare prompt and formatted tags
    formatted_tags = tags.replace(" ", ", ")
    prompt = f'{character}, {origin}, {formatted_tags}'

    return character, origin, formatted_tags, prompt


# Create a Gradio interface
iface = gr.Interface(
    fn=fetch_tags,
    inputs=gr.Textbox(label="Danbooru Image URL"),
    outputs=[
        gr.Textbox(label="Character"),
        gr.Textbox(label="Origin"),
        gr.Textbox(label="Tags (comma-separated)"),
        gr.Textbox(label="Prompt (comma-separated)")
    ],
    title="Danbooru Tag Extractor",
    description="Enter a Danbooru image URL to fetch and format character, origin, tags, and prompt information."
)

# Launch the interface
iface.launch()