pokemon / app.py
yoon-gu's picture
Update app.py
55efc3b
raw
history blame
4.31 kB
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]
}
with gr.Blocks(title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ ๐Ÿ™") as block:
with gr.Row():
gr.Markdown("# ๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ ๐Ÿ™")
with gr.Row():
generation = gr.Dropdown(
[f"{k}์„ธ๋Œ€" for k in range(1, 10)] + ["๋ชจ๋“  ์„ธ๋Œ€"], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
)
poke_types = gr.Dropdown(
pokemons_types, value="๋ชจ๋“  ํƒ€์ž…", label="ํฌ์ผ“๋ชฌ ํƒ€์ž…", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ํƒ€์ž…์„ ์„ ํƒํ•˜์„ธ์š”."
)
button = gr.Button(label="๐ŸŽฉ ๋„๊ฐ ์ƒ์„ฑ")
with gr.Row():
with gr.Column():
download = gr.File(label="ํŒŒ๋ž€ ๊ธ€์”จ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์„ธ์š”.")
df_view = gr.DataFrame(label="ํฌ์ผ“๋ชฌ ๋ฆฌ์ŠคํŠธ", wrap=True, row_count=10)
with gr.Column():
gallery = gr.Gallery(columns=4, label="ํฌ์ผ“๋ชฌ ๊ฐค๋Ÿฌ๋ฆฌ")
report = gr.Markdown("## ํƒ€์ž…๋ณ„ ํฌ์ผ“๋ชฌ ๋ถ„์„")
def write_pdf(gen, poke_type):
filename = f'ํฌ์ผ“๋ชฌ{gen}_{poke_type}.pdf'
# PDF ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
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),
])
images = []
story = []
data_dict = []
start, end = GEN_RANGE[gen]
for k in 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, ๋ฒˆํ˜ธ=number, ํƒ€์ž…='+'.join(types))
)
images.append((image_path, f"{number} {name}"))
# PDF์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.
image = Image(image_path, width=13.5*cm, height=13.5*cm)
# ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
data = [
[number],
[image], # 2๋ฒˆ์งธ ํ–‰์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
['+'.join(types)],
[name],
]
# ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
table = Table(data)
table.setStyle(style)
story.append(table)
doc.build(story)
df = pd.DataFrame(data_dict)
return df[['๋ฒˆํ˜ธ', '์ด๋ฆ„', 'ํƒ€์ž…']], filename, images
button.click(write_pdf, [generation, poke_types], [df_view, download, gallery])
block.queue(concurrency_count=3)
block.launch()