tesfdsfsf / app.py
openfree's picture
Update app.py
2b852a9 verified
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)