Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,87 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import pandas as pd
|
| 4 |
-
from typing import List
|
| 5 |
import tempfile
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
-
def process_json_files(files: List[tempfile._TemporaryFileWrapper]) -> str:
|
| 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 |
# Gradio 인터페이스 생성
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=process_json_files,
|
| 42 |
inputs=gr.File(file_count="multiple", label="JSON 파일 업로드 (여러 개 가능)"),
|
| 43 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 44 |
title="JSON 파일 중복 제거 도구",
|
| 45 |
description="repo 값을 기준으로 중복을 제거한 JSON 파일을 생성합니다.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# 앱 실행
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import pandas as pd
|
| 4 |
+
from typing import List
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
|
| 9 |
+
def process_json_files(files: List[tempfile._TemporaryFileWrapper]) -> tuple[str, str]:
|
| 10 |
+
try:
|
| 11 |
+
# 모든 JSON 데이터를 저장할 리스트
|
| 12 |
+
all_data = []
|
| 13 |
+
|
| 14 |
+
# 업로드된 각 파일 처리
|
| 15 |
+
for file in files:
|
| 16 |
+
try:
|
| 17 |
+
# 파일 내용 읽기
|
| 18 |
+
if hasattr(file, 'name'): # 실제 파일 객체인 경우
|
| 19 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 20 |
+
content = f.read()
|
| 21 |
+
else: # 문자열이나 다른 형태의 입력인 경우
|
| 22 |
+
content = file
|
| 23 |
|
| 24 |
+
json_data = json.loads(content)
|
| 25 |
+
|
| 26 |
+
# 단일 객체인 경우 리스트로 변환
|
| 27 |
+
if isinstance(json_data, dict):
|
| 28 |
+
json_data = [json_data]
|
| 29 |
+
|
| 30 |
+
all_data.extend(json_data)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return None, f"파일 처리 중 오류 발생: {str(e)}"
|
| 33 |
+
|
| 34 |
+
if not all_data:
|
| 35 |
+
return None, "처리할 데이터가 없습니다."
|
| 36 |
+
|
| 37 |
+
# DataFrame으로 변환하여 중복 제거
|
| 38 |
+
df = pd.DataFrame(all_data)
|
| 39 |
+
df_deduplicated = df.drop_duplicates(subset=['repo'])
|
| 40 |
+
|
| 41 |
+
# 임시 디렉토리 생성
|
| 42 |
+
temp_dir = Path(tempfile.gettempdir()) / "json_dedup"
|
| 43 |
+
temp_dir.mkdir(parents=True, exist_ok=True)
|
| 44 |
+
|
| 45 |
+
# 결과를 임시 JSON 파일로 저장
|
| 46 |
+
output_path = temp_dir / "deduplicated_data.json"
|
| 47 |
+
result_json = df_deduplicated.to_dict('records')
|
| 48 |
+
|
| 49 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 50 |
+
json.dump(result_json, f, ensure_ascii=False, indent=2)
|
| 51 |
+
|
| 52 |
+
return str(output_path), f"성공적으로 처리되었습니다. 중복 제거 전 {len(all_data)}개, 중복 제거 후 {len(df_deduplicated)}개의 항목이 있습니다."
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return None, f"처리 중 오류가 발생했습니다: {str(e)}"
|
| 56 |
|
| 57 |
# Gradio 인터페이스 생성
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=process_json_files,
|
| 60 |
inputs=gr.File(file_count="multiple", label="JSON 파일 업로드 (여러 개 가능)"),
|
| 61 |
+
outputs=[
|
| 62 |
+
gr.File(label="처리된 JSON 파일 다운로드"),
|
| 63 |
+
gr.Textbox(label="처리 결과")
|
| 64 |
+
],
|
| 65 |
title="JSON 파일 중복 제거 도구",
|
| 66 |
description="repo 값을 기준으로 중복을 제거한 JSON 파일을 생성합니다.",
|
| 67 |
+
examples=[
|
| 68 |
+
[[
|
| 69 |
+
{
|
| 70 |
+
"image": "https://example.com/image1.jpg",
|
| 71 |
+
"title": "샘플1",
|
| 72 |
+
"repo": "repo1",
|
| 73 |
+
"trigger_word": "word1"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"image": "https://example.com/image2.jpg",
|
| 77 |
+
"title": "샘플2",
|
| 78 |
+
"repo": "repo1",
|
| 79 |
+
"trigger_word": "word2"
|
| 80 |
+
}
|
| 81 |
+
]]
|
| 82 |
+
]
|
| 83 |
)
|
| 84 |
|
| 85 |
# 앱 실행
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
iface.launch(share=True) # share=True를 추가하여 public 링크 생성
|