Update app.py
Browse files
app.py
CHANGED
|
@@ -1,202 +1,84 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import tempfile
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
-
# ๋ง์ฝ OPENAI API๋ฅผ ์ง์ ์ฌ์ฉํด์ผ ํ ๊ฒฝ์ฐ:
|
| 6 |
-
# import openai
|
| 7 |
-
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
def process_upload(file):
|
| 14 |
-
"""
|
| 15 |
-
1) ์
๋ก๋๋ ์์
ํ์ผ์์ A~F์ด(๋ฆฌ๋ทฐ๋ด์ฉ ํฌํจ)์ ์ฝ์ด์ด
|
| 16 |
-
2) ์ถ๋ ฅํ๋ฉด(2๊ฐ)์๋ ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ค
|
| 17 |
-
3) ์์ง ๋ค์ด๋ก๋ ํ์ผ์ ์์ฑํ์ง ์๊ณ , state(์๋ณธ df)๋ง ๋ฐํ
|
| 18 |
-
"""
|
| 19 |
-
if file is None:
|
| 20 |
-
# ์ถ๋ ฅํ๋ฉด 2๊ฐ + ๋ ์ง์ ๋ ฌ ๋ค์ด๋ก๋ 2๊ฐ + ์ ์์ ๋ ฌ ๋ค์ด๋ก๋ 2๊ฐ + ์๋ณธ ๋ค์ด๋ก๋ 2๊ฐ + state = ์ด 9๊ฐ
|
| 21 |
-
return None, None, None, None, None, None, None, None, None
|
| 22 |
-
|
| 23 |
-
# A~F ์ด๋ง ์ ํ
|
| 24 |
df = pd.read_excel(file.name)
|
| 25 |
-
df = df.iloc[:, :6]
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
๋ฆฌ๋ทฐ๋ ์ง
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
- ์ถ๋ ฅํ๋ฉด1: ๋์ ์ ์(๋ด๋ฆผ์ฐจ์)
|
| 63 |
-
- ์ถ๋ ฅํ๋ฉด2: ๋ฎ์ ์ ์(์ค๋ฆ์ฐจ์)
|
| 64 |
-
- ์ ์์ ๋ ฌ ๋ค์ด๋ก๋ ํ์ผ 2๊ฐ ์์ฑ
|
| 65 |
-
- ๋ค๋ฅธ ๋ค์ด๋ก๋(๋ ์ง์ ๋ ฌ, ์๋ณธ)๋ None ์ฒ๋ฆฌ
|
| 66 |
-
"""
|
| 67 |
-
if df is None:
|
| 68 |
-
return None, None, None, None, None, None, None, None, None
|
| 69 |
-
|
| 70 |
-
df_high = df.sort_values(by='๋ฆฌ๋ทฐ์ ์', ascending=False)
|
| 71 |
-
df_low = df.sort_values(by='๋ฆฌ๋ทฐ์ ์', ascending=True)
|
| 72 |
-
|
| 73 |
-
# ์ ์์ ๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ฐ ์์
ํ์ผ๋ก ์ ์ฅ
|
| 74 |
-
temp_file1 = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 75 |
-
temp_file2 = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 76 |
-
with pd.ExcelWriter(temp_file1.name, engine='openpyxl') as writer:
|
| 77 |
-
df_high.to_excel(writer, index=False)
|
| 78 |
-
with pd.ExcelWriter(temp_file2.name, engine='openpyxl') as writer:
|
| 79 |
-
df_low.to_excel(writer, index=False)
|
| 80 |
-
|
| 81 |
-
# ์ถ๋ ฅ ์์:
|
| 82 |
-
# (์ถ๋ ฅํ๋ฉด1, ์ถ๋ ฅํ๋ฉด2, ๋ ์ง๋ค์ด1, ๋ ์ง๋ค์ด2, ์ ์๋ค์ด1, ์ ์๋ค์ด2, ์๋ณธ๋ค์ด1, ์๋ณธ๋ค์ด2, state)
|
| 83 |
-
return df_high, df_low, None, None, temp_file1.name, temp_file2.name, None, None, df
|
| 84 |
-
|
| 85 |
-
def reset_data(df):
|
| 86 |
-
"""
|
| 87 |
-
์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ๋ ํ๋ฉด์ ํ์.
|
| 88 |
-
- ์๋ณธ ๋ค์ด๋ก๋ ํ์ผ 2๊ฐ ์์ฑ
|
| 89 |
-
- ๋ ์ง/์ ์์ ๋ ฌ ๋ค์ด๋ก๋๋ None ์ฒ๋ฆฌ
|
| 90 |
-
"""
|
| 91 |
-
if df is None:
|
| 92 |
-
return None, None, None, None, None, None, None, None, None
|
| 93 |
-
|
| 94 |
-
temp_file1 = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 95 |
-
temp_file2 = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 96 |
-
with pd.ExcelWriter(temp_file1.name, engine='openpyxl') as writer:
|
| 97 |
-
df.to_excel(writer, index=False)
|
| 98 |
-
with pd.ExcelWriter(temp_file2.name, engine='openpyxl') as writer:
|
| 99 |
-
df.to_excel(writer, index=False)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
###################################
|
| 106 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 107 |
-
###################################
|
| 108 |
-
|
| 109 |
with gr.Blocks(title="์์
๋ถ์๊ธฐ") as demo:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
์์
ํ์ผ(A~F์ด, D์ด์ ๋ฆฌ๋ทฐ๋ด์ฉ ํฌํจ)์ ์
๋ก๋ํ๊ณ ,
|
| 113 |
-
**๋ฆฌ๋ทฐ๋ ์ง**, **๋ฆฌ๋ทฐ์ ์**, **์๋ณธ๋ณด๊ธฐ** ๋ฒํผ์ผ๋ก ์ ๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
|
| 114 |
-
๊ฐ ์ ๋ ฌ ๊ฒฐ๊ณผ๋ณ๋ก **์์
ํ์ผ ๋ค์ด๋ก๋**๊ฐ ๋ถ๋ฆฌ๋์ด ์์ด, ์ถฉ๋ ์์ด ๋ค์ด๋ก๋๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
|
| 115 |
-
""")
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
file_input = gr.File(label="์์
ํ์ผ ์
๋ก๋", file_types=['.xlsx', '.xls'])
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
state = gr.State()
|
| 122 |
-
|
| 123 |
-
# ์ถ๋ ฅํ๋ฉด 2๊ฐ
|
| 124 |
with gr.Row():
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
#
|
| 129 |
-
with gr.Row():
|
| 130 |
-
date_file_output1 = gr.File(label="(๋ ์ง์ ๋ ฌ) ๋ค์ด๋ก๋ ํ์ผ1")
|
| 131 |
-
date_file_output2 = gr.File(label="(๋ ์ง์ ๋ ฌ) ๋ค์ด๋ก๋ ํ์ผ2")
|
| 132 |
-
|
| 133 |
-
# ์ ์์ ๋ ฌ ๋ค์ด๋ก๋ 2๊ฐ
|
| 134 |
with gr.Row():
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
#
|
| 139 |
with gr.Row():
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
with gr.Row():
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
fn=process_upload,
|
| 152 |
-
inputs=file_input,
|
| 153 |
-
outputs=[
|
| 154 |
-
output1, output2,
|
| 155 |
-
date_file_output1, date_file_output2,
|
| 156 |
-
score_file_output1, score_file_output2,
|
| 157 |
-
reset_file_output1, reset_file_output2,
|
| 158 |
-
state
|
| 159 |
-
]
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
# ๋ฆฌ๋ทฐ๋ ์ง ๋ฒํผ -> sort_by_date
|
| 163 |
-
btn_date.click(
|
| 164 |
-
fn=sort_by_date,
|
| 165 |
-
inputs=state,
|
| 166 |
-
outputs=[
|
| 167 |
-
output1, output2,
|
| 168 |
-
date_file_output1, date_file_output2,
|
| 169 |
-
score_file_output1, score_file_output2,
|
| 170 |
-
reset_file_output1, reset_file_output2,
|
| 171 |
-
state
|
| 172 |
-
]
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
# ๋ฆฌ๋ทฐ์ ์ ๋ฒํผ -> sort_by_score
|
| 176 |
-
btn_score.click(
|
| 177 |
-
fn=sort_by_score,
|
| 178 |
-
inputs=state,
|
| 179 |
-
outputs=[
|
| 180 |
-
output1, output2,
|
| 181 |
-
date_file_output1, date_file_output2,
|
| 182 |
-
score_file_output1, score_file_output2,
|
| 183 |
-
reset_file_output1, reset_file_output2,
|
| 184 |
-
state
|
| 185 |
-
]
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
# ์๋ณธ๋ณด๊ธฐ ๋ฒํผ -> reset_data
|
| 189 |
-
btn_reset.click(
|
| 190 |
-
fn=reset_data,
|
| 191 |
-
inputs=state,
|
| 192 |
-
outputs=[
|
| 193 |
-
output1, output2,
|
| 194 |
-
date_file_output1, date_file_output2,
|
| 195 |
-
score_file_output1, score_file_output2,
|
| 196 |
-
reset_file_output1, reset_file_output2,
|
| 197 |
-
state
|
| 198 |
-
]
|
| 199 |
-
)
|
| 200 |
|
| 201 |
-
|
| 202 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ์์
ํ์ผ์ ์ฝ์ด 4๊ฐ์ง ์ ๋ ฌ ๊ฒฐ๊ณผ์ ํด๋น CSV ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐํํ๋ ํจ์
|
| 7 |
+
def process_file(file):
|
| 8 |
+
# ์์
ํ์ผ ์ฝ๊ธฐ (์ฒซ 6๊ฐ ์ด๋ง ์ ํ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
df = pd.read_excel(file.name)
|
| 10 |
+
df = df.iloc[:, :6] # A~F ์ด๋ง ์ฌ์ฉ
|
| 11 |
+
|
| 12 |
+
# ๋ฆฌ๋ทฐ๋ ์ง ๊ธฐ์ค ์ ๋ ฌ (๋ ์งํ์ผ๋ก ๋ณํ, ๋ณํ ์คํจ์ NaT)
|
| 13 |
+
try:
|
| 14 |
+
df['๋ฆฌ๋ทฐ๋ ์ง'] = pd.to_datetime(df['๋ฆฌ๋ทฐ๋ ์ง'], errors='coerce')
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print("๋ฆฌ๋ทฐ๋ ์ง ๋ณํ ์๋ฌ:", e)
|
| 17 |
+
date_recent = df.sort_values(by='๋ฆฌ๋ทฐ๋ ์ง', ascending=False)
|
| 18 |
+
date_old = df.sort_values(by='๋ฆฌ๋ทฐ๋ ์ง', ascending=True)
|
| 19 |
+
|
| 20 |
+
# ๋ฆฌ๋ทฐ์ ์ ๊ธฐ์ค ์ ๋ ฌ (์ซ์ํ์ผ๋ก ๋ณํ)
|
| 21 |
+
try:
|
| 22 |
+
df['๋ฆฌ๋ทฐ์ ์'] = pd.to_numeric(df['๋ฆฌ๋ทฐ์ ์'], errors='coerce')
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("๋ฆฌ๋ทฐ์ ์ ๋ณํ ์๋ฌ:", e)
|
| 25 |
+
score_high = df.sort_values(by='๋ฆฌ๋ทฐ์ ์', ascending=False)
|
| 26 |
+
score_low = df.sort_values(by='๋ฆฌ๋ทฐ์ ์', ascending=True)
|
| 27 |
+
|
| 28 |
+
# CSV ํ์ผ๋ก ์ ์ฅ (ํ์ฌ ์์
๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋จ)
|
| 29 |
+
date_recent_file = "date_recent.csv"
|
| 30 |
+
date_old_file = "date_old.csv"
|
| 31 |
+
score_high_file = "score_high.csv"
|
| 32 |
+
score_low_file = "score_low.csv"
|
| 33 |
+
date_recent.to_csv(date_recent_file, index=False)
|
| 34 |
+
date_old.to_csv(date_old_file, index=False)
|
| 35 |
+
score_high.to_csv(score_high_file, index=False)
|
| 36 |
+
score_low.to_csv(score_low_file, index=False)
|
| 37 |
+
|
| 38 |
+
return date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
|
| 39 |
+
|
| 40 |
+
# ๋ฆฌ๋ทฐ๋ ์ง ๋ฒํผ ํด๋ฆญ ์ ์คํ๋๋ ํจ์
|
| 41 |
+
def on_date(file):
|
| 42 |
+
if file is None:
|
| 43 |
+
return [None] * 6
|
| 44 |
+
date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
|
| 45 |
+
# ์ฒซ๋ฒ์งธ ํ๋ฉด: ๋ฆฌ๋ทฐ๋ ์ง ๋ด๋ฆผ์ฐจ์, ๋๋ฒ์งธ ํ๋ฉด: ๋ฆฌ๋ทฐ๋ ์ง ์ค๋ฆ์ฐจ์
|
| 46 |
+
return date_recent, date_old, date_recent_file, date_old_file, score_high_file, score_low_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# ๋ฆฌ๋ทฐ์ ์ ๋ฒํผ ํด๋ฆญ ์ ์คํ๋๋ ํจ์
|
| 49 |
+
def on_score(file):
|
| 50 |
+
if file is None:
|
| 51 |
+
return [None] * 6
|
| 52 |
+
date_recent, date_old, score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file = process_file(file)
|
| 53 |
+
# ์ฒซ๋ฒ์งธ ํ๋ฉด: ๋ฆฌ๋ทฐ์ ์ ๋ด๋ฆผ์ฐจ์, ๋๋ฒ์งธ ํ๋ฉด: ๋ฆฌ๋ทฐ์ ์ ์ค๋ฆ์ฐจ์
|
| 54 |
+
return score_high, score_low, date_recent_file, date_old_file, score_high_file, score_low_file
|
| 55 |
|
|
|
|
| 56 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
|
|
|
|
|
|
| 57 |
with gr.Blocks(title="์์
๋ถ์๊ธฐ") as demo:
|
| 58 |
+
# ์์
ํ์ผ ์
๋ก๋ ์ปดํฌ๋ํธ (์์
ํ์ผ ํ์ฅ์ ์ง์ )
|
| 59 |
+
file_input = gr.File(label="์์
ํ์ผ ์
๋ก๋", file_types=[".xlsx", ".xls"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# ๋ ๊ฐ์ ๋ฒํผ ์์ฑ
|
|
|
|
|
|
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
+
btn_date = gr.Button("๋ฆฌ๋ทฐ๋ ์ง")
|
| 64 |
+
btn_score = gr.Button("๋ฆฌ๋ทฐ์ ์")
|
| 65 |
+
|
| 66 |
+
# ๋ ๊ฐ์ ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ ํ๋ฉด (A~F ์ด๋ง ํ์)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Row():
|
| 68 |
+
output1 = gr.Dataframe(label="์ถ๋ ฅ ํ๋ฉด 1 (A~F)")
|
| 69 |
+
output2 = gr.Dataframe(label="์ถ๋ ฅ ํ๋ฉด 2 (A~F)")
|
| 70 |
+
|
| 71 |
+
# ๋ค์ด๋ก๋ ๋งํฌ 4๊ฐ (๊ฐ CSV ํ์ผ ๋ค์ด๋ก๋)
|
| 72 |
with gr.Row():
|
| 73 |
+
dl_date_recent = gr.File(label="๋ฆฌ๋ทฐ๋ ์ง ๋ด๋ฆผ์ฐจ์ ๋ค์ด๋ก๋")
|
| 74 |
+
dl_date_old = gr.File(label="๋ฆฌ๋ทฐ๋ ์ง ์ค๋ฆ์ฐจ์ ๋ค์ด๋ก๋")
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
+
dl_score_high = gr.File(label="๋ฆฌ๋ทฐ์ ์ ๋ด๋ฆผ์ฐจ์ ๋ค์ด๋ก๋")
|
| 77 |
+
dl_score_low = gr.File(label="๋ฆฌ๋ทฐ์ ์ ์ค๋ฆ์ฐจ์ ๋ค์ด๋ก๋")
|
| 78 |
+
|
| 79 |
+
# ๋ฒํผ ํด๋ฆญ ์ ๊ฐ๊ฐ on_date, on_score ํจ์ ํธ์ถ
|
| 80 |
+
# ๊ฐ ํจ์๋ 6๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํจ: ๋ ๊ฐ์ ์ถ๋ ฅ ํ๋ฉด์ 4๊ฐ์ ๋ค์ด๋ก๋ ํ์ผ ๊ฒฝ๋ก
|
| 81 |
+
btn_date.click(on_date, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
|
| 82 |
+
btn_score.click(on_score, inputs=[file_input], outputs=[output1, output2, dl_date_recent, dl_date_old, dl_score_high, dl_score_low])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
demo.launch()
|
|
|