Sangwon78 commited on
Commit
63a1b71
ยท
verified ยท
1 Parent(s): 9bd633f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -57
app.py CHANGED
@@ -16,8 +16,11 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
16
  # [api์ž๋ฃŒ]์—์„œ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜ (์ˆ˜์ • ๊ธˆ์ง€)
17
  ######################
18
  def call_api(content, system_message, max_tokens, temperature, top_p):
 
 
 
19
  response = openai.ChatCompletion.create(
20
- model="gpt-4o-mini", # ์š”์ฒญ์‚ฌํ•ญ๋Œ€๋กœ gpt-4o-mini ์‚ฌ์šฉ
21
  messages=[
22
  {"role": "system", "content": system_message},
23
  {"role": "user", "content": content},
@@ -34,13 +37,16 @@ def call_api(content, system_message, max_tokens, temperature, top_p):
34
  def analyze_texts(difficulties, goals):
35
  """
36
  ์‚ฌ์šฉ์ž ์ž…๋ ฅ(์• ๋กœ์‚ฌํ•ญ, ์ปจ์„คํŒ… ๋ชฉํ‘œ)์„ gpt-4o-mini๋กœ ๋ถ„์„ ํ›„
37
- ๊ฒฐ๊ณผ(๋ฏธ๋ฆฌ๋ณด๊ธฐ)๋ฅผ ๋ฆฌํ„ด
 
38
  """
39
  # ์• ๋กœ์‚ฌํ•ญ ๋ถ„์„
40
  preview_difficulties = call_api(
41
  content=difficulties,
42
- system_message="์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์• ๋กœ์‚ฌํ•ญ์„ ๊ฐ„๋‹จํžˆ ๋ถ„์„ ๋ฐ ํ•ด๊ฒฐ๋ฐฉ์•ˆ ์ œ์‹œํ•ด์ค˜.",
43
- max_tokens=50,
 
 
44
  temperature=0.7,
45
  top_p=0.9,
46
  )
@@ -48,8 +54,10 @@ def analyze_texts(difficulties, goals):
48
  # ์ปจ์„คํŒ… ๋ชฉํ‘œ ๋ถ„์„
49
  preview_goals = call_api(
50
  content=goals,
51
- system_message="์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ปจ์„คํŒ… ๋ชฉํ‘œ์— ๋Œ€ํ•œ ์š”์•ฝ ๋ฐ ์‹คํ–‰๋ฐฉ์•ˆ์„ ์ œ์‹œํ•ด์ค˜.",
52
- max_tokens=50,
 
 
53
  temperature=0.7,
54
  top_p=0.9,
55
  )
@@ -61,63 +69,57 @@ def analyze_texts(difficulties, goals):
61
  ######################
62
  def generate_excel(photo1, photo2, preview_difficulties, preview_goals):
63
  """
64
- 1) 'EXE.xlsx' ์—‘์…€ ํŒŒ์ผ(ํ…œํ”Œ๋ฆฟ)์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
65
- 2) D11์— ์• ๋กœ์‚ฌํ•ญ(preview_difficulties), D22์— ์ปจ์„คํŒ…๋ชฉํ‘œ(preview_goals) ๊ธฐ์ž…
66
- - ํ•œ ์ค„์ด 15cm๋ฅผ ๋„˜์œผ๋ฉด ์ž๋™ ์ค„๋ฐ”๊ฟˆ.
67
- 3) B117, F117์— ๊ฐ๊ฐ ์‚ฌ์ง„ ์‚ฝ์ž… (๊ฐ€๋กœ 8cm, ์„ธ๋กœ 5cm).
68
- 4) ์ตœ์ข…์ ์œผ๋กœ 'EXE.xlsx'๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ž„์‹œํด๋”์— ์ €์žฅ ํ›„ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜.
 
69
  """
70
- wb = load_workbook("EXE.xlsx") # ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ
71
  ws = wb.active
72
 
73
- # ------------------------------
74
- # 1) ํ…์ŠคํŠธ ๊ธฐ์ž…: D11, D22
75
- # ------------------------------
76
  ws["D11"] = preview_difficulties
77
- ws["D22"] = preview_goals
78
-
79
- # ์ž๋™ ์ค„๋ฐ”๊ฟˆ์„ ์œ„ํ•ด wrap_text ์ ์šฉ
80
  ws["D11"].alignment = Alignment(wrap_text=True)
 
 
 
 
81
  ws["D22"].alignment = Alignment(wrap_text=True)
82
 
83
- # ์ปฌ๋Ÿผ D์˜ ํญ(ColumnWidth)์„ ์•ฝ 62 ์ •๋„๋กœ ์กฐ์ •
84
- # (์‹ค์ œ Excel ํฐํŠธ๋‚˜ ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋‚˜
85
- # ๋Œ€๋žต 62 ํญ์ด 15cm ์ •๋„์— ๊ทผ์ ‘)
86
- ws.column_dimensions["D"].width = 62
87
 
88
- # ------------------------------
89
- # 2) ์‚ฌ์ง„ ์‚ฝ์ž…: B117, F117
90
- # (๊ฐ€๋กœ 8cm, ์„ธ๋กœ 5cm)
91
- # ------------------------------
92
- # cm -> px ๋ณ€ํ™˜ (๋Œ€๋žต 1cm โ‰ˆ 37.79px)
93
  width_px = int(37.79 * 8) # 8cm
94
  height_px = int(37.79 * 5) # 5cm
95
 
96
- # ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ -> B117
97
  if photo1 is not None:
98
  img1_bytes = BytesIO()
99
  photo1.save(img1_bytes, format="PNG")
100
  img1_bytes.seek(0)
 
 
 
 
101
 
102
- img1_for_excel = ExcelImage(img1_bytes)
103
- img1_for_excel.width = width_px
104
- img1_for_excel.height = height_px
105
- ws.add_image(img1_for_excel, "B117")
106
-
107
- # ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ -> F117
108
  if photo2 is not None:
109
  img2_bytes = BytesIO()
110
  photo2.save(img2_bytes, format="PNG")
111
  img2_bytes.seek(0)
 
 
 
 
112
 
113
- img2_for_excel = ExcelImage(img2_bytes)
114
- img2_for_excel.width = width_px
115
- img2_for_excel.height = height_px
116
- ws.add_image(img2_for_excel, "F117")
117
-
118
- # ------------------------------
119
- # 3) ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ ํ›„ ์ž„์‹œํŒŒ์ผ ์ƒ์„ฑ
120
- # ------------------------------
121
  output = BytesIO()
122
  wb.save(output)
123
  output.seek(0)
@@ -127,9 +129,7 @@ def generate_excel(photo1, photo2, preview_difficulties, preview_goals):
127
  tmp.flush()
128
  temp_file_path = tmp.name
129
 
130
- # ์ž„์‹œํด๋” ๋‚ด ์ตœ์ข… ํŒŒ์ผ๋ช…์„ EXE.xlsx๋กœ rename
131
- dir_path = os.path.dirname(temp_file_path)
132
- final_path = os.path.join(dir_path, "EXE.xlsx")
133
  os.rename(temp_file_path, final_path)
134
 
135
  return final_path
@@ -140,7 +140,7 @@ def generate_excel(photo1, photo2, preview_difficulties, preview_goals):
140
  def run_analyze(difficulties, goals):
141
  """
142
  '๋ถ„์„ํ•˜๊ธฐ' ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰:
143
- - gpt-4o-mini API๋กœ ๋ถ„์„ ํ›„ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜
144
  """
145
  preview_difficulties, preview_goals = analyze_texts(difficulties, goals)
146
  return preview_difficulties, preview_goals
@@ -148,8 +148,7 @@ def run_analyze(difficulties, goals):
148
  def run_generate_excel(photo1, photo2, difficulties, goals, preview_difficulties, preview_goals):
149
  """
150
  '์—‘์…€๋กœ ๋‹ค์šด๋กœ๋“œ' ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰:
151
- - generate_excel()๋กœ EXE.xlsx ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ ,
152
- - Gradio File ์ปดํฌ๋„ŒํŠธ๋กœ ๋‹ค์šด๋กœ๋“œ
153
  """
154
  excel_path = generate_excel(photo1, photo2, preview_difficulties, preview_goals)
155
  return excel_path
@@ -159,7 +158,7 @@ def run_generate_excel(photo1, photo2, difficulties, goals, preview_difficulties
159
  ######################
160
  def create_app():
161
  with gr.Blocks() as demo:
162
- gr.Markdown("## ์• ๋กœ์‚ฌํ•ญ ๋ฐ ์ปจ์„คํŒ… ๋ชฉํ‘œ ์ž…๋ ฅ + ์‚ฌ์ง„ ์—…๋กœ๋“œ -> gpt-4o-mini ๋ถ„์„ -> Excel ๋‹ค์šด๋กœ๋“œ")
163
 
164
  with gr.Row():
165
  difficulties_input = gr.Textbox(label="์• ๋กœ์‚ฌํ•ญ ์ž…๋ ฅ", lines=3)
@@ -167,28 +166,25 @@ def create_app():
167
 
168
  gr.Markdown("---")
169
 
170
- # ์‚ฌ์ง„ 2๊ฐœ ์—…๋กœ๋“œ (๋‘˜ ๋‹ค '์ˆ˜์ง„์—…์ฒด์‚ฌ์ง„')
171
  with gr.Row():
172
  photo_input_1 = gr.Image(label="์ˆ˜์ง„์—…์ฒด์‚ฌ์ง„", type="pil")
173
  photo_input_2 = gr.Image(label="์ˆ˜์ง„์—…์ฒด์‚ฌ์ง„", type="pil")
174
 
175
- analyze_button = gr.Button("๋ถ„์„ํ•˜๊ธฐ")
176
-
177
- # ๋ฏธ๋ฆฌ๋ณด๊ธฐ
178
- preview_difficulties_output = gr.Textbox(label="์• ๋กœ์‚ฌํ•ญ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=3)
179
- preview_goals_output = gr.Textbox(label="์ปจ์„คํŒ… ๋ชฉํ‘œ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=3)
180
 
181
  download_button = gr.Button("์—‘์…€๋กœ ๋‹ค์šด๋กœ๋“œ")
182
  download_file = gr.File(label="๋‹ค์šด๋กœ๋“œ(ํด๋ฆญ ํ›„ ์ €์žฅ)")
183
 
184
- # [๋ถ„์„ํ•˜๊ธฐ] ๋ฒ„ํŠผ ํด๋ฆญ -> ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํ‘œ์‹œ
185
  analyze_button.click(
186
  fn=run_analyze,
187
  inputs=[difficulties_input, goals_input],
188
  outputs=[preview_difficulties_output, preview_goals_output]
189
  )
190
 
191
- # [์—‘์…€๋กœ ๋‹ค์šด๋กœ๋“œ] ๋ฒ„ํŠผ ํด๋ฆญ -> exe.xlsx ์ƒ์„ฑ & ๋‹ค์šด๋กœ๋“œ
192
  download_button.click(
193
  fn=run_generate_excel,
194
  inputs=[
 
16
  # [api์ž๋ฃŒ]์—์„œ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜ (์ˆ˜์ • ๊ธˆ์ง€)
17
  ######################
18
  def call_api(content, system_message, max_tokens, temperature, top_p):
19
+ """
20
+ gpt-4o-mini ๋ชจ๋ธ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด์–ด ์‘๋‹ต์„ ๋ฐ›๋Š” ํ•จ์ˆ˜.
21
+ """
22
  response = openai.ChatCompletion.create(
23
+ model="gpt-4o-mini", # ์š”์ฒญ์‚ฌํ•ญ: gpt-4o-mini ์‚ฌ์šฉ
24
  messages=[
25
  {"role": "system", "content": system_message},
26
  {"role": "user", "content": content},
 
37
  def analyze_texts(difficulties, goals):
38
  """
39
  ์‚ฌ์šฉ์ž ์ž…๋ ฅ(์• ๋กœ์‚ฌํ•ญ, ์ปจ์„คํŒ… ๋ชฉํ‘œ)์„ gpt-4o-mini๋กœ ๋ถ„์„ ํ›„
40
+ ๊ฒฐ๊ณผ(๋ฏธ๋ฆฌ๋ณด๊ธฐ)๋ฅผ ๋ฆฌํ„ด.
41
+ - ๋‹ต๋ณ€์€ ์ตœ์†Œ 10์ค„ ์ด์ƒ์˜ ํ…์ŠคํŠธ๋กœ ์ž‘์„ฑ๋˜๋„๋ก system_message์—์„œ ์š”๊ตฌํ•จ.
42
  """
43
  # ์• ๋กœ์‚ฌํ•ญ ๋ถ„์„
44
  preview_difficulties = call_api(
45
  content=difficulties,
46
+ system_message=(
47
+ "์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์• ๋กœ์‚ฌํ•ญ์„ ์ตœ์†Œ 10์ค„ ์ด์ƒ์˜ ํ…์ŠคํŠธ๋กœ ๊ธธ๊ฒŒ ๋ถ„์„ํ•ด์ค˜."
48
+ ),
49
+ max_tokens=400, # ์ถฉ๋ถ„ํ•œ ๊ธธ์ด๋ฅผ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด max_tokens๋ฅผ ๋„‰๋„‰ํžˆ ์„ค์ •
50
  temperature=0.7,
51
  top_p=0.9,
52
  )
 
54
  # ์ปจ์„คํŒ… ๋ชฉํ‘œ ๋ถ„์„
55
  preview_goals = call_api(
56
  content=goals,
57
+ system_message=(
58
+ "์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ปจ์„คํŒ… ๋ชฉํ‘œ๋ฅผ ์ตœ์†Œ 10์ค„ ์ด์ƒ์˜ ํ…์ŠคํŠธ๋กœ ์ž์„ธํžˆ ์ž‘์„ฑํ•ด์ค˜."
59
+ ),
60
+ max_tokens=400,
61
  temperature=0.7,
62
  top_p=0.9,
63
  )
 
69
  ######################
70
  def generate_excel(photo1, photo2, preview_difficulties, preview_goals):
71
  """
72
+ 1) 'EXE.xlsx' ์—‘์…€ ํ…œํ”Œ๋ฆฟ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
73
+ 2) ์• ๋กœ์‚ฌํ•ญ -> D11:H11์— ๊ธฐ์ž… (merge ์˜์—ญ ๊ฐ€์ •),
74
+ ์ปจ์„คํŒ…๋ชฉํ‘œ -> D22:H22์— ๊ธฐ์ž… (merge ์˜์—ญ ๊ฐ€์ •).
75
+ * wrap_text ์ ์šฉ, ๊ธ€ ํ•œ ์ค„ ํญ์€ ๋Œ€๋žต 15cm ์ด์ƒ ํ™•๋ณด.
76
+ 3) B117, F117์— ์‚ฌ์ง„ ์‚ฝ์ž… (๊ฐ€๋กœ 8cm, ์„ธ๋กœ 5cm).
77
+ 4) ๊ฒฐ๊ณผ๋ฅผ EXE.xlsx๋กœ ์ €์žฅ ํ›„ ์ž„์‹œ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜.
78
  """
79
+ wb = load_workbook("EXE.xlsx")
80
  ws = wb.active
81
 
82
+ # (1) D11:H11 -> ์• ๋กœ์‚ฌํ•ญ
83
+ # - merge ์…€(๊ฐ€์ •)์ด๋ผ๋ฉด top-left์ธ D11์—๋งŒ ์จ๋„ ์ „์ฒด ๋ฒ”์œ„์— ๋ณด์ž„
84
+ ws.merge_cells("D11:H11") # ํ˜น์‹œ ํ…œํ”Œ๋ฆฟ์—์„œ ๋ฏธ๋ฆฌ ๋ณ‘ํ•ฉ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ
85
  ws["D11"] = preview_difficulties
86
+ # wrap_text
 
 
87
  ws["D11"].alignment = Alignment(wrap_text=True)
88
+
89
+ # (2) D22:H22 -> ์ปจ์„คํŒ…๋ชฉํ‘œ
90
+ ws.merge_cells("D22:H22")
91
+ ws["D22"] = preview_goals
92
  ws["D22"].alignment = Alignment(wrap_text=True)
93
 
94
+ # (3) ์ปฌ๋Ÿผ ํญ ์„ค์ • (D~H๊นŒ์ง€ ํญ ๋„“ํž˜ -> 15cm ์ด์ƒ)
95
+ # ์‹ค์ œ cm๋กœ ์ •ํ™•ํžˆ ๋งž์ถ”๊ธฐ ์–ด๋ ค์šฐ๋ฏ€๋กœ ์ ๋‹นํžˆ ํฌ๊ฒŒ ์ง€์ •
96
+ for col in ["D", "E", "F", "G", "H"]:
97
+ ws.column_dimensions[col].width = 30
98
 
99
+ # (4) ์‚ฌ์ง„ ์‚ฝ์ž…
100
+ # B117, F117 ์œ„์น˜ (8cm x 5cm)
 
 
 
101
  width_px = int(37.79 * 8) # 8cm
102
  height_px = int(37.79 * 5) # 5cm
103
 
 
104
  if photo1 is not None:
105
  img1_bytes = BytesIO()
106
  photo1.save(img1_bytes, format="PNG")
107
  img1_bytes.seek(0)
108
+ img_for_excel_1 = ExcelImage(img1_bytes)
109
+ img_for_excel_1.width = width_px
110
+ img_for_excel_1.height = height_px
111
+ ws.add_image(img_for_excel_1, "B117")
112
 
 
 
 
 
 
 
113
  if photo2 is not None:
114
  img2_bytes = BytesIO()
115
  photo2.save(img2_bytes, format="PNG")
116
  img2_bytes.seek(0)
117
+ img_for_excel_2 = ExcelImage(img2_bytes)
118
+ img_for_excel_2.width = width_px
119
+ img_for_excel_2.height = height_px
120
+ ws.add_image(img_for_excel_2, "F117")
121
 
122
+ # (5) ์—‘์…€์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ -> ์ž„์‹œํŒŒ์ผ -> ์ตœ์ข… 'EXE.xlsx'
 
 
 
 
 
 
 
123
  output = BytesIO()
124
  wb.save(output)
125
  output.seek(0)
 
129
  tmp.flush()
130
  temp_file_path = tmp.name
131
 
132
+ final_path = os.path.join(os.path.dirname(temp_file_path), "EXE.xlsx")
 
 
133
  os.rename(temp_file_path, final_path)
134
 
135
  return final_path
 
140
  def run_analyze(difficulties, goals):
141
  """
142
  '๋ถ„์„ํ•˜๊ธฐ' ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰:
143
+ - gpt-4o-mini API๋กœ (์• ๋กœ์‚ฌํ•ญ/๋ชฉํ‘œ) ์ตœ์†Œ 10์ค„ ์ด์ƒ์˜ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
144
  """
145
  preview_difficulties, preview_goals = analyze_texts(difficulties, goals)
146
  return preview_difficulties, preview_goals
 
148
  def run_generate_excel(photo1, photo2, difficulties, goals, preview_difficulties, preview_goals):
149
  """
150
  '์—‘์…€๋กœ ๋‹ค์šด๋กœ๋“œ' ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰:
151
+ - generate_excel()๋กœ EXE.xlsx ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ๋ฐ˜ํ™˜
 
152
  """
153
  excel_path = generate_excel(photo1, photo2, preview_difficulties, preview_goals)
154
  return excel_path
 
158
  ######################
159
  def create_app():
160
  with gr.Blocks() as demo:
161
+ gr.Markdown("## (1) ์• ๋กœ์‚ฌํ•ญ ๋ฐ ์ปจ์„คํŒ… ๋ชฉํ‘œ ์ž…๋ ฅ + (2) ์‚ฌ์ง„ ์—…๋กœ๋“œ + (3) ๋ถ„์„ -> Excel ๋‹ค์šด๋กœ๋“œ")
162
 
163
  with gr.Row():
164
  difficulties_input = gr.Textbox(label="์• ๋กœ์‚ฌํ•ญ ์ž…๋ ฅ", lines=3)
 
166
 
167
  gr.Markdown("---")
168
 
 
169
  with gr.Row():
170
  photo_input_1 = gr.Image(label="์ˆ˜์ง„์—…์ฒด์‚ฌ์ง„", type="pil")
171
  photo_input_2 = gr.Image(label="์ˆ˜์ง„์—…์ฒด์‚ฌ์ง„", type="pil")
172
 
173
+ analyze_button = gr.Button("๋ถ„์„ํ•˜๊ธฐ (10์ค„ ์ด์ƒ)")
174
+ preview_difficulties_output = gr.Textbox(label="์• ๋กœ์‚ฌํ•ญ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=5)
175
+ preview_goals_output = gr.Textbox(label="์ปจ์„คํŒ… ๋ชฉํ‘œ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", lines=5)
 
 
176
 
177
  download_button = gr.Button("์—‘์…€๋กœ ๋‹ค์šด๋กœ๋“œ")
178
  download_file = gr.File(label="๋‹ค์šด๋กœ๋“œ(ํด๋ฆญ ํ›„ ์ €์žฅ)")
179
 
180
+ # ๋ถ„์„ํ•˜๊ธฐ
181
  analyze_button.click(
182
  fn=run_analyze,
183
  inputs=[difficulties_input, goals_input],
184
  outputs=[preview_difficulties_output, preview_goals_output]
185
  )
186
 
187
+ # ์—‘์…€ ๋‹ค์šด๋กœ๋“œ
188
  download_button.click(
189
  fn=run_generate_excel,
190
  inputs=[