|
import gradio as gr |
|
import json |
|
import pandas as pd |
|
from reportlab.lib import colors |
|
from reportlab.pdfbase import pdfmetrics |
|
from reportlab.pdfbase.ttfonts import TTFont |
|
from reportlab.platypus import Table, TableStyle, Image, SimpleDocTemplate, PageBreak |
|
from reportlab.lib import colors |
|
from reportlab.lib.pagesizes import A4 |
|
from reportlab.lib.units import cm |
|
|
|
with open('pokemon.json', 'r') as f: |
|
pokemons = json.load(f) |
|
pokemons_types = ["๋ชจ๋ ํ์
"] + sorted(set([t for poke in pokemons for t in poke['types']])) |
|
GEN_RANGE = { |
|
"๋ชจ๋ ์ธ๋": [1, 1017], |
|
"1์ธ๋": [1, 151], |
|
"2์ธ๋": [152, 251], |
|
"3์ธ๋": [252, 386], |
|
"4์ธ๋": [387, 493], |
|
"5์ธ๋": [494, 649], |
|
"6์ธ๋": [650, 721], |
|
"7์ธ๋": [722, 809], |
|
"8์ธ๋": [810, 905], |
|
"9์ธ๋": [906, 1017] |
|
} |
|
|
|
generation = gr.Dropdown( |
|
[f"{k}์ธ๋" for k in range(1, 10)] + ["๋ชจ๋ ์ธ๋"], value="1์ธ๋", label="ํฌ์ผ๋ชฌ ์ธ๋", info="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์." |
|
) |
|
poke_types = gr.Dropdown( |
|
pokemons_types, value="๋ชจ๋ ํ์
", label="ํฌ์ผ๋ชฌ ํ์
", info="์ํ๋ ํฌ์ผ๋ชฌ ํ์
์ ์ ํํ์ธ์." |
|
) |
|
|
|
download = gr.File(label="ํ๋ ๊ธ์จ๋ฅผ ๋๋ฌ์ ๋ค์ด๋ก๋ ๋ฐ์ผ์ธ์.") |
|
text = gr.DataFrame(label="ํฌ์ผ๋ชฌ ๋ฆฌ์คํธ") |
|
|
|
def write_pdf(gen, poke_type, progress=gr.Progress()): |
|
filename = f'ํฌ์ผ๋ชฌ{gen}_{poke_type}.pdf' |
|
pdfmetrics.registerFont(TTFont("๋๋๊ณ ๋", "NanumGothic.ttf")) |
|
|
|
pdfmetrics.registerFont(TTFont("๋๋๊ณ ๋", "NanumGothic.ttf")) |
|
doc = SimpleDocTemplate(filename, pagesize=A4) |
|
|
|
|
|
style = TableStyle([ |
|
('TEXTCOLOR', (0, 1), (-1, -1), colors.gray), |
|
('ALIGN', (0, 0), (-1, -1), 'CENTER'), |
|
('FONTNAME', (0, 0), (-1, -1), '๋๋๊ณ ๋'), |
|
('SIZE', (0, 0), (-1, -1), 50), |
|
('BACKGROUND', (0, 1), (-1, 1), colors.white), |
|
('GRID', (0, 0), (-1, -1), 1, colors.black), |
|
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), |
|
('LEADING', (0, 0), (-1, -1), 14), |
|
('BOTTOMPADDING', (0, 0), (-1, -1), 60), |
|
]) |
|
|
|
story = [] |
|
data_dict = [] |
|
start, end = GEN_RANGE[gen] |
|
for k in progress.tqdm(range(start, end+1)): |
|
name = pokemons[k-1]['name'] |
|
number = pokemons[k-1]['number'] |
|
types = pokemons[k-1]['types'] |
|
image_path = pokemons[k-1]['image_path'] |
|
|
|
if poke_type == "๋ชจ๋ ํ์
": |
|
condition = True |
|
else: |
|
condition = poke_type in types |
|
if condition: |
|
data_dict.append( |
|
dict(์ด๋ฆ=name, No=number, ํ์
='+'.join(types)) |
|
) |
|
|
|
|
|
image = Image(image_path, width=13.5*cm, height=13.5*cm) |
|
|
|
|
|
data = [ |
|
[number], |
|
[image], |
|
['+'.join(types)], |
|
[name], |
|
] |
|
|
|
table = Table(data) |
|
table.setStyle(style) |
|
story.append(table) |
|
|
|
doc.build(story) |
|
df = pd.DataFrame(data_dict) |
|
return df[['No', '์ด๋ฆ', 'ํ์
']], filename |
|
|
|
demo = gr.Interface(write_pdf, [generation, poke_types], [text, download], title="๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ", |
|
description="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ๊ณ , ๋ค์ด๋ก๋๋ฅผ ๋๋ฌ์ฃผ์ธ์.") |
|
demo.queue(concurrency_count=3) |
|
demo.launch() |