|
import os |
|
os.system("pip install gradio==3.46.0") |
|
|
|
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("# λμΉλ ν¬μΌλͺ¬ λκ° μμ±κΈ° π\n- ν¬μΌλͺ¬μ μΈλλ³, νμ
λ³λ‘ λκ°μ λ§λ€μ΄μ£Όλ κ³³μ
λλ€.\n- μλ£μ μΆμ²λ http://pokemon.fandom.com μ
λλ€.") |
|
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(value="π© λκ° μμ±") |
|
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=3, label="π©π»βπ¨ν¬μΌλͺ¬ κ°€λ¬λ¦¬", rows=4, height=600) |
|
report = gr.Markdown("## νμ
λ³ ν¬μΌλͺ¬ λΆμ") |
|
|
|
def write_pdf(gen, poke_type): |
|
filename = f'ν¬μΌλͺ¬{gen}_{poke_type}.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}({'+'.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) |
|
analysis = df[['μ΄λ¦', 'νμ
']].groupby(by='νμ
').count().sort_values(by='μ΄λ¦', ascending=False).to_markdown().replace('μ΄λ¦', 'κ°μ') |
|
return df[['λ²νΈ', 'μ΄λ¦', 'νμ
']], filename, images, analysis |
|
|
|
button.click(write_pdf, [generation, poke_types], [df_view, download, gallery, report]) |
|
block.queue(concurrency_count=3) |
|
block.launch() |