yoon-gu commited on
Commit
55efc3b
โ€ข
1 Parent(s): ce2e19a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -64
app.py CHANGED
@@ -25,74 +25,83 @@ GEN_RANGE = {
25
  "9์„ธ๋Œ€": [906, 1017]
26
  }
27
 
28
- generation = gr.Dropdown(
29
- [f"{k}์„ธ๋Œ€" for k in range(1, 10)] + ["๋ชจ๋“  ์„ธ๋Œ€"], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
30
- )
31
- poke_types = gr.Dropdown(
32
- pokemons_types, value="๋ชจ๋“  ํƒ€์ž…", label="ํฌ์ผ“๋ชฌ ํƒ€์ž…", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ํƒ€์ž…์„ ์„ ํƒํ•˜์„ธ์š”."
33
- )
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- download = gr.File(label="ํŒŒ๋ž€ ๊ธ€์”จ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์„ธ์š”.")
36
- text = gr.DataFrame(label="ํฌ์ผ“๋ชฌ ๋ฆฌ์ŠคํŠธ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- def write_pdf(gen, poke_type, progress=gr.Progress()):
39
- filename = f'ํฌ์ผ“๋ชฌ{gen}_{poke_type}.pdf'
40
- pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
41
- # PDF ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
42
- pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
43
- doc = SimpleDocTemplate(filename, pagesize=A4)
44
-
45
- # ํ…Œ์ด๋ธ” ์Šคํƒ€์ผ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
46
- style = TableStyle([
47
- ('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
48
- ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
49
- ('FONTNAME', (0, 0), (-1, -1), '๋‚˜๋ˆ”๊ณ ๋”•'),
50
- ('SIZE', (0, 0), (-1, -1), 50),
51
- ('BACKGROUND', (0, 1), (-1, 1), colors.white),
52
- ('GRID', (0, 0), (-1, -1), 1, colors.black),
53
- ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
54
- ('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ„๊ฒฉ ์กฐ์ •
55
- ('BOTTOMPADDING', (0, 0), (-1, -1), 60),
56
- ])
57
 
58
- story = []
59
- data_dict = []
60
- start, end = GEN_RANGE[gen]
61
- for k in progress.tqdm(range(start, end+1)):
62
- name = pokemons[k-1]['name']
63
- number = pokemons[k-1]['number']
64
- types = pokemons[k-1]['types']
65
- image_path = pokemons[k-1]['image_path']
 
66
 
67
- if poke_type == "๋ชจ๋“  ํƒ€์ž…":
68
- condition = True
69
- else:
70
- condition = poke_type in types
71
- if condition:
72
- data_dict.append(
73
- dict(์ด๋ฆ„=name, No=number, ํƒ€์ž…='+'.join(types))
74
- )
75
 
76
- # PDF์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.
77
- image = Image(image_path, width=13.5*cm, height=13.5*cm)
 
 
 
 
 
 
 
 
 
78
 
79
- # ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
80
- data = [
81
- [number],
82
- [image], # 2๋ฒˆ์งธ ํ–‰์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
83
- ['+'.join(types)],
84
- [name],
85
- ]
86
- # ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
87
- table = Table(data)
88
- table.setStyle(style)
89
- story.append(table)
90
 
91
- doc.build(story)
92
- df = pd.DataFrame(data_dict)
93
- return df[['No', '์ด๋ฆ„', 'ํƒ€์ž…']], filename
94
-
95
- demo = gr.Interface(write_pdf, [generation, poke_types], [text, download], title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ",
96
- description="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
97
- demo.queue(concurrency_count=3)
98
- demo.launch()
 
25
  "9์„ธ๋Œ€": [906, 1017]
26
  }
27
 
28
+ with gr.Blocks(title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ ๐Ÿ™") as block:
29
+ with gr.Row():
30
+ gr.Markdown("# ๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ ๐Ÿ™")
31
+ with gr.Row():
32
+ generation = gr.Dropdown(
33
+ [f"{k}์„ธ๋Œ€" for k in range(1, 10)] + ["๋ชจ๋“  ์„ธ๋Œ€"], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
34
+ )
35
+ poke_types = gr.Dropdown(
36
+ pokemons_types, value="๋ชจ๋“  ํƒ€์ž…", label="ํฌ์ผ“๋ชฌ ํƒ€์ž…", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ํƒ€์ž…์„ ์„ ํƒํ•˜์„ธ์š”."
37
+ )
38
+ button = gr.Button(label="๐ŸŽฉ ๋„๊ฐ ์ƒ์„ฑ")
39
+ with gr.Row():
40
+ with gr.Column():
41
+ download = gr.File(label="ํŒŒ๋ž€ ๊ธ€์”จ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์„ธ์š”.")
42
+ df_view = gr.DataFrame(label="ํฌ์ผ“๋ชฌ ๋ฆฌ์ŠคํŠธ", wrap=True, row_count=10)
43
+ with gr.Column():
44
+ gallery = gr.Gallery(columns=4, label="ํฌ์ผ“๋ชฌ ๊ฐค๋Ÿฌ๋ฆฌ")
45
+ report = gr.Markdown("## ํƒ€์ž…๋ณ„ ํฌ์ผ“๋ชฌ ๋ถ„์„")
46
 
47
+ def write_pdf(gen, poke_type):
48
+ filename = f'ํฌ์ผ“๋ชฌ{gen}_{poke_type}.pdf'
49
+ # PDF ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
50
+ pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
51
+ doc = SimpleDocTemplate(filename, pagesize=A4)
52
+
53
+ # ํ…Œ์ด๋ธ” ์Šคํƒ€์ผ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
54
+ style = TableStyle([
55
+ ('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
56
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
57
+ ('FONTNAME', (0, 0), (-1, -1), '๋‚˜๋ˆ”๊ณ ๋”•'),
58
+ ('SIZE', (0, 0), (-1, -1), 50),
59
+ ('BACKGROUND', (0, 1), (-1, 1), colors.white),
60
+ ('GRID', (0, 0), (-1, -1), 1, colors.black),
61
+ ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
62
+ ('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ„๊ฒฉ ์กฐ์ •
63
+ ('BOTTOMPADDING', (0, 0), (-1, -1), 60),
64
+ ])
65
 
66
+ images = []
67
+ story = []
68
+ data_dict = []
69
+ start, end = GEN_RANGE[gen]
70
+ for k in range(start, end+1):
71
+ name = pokemons[k-1]['name']
72
+ number = pokemons[k-1]['number']
73
+ types = pokemons[k-1]['types']
74
+ image_path = pokemons[k-1]['image_path']
 
 
 
 
 
 
 
 
 
 
75
 
76
+ if poke_type == "๋ชจ๋“  ํƒ€์ž…":
77
+ condition = True
78
+ else:
79
+ condition = poke_type in types
80
+ if condition:
81
+ data_dict.append(
82
+ dict(์ด๋ฆ„=name, ๋ฒˆํ˜ธ=number, ํƒ€์ž…='+'.join(types))
83
+ )
84
+ images.append((image_path, f"{number} {name}"))
85
 
86
+ # PDF์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.
87
+ image = Image(image_path, width=13.5*cm, height=13.5*cm)
 
 
 
 
 
 
88
 
89
+ # ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
90
+ data = [
91
+ [number],
92
+ [image], # 2๋ฒˆ์งธ ํ–‰์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
93
+ ['+'.join(types)],
94
+ [name],
95
+ ]
96
+ # ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
97
+ table = Table(data)
98
+ table.setStyle(style)
99
+ story.append(table)
100
 
101
+ doc.build(story)
102
+ df = pd.DataFrame(data_dict)
103
+ return df[['๋ฒˆํ˜ธ', '์ด๋ฆ„', 'ํƒ€์ž…']], filename, images
 
 
 
 
 
 
 
 
104
 
105
+ button.click(write_pdf, [generation, poke_types], [df_view, download, gallery])
106
+ block.queue(concurrency_count=3)
107
+ block.launch()