Sangwon78 commited on
Commit
cf7d61c
Β·
verified Β·
1 Parent(s): 4775b04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -26
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import openai
3
  import gradio as gr
4
  from openpyxl import load_workbook
 
5
  from io import BytesIO
6
  import tempfile
7
 
@@ -24,14 +25,15 @@ def call_api(content, system_message, max_tokens, temperature, top_p):
24
  temperature=temperature,
25
  top_p=top_p,
26
  )
27
- return response.choices[0].message['content']
28
 
29
  ######################
30
  # 뢄석 ν•¨μˆ˜
31
  ######################
32
  def analyze_texts(difficulties, goals):
33
  """
34
- μ‚¬μš©μž μž…λ ₯(μ• λ‘œμ‚¬ν•­, μ»¨μ„€νŒ…λͺ©ν‘œ)을 gpt-4o-mini둜 뢄석 ν›„ κ²°κ³Ό(미리보기)λ₯Ό 리턴
 
35
  """
36
  # μ• λ‘œμ‚¬ν•­ 뢄석
37
  preview_difficulties = call_api(
@@ -39,7 +41,7 @@ def analyze_texts(difficulties, goals):
39
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ• λ‘œμ‚¬ν•­μ„ κ°„λ‹¨νžˆ 뢄석 및 ν•΄κ²°λ°©μ•ˆ μ œμ‹œν•΄μ€˜.",
40
  max_tokens=50,
41
  temperature=0.7,
42
- top_p=0.9
43
  )
44
 
45
  # μ»¨μ„€νŒ… λͺ©ν‘œ 뢄석
@@ -48,7 +50,7 @@ def analyze_texts(difficulties, goals):
48
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ»¨μ„€νŒ… λͺ©ν‘œμ— λŒ€ν•œ μš”μ•½ 및 μ‹€ν–‰λ°©μ•ˆμ„ μ œμ‹œν•΄μ€˜.",
49
  max_tokens=50,
50
  temperature=0.7,
51
- top_p=0.9
52
  )
53
 
54
  return preview_difficulties, preview_goals
@@ -56,32 +58,48 @@ def analyze_texts(difficulties, goals):
56
  ######################
57
  # μ—‘μ…€ 생성(λ‹€μš΄λ‘œλ“œ) ν•¨μˆ˜
58
  ######################
59
- def generate_excel(preview_difficulties, preview_goals):
60
  """
61
- 1) 'EXE.xlsx' ν…œν”Œλ¦Ώμ„ λΆˆλŸ¬μ™€μ„œ
62
- 2) D11 = μ• λ‘œμ‚¬ν•­ 뢄석 κ²°κ³Ό
63
- D22 = μ»¨μ„€νŒ… λͺ©ν‘œ 뢄석 κ²°κ³Ό
64
- 3) μ΅œμ’…μ μœΌλ‘œλ„ 'EXE.xlsx'λΌλŠ” μ΄λ¦„μœΌλ‘œ μž„μ‹œν΄λ”μ— μ €μž₯ ν›„ 경둜 λ°˜ν™˜
65
  """
66
- # β‘  μž…λ ₯ ν…œν”Œλ¦Ώ 파일λͺ…을 EXE.xlsx둜 λ³€κ²½
67
- wb = load_workbook("EXE.xlsx")
68
  ws = wb.active
69
 
70
- # D11, D22 셀에 κ²°κ³Ό κΈ°μž…
71
- ws["D11"] = preview_difficulties
72
- ws["D22"] = preview_goals
73
-
74
- # λ©”λͺ¨λ¦¬ 버퍼에 μ €μž₯
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  output = BytesIO()
76
  wb.save(output)
77
  output.seek(0)
78
 
79
- # μž„μ‹œ 파일 생성 ν›„, μ΅œμ’… 파일λͺ…도 'EXE.xlsx'둜 μ§€μ •
80
  with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
81
  tmp.write(output.getvalue())
82
  tmp.flush()
83
  temp_file_path = tmp.name
84
 
 
85
  dir_path = os.path.dirname(temp_file_path)
86
  final_path = os.path.join(dir_path, "EXE.xlsx")
87
  os.rename(temp_file_path, final_path)
@@ -99,14 +117,14 @@ def run_analyze(difficulties, goals):
99
  preview_difficulties, preview_goals = analyze_texts(difficulties, goals)
100
  return preview_difficulties, preview_goals
101
 
102
- def run_generate_excel(difficulties, goals, preview_difficulties, preview_goals):
103
  """
104
  'μ—‘μ…€λ‘œ λ‹€μš΄λ‘œλ“œ' λ²„νŠΌ λˆ„λ₯΄λ©΄ μ‹€ν–‰:
105
  - generate_excel()둜 EXE.xlsx νŒŒμΌμ„ λ§Œλ“€κ³ ,
106
  - Gradio File μ»΄ν¬λ„ŒνŠΈλ‘œ λ‹€μš΄λ‘œλ“œ
107
  """
108
- excel_path = generate_excel(preview_difficulties, preview_goals)
109
- # gr.File() μ»΄ν¬λ„ŒνŠΈμ— λ°˜ν™˜ -> μ‚¬μš©μžκ°€ λ‹€μš΄ κ°€λŠ₯
110
  return excel_path
111
 
112
  ######################
@@ -114,11 +132,18 @@ def run_generate_excel(difficulties, goals, preview_difficulties, preview_goals)
114
  ######################
115
  def create_app():
116
  with gr.Blocks() as demo:
117
- gr.Markdown("## μ• λ‘œμ‚¬ν•­ 및 μ»¨μ„€νŒ… λͺ©ν‘œ μž…λ ₯ -> gpt-4o-mini 뢄석 -> Excel λ‹€μš΄λ‘œλ“œ")
 
 
 
 
 
 
118
 
 
119
  with gr.Row():
120
- difficulties_input = gr.Textbox(label="μ• λ‘œμ‚¬ν•­ μž…λ ₯", lines=3, value="")
121
- goals_input = gr.Textbox(label="μ»¨μ„€νŒ… λͺ©ν‘œ μž…λ ₯", lines=3, value="")
122
 
123
  analyze_button = gr.Button("λΆ„μ„ν•˜κΈ°")
124
 
@@ -129,17 +154,24 @@ def create_app():
129
  download_button = gr.Button("μ—‘μ…€λ‘œ λ‹€μš΄λ‘œλ“œ")
130
  download_file = gr.File(label="λ‹€μš΄λ‘œλ“œ(클릭 ν›„ μ €μž₯)")
131
 
132
- # 뢄석 λ²„νŠΌ 클릭 -> 미리보기
133
  analyze_button.click(
134
  fn=run_analyze,
135
  inputs=[difficulties_input, goals_input],
136
  outputs=[preview_difficulties_output, preview_goals_output]
137
  )
138
 
139
- # λ‹€μš΄λ‘œλ“œ λ²„νŠΌ 클릭 -> 파일 생성
140
  download_button.click(
141
  fn=run_generate_excel,
142
- inputs=[difficulties_input, goals_input, preview_difficulties_output, preview_goals_output],
 
 
 
 
 
 
 
143
  outputs=[download_file]
144
  )
145
 
 
2
  import openai
3
  import gradio as gr
4
  from openpyxl import load_workbook
5
+ from openpyxl.drawing.image import Image as ExcelImage
6
  from io import BytesIO
7
  import tempfile
8
 
 
25
  temperature=temperature,
26
  top_p=top_p,
27
  )
28
+ return response.choices[0].message["content"]
29
 
30
  ######################
31
  # 뢄석 ν•¨μˆ˜
32
  ######################
33
  def analyze_texts(difficulties, goals):
34
  """
35
+ μ‚¬μš©μž μž…λ ₯(μ• λ‘œμ‚¬ν•­, μ»¨μ„€νŒ… λͺ©ν‘œ)을 gpt-4o-mini둜 뢄석 ν›„
36
+ κ²°κ³Ό(미리보기)λ₯Ό 리턴
37
  """
38
  # μ• λ‘œμ‚¬ν•­ 뢄석
39
  preview_difficulties = call_api(
 
41
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ• λ‘œμ‚¬ν•­μ„ κ°„λ‹¨νžˆ 뢄석 및 ν•΄κ²°λ°©μ•ˆ μ œμ‹œν•΄μ€˜.",
42
  max_tokens=50,
43
  temperature=0.7,
44
+ top_p=0.9,
45
  )
46
 
47
  # μ»¨μ„€νŒ… λͺ©ν‘œ 뢄석
 
50
  system_message="μ‚¬μš©μžκ°€ μž…λ ₯ν•œ μ»¨μ„€νŒ… λͺ©ν‘œμ— λŒ€ν•œ μš”μ•½ 및 μ‹€ν–‰λ°©μ•ˆμ„ μ œμ‹œν•΄μ€˜.",
51
  max_tokens=50,
52
  temperature=0.7,
53
+ top_p=0.9,
54
  )
55
 
56
  return preview_difficulties, preview_goals
 
58
  ######################
59
  # μ—‘μ…€ 생성(λ‹€μš΄λ‘œλ“œ) ν•¨μˆ˜
60
  ######################
61
+ def generate_excel(photo1, photo2, preview_difficulties, preview_goals):
62
  """
63
+ 1) 'EXE.xlsx' μ—‘μ…€ 파일(ν…œν”Œλ¦Ώ)을 λΆˆλŸ¬μ˜¨λ‹€.
64
+ 2) 이미지(photo1, photo2)λ₯Ό 각각 B117, F117 μœ„μΉ˜μ— μ‚½μž…ν•œλ‹€.
65
+ 3) ν•„μš”ν•œ 경우, preview_difficulties, preview_goals 등을 μ—‘μ…€ 셀에 적을 μˆ˜λ„ 있음.
66
+ 4) μ΅œμ’…μ μœΌλ‘œ 'EXE.xlsx'λΌλŠ” μ΄λ¦„μœΌλ‘œ μž„μ‹œν΄λ”μ— μ €μž₯ ν›„ 경둜 λ°˜ν™˜.
67
  """
68
+ wb = load_workbook("EXE.xlsx") # μž…λ ₯ ν…œν”Œλ¦Ώ
 
69
  ws = wb.active
70
 
71
+ # (μ˜ˆμ‹œ) 뢄석 κ²°κ³Όλ₯Ό λ‹€λ₯Έ 셀에 μ“°κ³  μ‹Άλ‹€λ©΄ μΆ”κ°€ μ˜ˆμ‹œ:
72
+ # ws["B10"] = preview_difficulties
73
+ # ws["F10"] = preview_goals
74
+
75
+ # β‘  첫 번째 사진을 B117 μ…€ μ˜μ—­μ— μΆ”κ°€
76
+ if photo1 is not None:
77
+ # Gradio의 Image νƒ€μž…μ€ PIL Image 객체둜 λ„˜μ–΄μ˜΄
78
+ img1_bytes = BytesIO()
79
+ photo1.save(img1_bytes, format="PNG")
80
+ img1_bytes.seek(0)
81
+ img1_for_excel = ExcelImage(img1_bytes)
82
+ ws.add_image(img1_for_excel, "B117")
83
+
84
+ # β‘‘ 두 번째 사진을 F117 μ…€ μ˜μ—­μ— μΆ”κ°€
85
+ if photo2 is not None:
86
+ img2_bytes = BytesIO()
87
+ photo2.save(img2_bytes, format="PNG")
88
+ img2_bytes.seek(0)
89
+ img2_for_excel = ExcelImage(img2_bytes)
90
+ ws.add_image(img2_for_excel, "F117")
91
+
92
+ # λ©”λͺ¨λ¦¬μ— μ €μž₯ ν›„ μž„μ‹œ 파일 생성
93
  output = BytesIO()
94
  wb.save(output)
95
  output.seek(0)
96
 
 
97
  with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
98
  tmp.write(output.getvalue())
99
  tmp.flush()
100
  temp_file_path = tmp.name
101
 
102
+ # μž„μ‹œν΄λ” λ‚΄ μ΅œμ’… 파일λͺ…을 EXE.xlsx둜
103
  dir_path = os.path.dirname(temp_file_path)
104
  final_path = os.path.join(dir_path, "EXE.xlsx")
105
  os.rename(temp_file_path, final_path)
 
117
  preview_difficulties, preview_goals = analyze_texts(difficulties, goals)
118
  return preview_difficulties, preview_goals
119
 
120
+ def run_generate_excel(photo1, photo2, difficulties, goals, preview_difficulties, preview_goals):
121
  """
122
  'μ—‘μ…€λ‘œ λ‹€μš΄λ‘œλ“œ' λ²„νŠΌ λˆ„λ₯΄λ©΄ μ‹€ν–‰:
123
  - generate_excel()둜 EXE.xlsx νŒŒμΌμ„ λ§Œλ“€κ³ ,
124
  - Gradio File μ»΄ν¬λ„ŒνŠΈλ‘œ λ‹€μš΄λ‘œλ“œ
125
  """
126
+ excel_path = generate_excel(photo1, photo2, preview_difficulties, preview_goals)
127
+ # gr.File() μ»΄ν¬λ„ŒνŠΈλ‘œ λ°˜ν™˜ -> μ‚¬μš©μžκ°€ λ‹€μš΄ κ°€λŠ₯
128
  return excel_path
129
 
130
  ######################
 
132
  ######################
133
  def create_app():
134
  with gr.Blocks() as demo:
135
+ gr.Markdown("## μ• λ‘œμ‚¬ν•­ 및 μ»¨μ„€νŒ… λͺ©ν‘œ μž…λ ₯ + 사진 μ—…λ‘œλ“œ -> gpt-4o-mini 뢄석 -> Excel λ‹€μš΄λ‘œλ“œ")
136
+
137
+ with gr.Row():
138
+ difficulties_input = gr.Textbox(label="μ• λ‘œμ‚¬ν•­ μž…λ ₯", lines=3)
139
+ goals_input = gr.Textbox(label="μ»¨μ„€νŒ… λͺ©ν‘œ μž…λ ₯", lines=3)
140
+
141
+ gr.Markdown("---")
142
 
143
+ # 사진 2개 μ—…λ‘œλ“œ (두 개 λͺ¨λ‘ 라벨은 'μˆ˜μ§„μ—…μ²΄μ‚¬μ§„')
144
  with gr.Row():
145
+ photo_input_1 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
146
+ photo_input_2 = gr.Image(label="μˆ˜μ§„μ—…μ²΄μ‚¬μ§„", type="pil")
147
 
148
  analyze_button = gr.Button("λΆ„μ„ν•˜κΈ°")
149
 
 
154
  download_button = gr.Button("μ—‘μ…€λ‘œ λ‹€μš΄λ‘œλ“œ")
155
  download_file = gr.File(label="λ‹€μš΄λ‘œλ“œ(클릭 ν›„ μ €μž₯)")
156
 
157
+ # [λΆ„μ„ν•˜κΈ°] λ²„νŠΌ 클릭 -> 미리보기
158
  analyze_button.click(
159
  fn=run_analyze,
160
  inputs=[difficulties_input, goals_input],
161
  outputs=[preview_difficulties_output, preview_goals_output]
162
  )
163
 
164
+ # [μ—‘μ…€λ‘œ λ‹€μš΄λ‘œλ“œ] λ²„νŠΌ 클릭 -> exe.xlsx 생성 & λ‹€μš΄λ‘œλ“œ
165
  download_button.click(
166
  fn=run_generate_excel,
167
+ inputs=[
168
+ photo_input_1,
169
+ photo_input_2,
170
+ difficulties_input,
171
+ goals_input,
172
+ preview_difficulties_output,
173
+ preview_goals_output,
174
+ ],
175
  outputs=[download_file]
176
  )
177