File size: 2,283 Bytes
b197a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# components/items.py
import gradio as gr
from image_utils import generate_item_image
from datetime import datetime
import json
import os

def save_to_gallery(content_type: str, data: dict, image_path: str):
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    os.makedirs("gallery", exist_ok=True)
    filename = f"gallery/{content_type}_{timestamp}.json"
    with open(filename, "w") as f:
        json.dump({"type": content_type, "data": data, "image": image_path}, f, indent=2)

def item_tab():
    with gr.TabItem("πŸͺ™ Loot Generator"):
        with gr.Row():
            with gr.Column():
                name = gr.Textbox(label="Item Name", placeholder="Amulet of Twilight")
                item_type = gr.Dropdown(label="Item Type", choices=["Weapon", "Armor", "Accessory", "Consumable", "Tool"], value="Accessory")
                rarity = gr.Dropdown(label="Rarity", choices=["Common", "Uncommon", "Rare", "Very Rare", "Legendary"], value="Rare")
                is_magical = gr.Checkbox(label="Magical", value=True)
                is_cursed = gr.Checkbox(label="Cursed", value=False)
                gen_button = gr.Button("✨ Generate Item Image")
                item_img = gr.Image(label="Item Image", height=512)

                export_button = gr.Button("πŸ’Ύ Save Item to Gallery")
                export_status = gr.Textbox(label="Export Status")

        def build_and_generate(name, item_type, rarity, is_magical, is_cursed):
            data = {
                "name": name,
                "item_type": item_type,
                "rarity": rarity,
                "magical": is_magical,
                "cursed": is_cursed
            }
            return generate_item_image(data, art_style="Concept Art")

        gen_button.click(
            build_and_generate,
            inputs=[name, item_type, rarity, is_magical, is_cursed],
            outputs=item_img
        )

        export_button.click(
            lambda n, t, r, m, c, img: save_to_gallery(
                "item",
                {"name": n, "item_type": t, "rarity": r, "magical": m, "cursed": c},
                img
            ) or "βœ… Item saved!",
            inputs=[name, item_type, rarity, is_magical, is_cursed, item_img],
            outputs=export_status
        )