File size: 4,540 Bytes
6f65709
 
 
5645205
6f65709
 
5645205
a99b8c6
2b852a9
a99b8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f65709
5645205
 
 
 
 
 
 
 
 
 
 
 
 
 
6f65709
a99b8c6
 
 
 
 
 
 
5645205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b852a9
 
5645205
 
 
 
 
2b852a9
5645205
 
 
6f65709
0b8ef34
 
 
a99b8c6
 
 
 
 
0b8ef34
 
a99b8c6
 
 
 
 
0b8ef34
 
 
2b852a9
0b8ef34
 
 
 
2b852a9
0b8ef34
6f65709
 
 
 
5645205
2b852a9
5645205
 
6f65709
2b852a9
0b8ef34
6f65709
 
 
 
0b8ef34
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
import json
import pandas as pd
from typing import List
import tempfile
import os
from pathlib import Path
import re
import shutil

def clean_json_content(content: str) -> str:
    """JSON ๋ฌธ์ž์—ด์—์„œ ์ฃผ์„์„ ์ œ๊ฑฐํ•˜๊ณ  JSON ํ˜•์‹์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค."""
    # ํ•œ ์ค„ ์ฃผ์„ ์ œ๊ฑฐ (#์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ค„)
    content = re.sub(r'^\s*#.*$', '', content, flags=re.MULTILINE)
    
    # ์—ฌ๋Ÿฌ ์ค„์˜ ์—ฐ์†๋œ ์ฝค๋งˆ ์ •๋ฆฌ
    content = re.sub(r'},\s*,\s*{', '},{', content)
    
    # ์‹œ์ž‘๊ณผ ๋์— ๋Œ€๊ด„ํ˜ธ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์ถ”๊ฐ€
    content = content.strip()
    if not content.startswith('['):
        content = '[' + content
    if not content.endswith(']'):
        content = content + ']'
    
    # ๋งˆ์ง€๋ง‰ ๊ฐ์ฒด ๋’ค์˜ ์ฝค๋งˆ ์ œ๊ฑฐ
    content = re.sub(r'},\s*]', '}]', content)
    
    return content

def process_json_files(files: List[tempfile._TemporaryFileWrapper]) -> tuple[str, str]:
    try:
        # ๋ชจ๋“  JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
        all_data = []
        
        # ์—…๋กœ๋“œ๋œ ๊ฐ ํŒŒ์ผ ์ฒ˜๋ฆฌ
        for file in files:
            try:
                # ํŒŒ์ผ ๋‚ด์šฉ ์ฝ๊ธฐ
                if hasattr(file, 'name'):  # ์‹ค์ œ ํŒŒ์ผ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ
                    with open(file.name, 'r', encoding='utf-8') as f:
                        content = f.read()
                else:  # ๋ฌธ์ž์—ด์ด๋‚˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ์ž…๋ ฅ์ธ ๊ฒฝ์šฐ
                    content = file
                
                # JSON ๋‚ด์šฉ ์ •๋ฆฌ
                cleaned_content = clean_json_content(content)
                
                try:
                    json_data = json.loads(cleaned_content)
                except json.JSONDecodeError as e:
                    return None, f"JSON ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {str(e)}\n์ •๋ฆฌ๋œ ๋‚ด์šฉ:\n{cleaned_content}"
                
                # ๋‹จ์ผ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜
                if isinstance(json_data, dict):
                    json_data = [json_data]
                    
                all_data.extend(json_data)
            except Exception as e:
                return None, f"ํŒŒ์ผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
        
        if not all_data:
            return None, "์ฒ˜๋ฆฌํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
        
        # DataFrame์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ค‘๋ณต ์ œ๊ฑฐ
        df = pd.DataFrame(all_data)
        df_deduplicated = df.drop_duplicates(subset=['repo'])
        
        # ๊ฒฐ๊ณผ๋ฅผ loras.json ํŒŒ์ผ๋กœ ์ €์žฅ
        output_path = "loras.json"
        result_json = df_deduplicated.to_dict('records')
        
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(result_json, f, ensure_ascii=False, indent=2)
        
        return output_path, f"์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต ์ œ๊ฑฐ ์ „ {len(all_data)}๊ฐœ, ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ {len(df_deduplicated)}๊ฐœ์˜ ํ•ญ๋ชฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค."
        
    except Exception as e:
        return None, f"์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"

def create_example_file():
    example_data = [
        {
            "image": "https://huggingface.co/strangerzonehf/Flux-Super-Realism-LoRA/resolve/main/images/1.png",
            "title": "Super Realism",
            "repo": "strangerzonehf/Flux-Super-Realism-LoRA",
            "weights": "super-realism.safetensors",
            "trigger_word": "Super Realism"            
        },
        {
            "image": "https://huggingface.co/prithivMLmods/Flux-Dalle-Mix-LoRA/resolve/main/images/D3.png",
            "title": "Dalle Mix",
            "repo": "prithivMLmods/Flux-Dalle-Mix-LoRA",
            "weights": "dalle-mix.safetensors",
            "trigger_word": "dalle-mix"           
        }
    ]
    
    example_path = "example_loras.json"
    
    with open(example_path, 'w', encoding='utf-8') as f:
        json.dump(example_data, f, ensure_ascii=False, indent=2)
    
    return example_path

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
iface = gr.Interface(
    fn=process_json_files,
    inputs=gr.File(file_count="multiple", label="JSON ํŒŒ์ผ ์—…๋กœ๋“œ (์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€๋Šฅ)"),
    outputs=[
        gr.File(label="loras.json ๋‹ค์šด๋กœ๋“œ"),
        gr.Textbox(label="์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ")
    ],
    title="JSON ํŒŒ์ผ ์ค‘๋ณต ์ œ๊ฑฐ ๋„๊ตฌ",
    description="repo ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ loras.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.\n์ฃผ์„์ด๋‚˜ ๋ถˆ์™„์ „ํ•œ JSON ํ˜•์‹๋„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.",
    examples=[[create_example_file()]]
)

# ์•ฑ ์‹คํ–‰
if __name__ == "__main__":
    iface.launch(share=True)