File size: 1,654 Bytes
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
52
53
54
55
56
57
58
import requests
import json
import gradio as gr


def fetch_tags(image_url):
    # Extract image ID from the provided URL
    try:
        image_id = image_url.split('/')[-1].split('.')[0]
        if not image_id.isdigit():
            return '[ERROR]: Invalid image URL format.'
    except IndexError:
        return '[ERROR]: Invalid image URL format.'

    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
    prompt = f'{character} {origin} {tags}'
    formatted_tags = tags.replace(" ", ", ")
    formatted_prompt = prompt.replace(" ", ", ")

    # Display the results
    return {
        "Character": character,
        "Origin": origin,
        "Tags": formatted_tags,
        "Prompt": formatted_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 Fetcher",
    description="Enter a Danbooru image URL to fetch character, origin, tags, and prompt information."
)

# Launch the interface
iface.launch()