Sangwon78 commited on
Commit
fb699c2
Β·
verified Β·
1 Parent(s): 1d32711

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -44
app.py CHANGED
@@ -28,28 +28,31 @@ def call_api(content, system_message, max_tokens, temperature, top_p):
28
  )
29
  return response.choices[0].message["content"]
30
 
31
- #########################
32
- # 병합셀 μ•ˆμ „ 기둝 헬퍼
33
- #########################
34
- def set_value_in_merged_cell(ws, cell_coordinate, value):
35
  """
36
- 병합 μ…€ λ²”μœ„μ— ν•΄λ‹Ή cell_coordinateκ°€ ν¬ν•¨λ˜λ©΄,
37
- ν•΄λ‹Ή λ³‘ν•©λ²”μœ„μ˜ (top-left) μ…€μ—λ§Œ valueλ₯Ό κΈ°λ‘ν•œλ‹€.
38
- λ³‘ν•©λ˜μ§€ μ•Šμ•˜λ‹€λ©΄, cell_coordinate κ·Έ μžμ²΄μ— κΈ°λ‘ν•œλ‹€.
39
  """
40
- for merged_range in ws.merged_cells.ranges:
41
- if cell_coordinate in merged_range.coord:
42
- min_col, min_row, max_col, max_row = merged_range.bounds
43
- top_left_coord = ws.cell(row=min_row, column=min_col).coordinate
44
- ws[top_left_coord] = value
45
- return
46
- ws[cell_coordinate] = value
 
 
 
47
 
48
  ######################
49
  # 뢄석 ν•¨μˆ˜
50
  ######################
51
  def analyze_texts(difficulties, goals):
52
- # μ• λ‘œμ‚¬ν•­ 뢄석
53
  preview_difficulties = call_api(
54
  content=difficulties,
55
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ• λ‘œμ‚¬ν•­μ„ κ°„λ‹¨νžˆ λΆ„μ„ν•΄μ€˜.",
@@ -57,7 +60,7 @@ def analyze_texts(difficulties, goals):
57
  temperature=0.7,
58
  top_p=0.9,
59
  )
60
- # μ»¨μ„€νŒ… λͺ©ν‘œ 뢄석
61
  preview_goals = call_api(
62
  content=goals,
63
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ»¨μ„€νŒ… λͺ©ν‘œλ₯Ό κ°„λ‹¨νžˆ μš”μ•½ν•΄μ€˜.",
@@ -71,7 +74,7 @@ def analyze_texts(difficulties, goals):
71
  # μ—‘μ…€ 생성(λ‹€μš΄λ‘œλ“œ) ν•¨μˆ˜
72
  ######################
73
  def generate_excel(
74
- # 8개 μž…λ ₯κ°’
75
  consultant_name_rep,
76
  consultant_contact,
77
  consultant_address,
@@ -85,7 +88,7 @@ def generate_excel(
85
  photo1,
86
  photo2,
87
 
88
- # μ• λ‘œμ‚¬ν•­/λͺ©ν‘œ 뢄석결과
89
  preview_difficulties,
90
  preview_goals
91
  ):
@@ -93,34 +96,39 @@ def generate_excel(
93
  ws = wb.active
94
 
95
  ############################
96
- # 상단 정보 8개 좜λ ₯
97
- # (C4, G4, C5, G5, C6, G6, C7, G7)
98
  ############################
99
- set_value_in_merged_cell(ws, "C4", consultant_name_rep)
100
- set_value_in_merged_cell(ws, "G4", consultant_contact)
101
- set_value_in_merged_cell(ws, "C5", consultant_address)
102
- set_value_in_merged_cell(ws, "G5", consultant_time)
103
- set_value_in_merged_cell(ws, "C6", consultant_field)
104
- set_value_in_merged_cell(ws, "G6", consultant_round)
105
- set_value_in_merged_cell(ws, "C7", consultant_client_name)
106
- set_value_in_merged_cell(ws, "G7", consultant_region)
107
 
108
  ############################
109
- # μ• λ‘œμ‚¬ν•­(D11), μ»¨μ„€νŒ…λͺ©ν‘œ(D22)
110
  ############################
111
- set_value_in_merged_cell(ws, "D11", preview_difficulties)
112
- set_value_in_merged_cell(ws, "D22", preview_goals)
113
-
114
- # μžλ™ μ€„λ°”κΏˆ
 
 
 
 
 
 
 
 
 
 
 
115
  ws["D11"].alignment = Alignment(wrap_text=True)
116
  ws["D22"].alignment = Alignment(wrap_text=True)
117
-
118
- # 10cm κ·Όμ‚¬μΉ˜
119
- ws.column_dimensions["D"].width = 42
120
 
121
  ############################
122
- # 사진 μ‚½μž… (B117, F117)
123
- # (κ°€λ‘œ 8cm, μ„Έλ‘œ 5cm)
124
  ############################
125
  width_px = int(37.79 * 8) # 8cm
126
  height_px = int(37.79 * 5) # 5cm
@@ -144,7 +152,7 @@ def generate_excel(
144
  ws.add_image(img2_for_excel, "F117")
145
 
146
  ############################
147
- # μž„μ‹œ 파일 -> EXE.xlsx
148
  ############################
149
  output = BytesIO()
150
  wb.save(output)
@@ -195,8 +203,10 @@ def run_generate_excel(
195
  consultant_round,
196
  consultant_client_name,
197
  consultant_region,
 
198
  photo1,
199
  photo2,
 
200
  preview_difficulties,
201
  preview_goals
202
  )
@@ -207,7 +217,7 @@ def run_generate_excel(
207
  ######################
208
  def create_app():
209
  with gr.Blocks() as demo:
210
- gr.Markdown("## (1) 상단 정보 8개 μž…λ ₯ + (2) μ• λ‘œμ‚¬ν•­ 및 μ»¨μ„€νŒ… λͺ©ν‘œ μž…λ ₯ + (3) 사진 μ—…λ‘œλ“œ -> Excel λ‹€μš΄λ‘œλ“œ")
211
 
212
  with gr.Row():
213
  consultant_name_rep = gr.Textbox(label="μ»¨μ„€νŒ… 업체 및 λŒ€ν‘œ 이름")
@@ -229,7 +239,6 @@ def create_app():
229
 
230
  gr.Markdown("---")
231
 
232
- # 사진 2개 μ—…λ‘œλ“œ
233
  with gr.Row():
234
  photo_input_1 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
235
  photo_input_2 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
@@ -245,7 +254,7 @@ def create_app():
245
  analyze_button.click(
246
  fn=run_analyze,
247
  inputs=[difficulties_input, goals_input],
248
- outputs=[preview_difficulties_output, preview_goals_output],
249
  )
250
 
251
  download_button.click(
@@ -259,14 +268,16 @@ def create_app():
259
  consultant_round,
260
  consultant_client_name,
261
  consultant_region,
 
262
  photo_input_1,
263
  photo_input_2,
 
264
  difficulties_input,
265
  goals_input,
266
  preview_difficulties_output,
267
- preview_goals_output,
268
  ],
269
- outputs=[download_file],
270
  )
271
 
272
  return demo
 
28
  )
29
  return response.choices[0].message["content"]
30
 
31
+ ############################
32
+ # 병합 λ²”μœ„ κ°•μ œ ν•΄μ œ ν•¨μˆ˜
33
+ ############################
34
+ def unmerge_conflicting_cells(ws, cell_coords):
35
  """
36
+ cell_coords 리슀트 내에 μžˆλŠ” 각 μ’Œν‘œ(예: 'C4')κ°€
37
+ μ–΄λ–€ 병합 λ²”μœ„(merged_range)에 ν¬ν•¨λ˜μ–΄ 있으면,
38
+ ν•΄λ‹Ή λ²”μœ„λ₯Ό μ „λΆ€ unmerge 해버린닀.
39
  """
40
+ # 병합 λ²”μœ„λ₯Ό μˆœνšŒν•˜λ©΄μ„œ ν•΄μ œν•΄μ•Ό ν•˜λ―€λ‘œ,
41
+ # 병합 λͺ©λ‘μ„ list(...)둜 볡사 ν›„ μˆ˜μ •ν•΄μ•Ό 함
42
+ merged_ranges = list(ws.merged_cells.ranges)
43
+
44
+ for rng in merged_ranges:
45
+ # 예: rng.coord == 'C4:D42'
46
+ for c in cell_coords:
47
+ if c in rng.coord:
48
+ ws.unmerge_cells(str(rng.coord)) # κ°•μ œ ν•΄μ œ
49
+ break # ν•œλ²ˆ unmerge ν•˜λ©΄ ν•΄λ‹Ή rngλŠ” κΉ¨μ‘ŒμœΌλ―€λ‘œ break
50
 
51
  ######################
52
  # 뢄석 ν•¨μˆ˜
53
  ######################
54
  def analyze_texts(difficulties, goals):
55
+ # μ• λ‘œμ‚¬ν•­
56
  preview_difficulties = call_api(
57
  content=difficulties,
58
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ• λ‘œμ‚¬ν•­μ„ κ°„λ‹¨νžˆ λΆ„μ„ν•΄μ€˜.",
 
60
  temperature=0.7,
61
  top_p=0.9,
62
  )
63
+ # μ»¨μ„€νŒ… λͺ©ν‘œ
64
  preview_goals = call_api(
65
  content=goals,
66
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ»¨μ„€νŒ… λͺ©ν‘œλ₯Ό κ°„λ‹¨νžˆ μš”μ•½ν•΄μ€˜.",
 
74
  # μ—‘μ…€ 생성(λ‹€μš΄λ‘œλ“œ) ν•¨μˆ˜
75
  ######################
76
  def generate_excel(
77
+ # 8개 μž…λ ₯
78
  consultant_name_rep,
79
  consultant_contact,
80
  consultant_address,
 
88
  photo1,
89
  photo2,
90
 
91
+ # 뢄석 ν…μŠ€νŠΈ
92
  preview_difficulties,
93
  preview_goals
94
  ):
 
96
  ws = wb.active
97
 
98
  ############################
99
+ # 1) 병합 ν•΄μ œ (ν•„μš”ν•œ μ…€λ§Œ)
 
100
  ############################
101
+ # C4,G4,C5,G5,C6,G6,C7,G7 와 D11,D22
102
+ # -> 이 쀑 ν•˜λ‚˜λΌλ„ 병합 λ²”μœ„μ— ν¬ν•¨λ˜μ–΄ μžˆλ‹€λ©΄
103
+ # μ „λΆ€ unmerge 해버림
104
+ targets = ["C4","G4","C5","G5","C6","G6","C7","G7","D11","D22"]
105
+ unmerge_conflicting_cells(ws, targets)
 
 
 
106
 
107
  ############################
108
+ # 2) μœ„/μ•„λž˜ 정보 κΈ°μž…
109
  ############################
110
+ # (1) 상단 정보 (C4, G4, C5, G5, C6, G6, C7, G7)
111
+ ws["C4"] = consultant_name_rep
112
+ ws["G4"] = consultant_contact
113
+ ws["C5"] = consultant_address
114
+ ws["G5"] = consultant_time
115
+ ws["C6"] = consultant_field
116
+ ws["G6"] = consultant_round
117
+ ws["C7"] = consultant_client_name
118
+ ws["G7"] = consultant_region
119
+
120
+ # (2) μ• λ‘œμ‚¬ν•­/λͺ©ν‘œ (D11, D22)
121
+ ws["D11"] = preview_difficulties
122
+ ws["D22"] = preview_goals
123
+
124
+ # wrap_text / μ»¬λŸΌλ„ˆλΉ„
125
  ws["D11"].alignment = Alignment(wrap_text=True)
126
  ws["D22"].alignment = Alignment(wrap_text=True)
127
+ ws.column_dimensions["D"].width = 42 # λŒ€λž΅ 10cm
 
 
128
 
129
  ############################
130
+ # 3) 사진 μ‚½μž… (B117, F117)
131
+ # (κ°€λ‘œ 8cm, μ„Έλ‘œ 5cm)
132
  ############################
133
  width_px = int(37.79 * 8) # 8cm
134
  height_px = int(37.79 * 5) # 5cm
 
152
  ws.add_image(img2_for_excel, "F117")
153
 
154
  ############################
155
+ # 4) μž„μ‹œ 파일 -> EXE.xlsx
156
  ############################
157
  output = BytesIO()
158
  wb.save(output)
 
203
  consultant_round,
204
  consultant_client_name,
205
  consultant_region,
206
+
207
  photo1,
208
  photo2,
209
+
210
  preview_difficulties,
211
  preview_goals
212
  )
 
217
  ######################
218
  def create_app():
219
  with gr.Blocks() as demo:
220
+ gr.Markdown("## λ³‘ν•©λœ μ…€ μžλ™ ν•΄μ œ -> μ•ˆμ „ν•˜κ²Œ μ“°κΈ°")
221
 
222
  with gr.Row():
223
  consultant_name_rep = gr.Textbox(label="μ»¨μ„€νŒ… 업체 및 λŒ€ν‘œ 이름")
 
239
 
240
  gr.Markdown("---")
241
 
 
242
  with gr.Row():
243
  photo_input_1 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
244
  photo_input_2 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
 
254
  analyze_button.click(
255
  fn=run_analyze,
256
  inputs=[difficulties_input, goals_input],
257
+ outputs=[preview_difficulties_output, preview_goals_output]
258
  )
259
 
260
  download_button.click(
 
268
  consultant_round,
269
  consultant_client_name,
270
  consultant_region,
271
+
272
  photo_input_1,
273
  photo_input_2,
274
+
275
  difficulties_input,
276
  goals_input,
277
  preview_difficulties_output,
278
+ preview_goals_output
279
  ],
280
+ outputs=[download_file]
281
  )
282
 
283
  return demo