Update app.py
Browse files
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", # ์์ฒญ์ฌํญ
|
| 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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 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)
|
| 66 |
-
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
"""
|
| 70 |
-
wb = load_workbook("EXE.xlsx")
|
| 71 |
ws = wb.active
|
| 72 |
|
| 73 |
-
# -
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
ws["D11"] = preview_difficulties
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# ์๋ ์ค๋ฐ๊ฟ์ ์ํด wrap_text ์ ์ฉ
|
| 80 |
ws["D11"].alignment = Alignment(wrap_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
ws["D22"].alignment = Alignment(wrap_text=True)
|
| 82 |
|
| 83 |
-
# ์ปฌ๋ผ D
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
#
|
| 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 |
-
|
| 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 |
-
|
| 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("## ์ ๋ก์ฌํญ ๋ฐ ์ปจ์คํ
๋ชฉํ ์
๋ ฅ + ์ฌ์ง ์
๋ก๋
|
| 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 |
-
#
|
| 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=[
|