Update app.py
Browse files
app.py
CHANGED
|
@@ -1,168 +1,156 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import datetime as dt
|
| 4 |
-
import warnings
|
| 5 |
-
|
| 6 |
-
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 7 |
-
import plotly.graph_objects as go
|
| 8 |
import gradio as gr
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
monthly = (
|
| 40 |
-
df.groupby(["date", item_col])[price_col]
|
| 41 |
-
.mean()
|
| 42 |
-
.reset_index()
|
| 43 |
-
)
|
| 44 |
-
pivot = (
|
| 45 |
-
monthly
|
| 46 |
-
.pivot(index="date", columns=item_col, values=price_col)
|
| 47 |
-
.sort_index()
|
| 48 |
-
)
|
| 49 |
-
# ์ ์์์ผ MS ๋น๋๋ก ์ ๋ ฌ
|
| 50 |
-
pivot.index = pd.to_datetime(pivot.index).to_period("M").to_timestamp()
|
| 51 |
-
return pivot
|
| 52 |
-
|
| 53 |
-
pivot = load_data(DATA_FILE)
|
| 54 |
-
products = pivot.columns.tolist()
|
| 55 |
-
|
| 56 |
-
# -----------------------------
|
| 57 |
-
# 2. ๊ณ ์ ๋ชจ๋ธ ์ ์ (HoltโWinters + fallback)
|
| 58 |
-
# -----------------------------
|
| 59 |
-
|
| 60 |
-
def _fit_forecast(series: pd.Series) -> pd.Series:
|
| 61 |
-
"""์๋ณ ์๊ณ์ด โ 2025โ04 ์ดํ FORECAST_END_YEARโ12๊น์ง ์์ธก."""
|
| 62 |
-
# Ensure Monthly Start frequency
|
| 63 |
-
series = series.asfreq("MS")
|
| 64 |
-
|
| 65 |
-
# ์์ธก ๊ธฐ๊ฐ ๊ณ์ฐ
|
| 66 |
-
last_date = series.index[-1]
|
| 67 |
-
end_date = dt.datetime(FORECAST_END_YEAR, 12, 1)
|
| 68 |
-
horizon = (end_date.year - last_date.year) * 12 + (end_date.month - last_date.month)
|
| 69 |
-
if horizon <= 0:
|
| 70 |
-
return pd.Series(dtype=float)
|
| 71 |
-
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
| 121 |
)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
)
|
| 143 |
-
return fig
|
| 144 |
-
|
| 145 |
-
# -----------------------------
|
| 146 |
-
# 5. Gradio UI
|
| 147 |
-
# -----------------------------
|
| 148 |
-
with gr.Blocks(title="๋๋งค ๊ฐ๊ฒฉ ์์ธกย App") as demo:
|
| 149 |
-
gr.Markdown("## ๐ ๋๋งค ๊ฐ๊ฒฉ ์์ธก ๋์๋ณด๋ (1996โ2030)")
|
| 150 |
-
|
| 151 |
-
# ํ๋ชฉ ์ ํ โ ๊ทธ๋ํ ์
๋ฐ์ดํธ
|
| 152 |
-
item_dd = gr.Dropdown(products, value=products[0], label="ํ๋ชฉ ์ ํ")
|
| 153 |
-
chart_out = gr.Plot(label="๊ฐ๊ฒฉ ์ถ์ธ")
|
| 154 |
-
|
| 155 |
-
# ๋ด์ผ ๊ฐ๊ฒฉ ํ (์ด๊ธฐ ๊ณ ์ )
|
| 156 |
-
gr.Markdown(f"### ๋ด์ผ({tomorrow}) ๊ฐ ํ๋ชฉ ์์๊ฐ (KRW)")
|
| 157 |
-
tomorrow_table = gr.Dataframe(tomorrow_df, interactive=False, height=400)
|
| 158 |
-
|
| 159 |
-
def update_chart(product):
|
| 160 |
-
return plot_item(product)
|
| 161 |
-
|
| 162 |
-
item_dd.change(update_chart, inputs=item_dd, outputs=chart_out, queue=False)
|
| 163 |
|
| 164 |
-
#
|
| 165 |
-
# 6. ์คํ ์คํฌ๋ฆฝํธ ์ํธ๋ฆฌํฌ์ธํธ
|
| 166 |
-
# -----------------------------
|
| 167 |
if __name__ == "__main__":
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import chardet
|
| 6 |
+
|
| 7 |
+
def detect_encoding(file_path):
|
| 8 |
+
"""
|
| 9 |
+
Function to detect file encoding
|
| 10 |
+
"""
|
| 11 |
+
with open(file_path, 'rb') as f:
|
| 12 |
+
result = chardet.detect(f.read())
|
| 13 |
+
return result['encoding']
|
| 14 |
+
|
| 15 |
+
def merge_csv_files(files):
|
| 16 |
+
"""
|
| 17 |
+
Function to merge multiple CSV files into one
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
files: List of uploaded CSV files
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Path to the merged CSV file and status message
|
| 24 |
+
"""
|
| 25 |
+
if not files or len(files) == 0:
|
| 26 |
+
return None, "No files were uploaded. Please select CSV files to merge."
|
| 27 |
+
|
| 28 |
+
if len(files) > 30:
|
| 29 |
+
return None, "Maximum 30 files can be merged at once."
|
| 30 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
+
# Read all files into DataFrame list
|
| 33 |
+
dataframes = []
|
| 34 |
+
for file in files:
|
| 35 |
+
# Detect file encoding
|
| 36 |
+
encoding = detect_encoding(file.name)
|
| 37 |
+
try:
|
| 38 |
+
df = pd.read_csv(file.name, encoding=encoding)
|
| 39 |
+
except UnicodeDecodeError:
|
| 40 |
+
# Try other encodings if detected encoding fails
|
| 41 |
+
encodings_to_try = ['cp949', 'euc-kr', 'latin1', 'ISO-8859-1']
|
| 42 |
+
for enc in encodings_to_try:
|
| 43 |
+
try:
|
| 44 |
+
df = pd.read_csv(file.name, encoding=enc)
|
| 45 |
+
break
|
| 46 |
+
except UnicodeDecodeError:
|
| 47 |
+
continue
|
| 48 |
+
else:
|
| 49 |
+
return None, f"Could not determine encoding for '{os.path.basename(file.name)}'."
|
| 50 |
+
|
| 51 |
+
dataframes.append(df)
|
| 52 |
+
|
| 53 |
+
# Merge all DataFrames
|
| 54 |
+
if dataframes:
|
| 55 |
+
merged_df = pd.concat(dataframes, ignore_index=True)
|
| 56 |
+
|
| 57 |
+
# Save to temporary file
|
| 58 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
| 59 |
+
output_path = tmp.name
|
| 60 |
+
|
| 61 |
+
# Save merged data in Excel-compatible format (UTF-8 with BOM)
|
| 62 |
+
merged_df.to_csv(output_path, index=False, encoding='utf-8-sig')
|
| 63 |
+
|
| 64 |
+
return output_path, f"Successfully merged {len(files)} files. Please open with UTF-8 encoding in Excel."
|
| 65 |
+
else:
|
| 66 |
+
return None, "No data to merge."
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return None, f"Error occurred: {str(e)}"
|
| 70 |
+
|
| 71 |
+
# Create a stylish Gradio interface
|
| 72 |
+
with gr.Blocks(title="CSVFusion") as app:
|
| 73 |
+
gr.Markdown(
|
| 74 |
+
"""
|
| 75 |
+
# ๐ CSVFusion: Intelligent File Merger
|
| 76 |
+
|
| 77 |
+
*Seamlessly combine multiple CSV files into one unified dataset*
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
"""
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=2):
|
| 85 |
+
gr.Markdown("""
|
| 86 |
+
### How to use CSVFusion:
|
| 87 |
+
1. Upload up to 30 CSV files using the panel on the right
|
| 88 |
+
2. Click the "Merge Files" button
|
| 89 |
+
3. Download your consolidated CSV file
|
| 90 |
+
|
| 91 |
+
### Features:
|
| 92 |
+
- Automatic encoding detection
|
| 93 |
+
- Handles various CSV formats
|
| 94 |
+
- Excel-compatible output (UTF-8)
|
| 95 |
+
- Preserves all data columns
|
| 96 |
+
""")
|
| 97 |
+
|
| 98 |
+
with gr.Column(scale=3):
|
| 99 |
+
input_files = gr.File(
|
| 100 |
+
file_count="multiple",
|
| 101 |
+
label="Upload CSV Files (Max 30)",
|
| 102 |
+
file_types=[".csv"],
|
| 103 |
+
elem_id="file_upload"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
merge_button = gr.Button("Merge Files", variant="primary", size="lg")
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
with gr.Column():
|
| 111 |
+
status = gr.Textbox(label="Status", placeholder="Ready to merge your files...")
|
| 112 |
+
with gr.Column():
|
| 113 |
+
output_file = gr.File(label="Download Merged CSV")
|
| 114 |
+
|
| 115 |
+
# Add custom CSS for better visual appeal
|
| 116 |
+
gr.HTML("""
|
| 117 |
+
<style>
|
| 118 |
+
.gradio-container {
|
| 119 |
+
background: linear-gradient(to right, #f9f9f9, #ffffff);
|
| 120 |
+
border-radius: 12px;
|
| 121 |
+
}
|
| 122 |
+
#file_upload {
|
| 123 |
+
border: 2px dashed #3498db;
|
| 124 |
+
border-radius: 8px;
|
| 125 |
+
padding: 20px;
|
| 126 |
+
transition: all 0.3s;
|
| 127 |
+
}
|
| 128 |
+
#file_upload:hover {
|
| 129 |
+
border-color: #2980b9;
|
| 130 |
+
box-shadow: 0 0 10px rgba(52, 152, 219, 0.3);
|
| 131 |
+
}
|
| 132 |
+
.footer {
|
| 133 |
+
text-align: center;
|
| 134 |
+
margin-top: 30px;
|
| 135 |
+
color: #7f8c8d;
|
| 136 |
+
font-size: 0.9em;
|
| 137 |
+
}
|
| 138 |
+
</style>
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
# Add footer
|
| 142 |
+
gr.HTML("""
|
| 143 |
+
<div class="footer">
|
| 144 |
+
<p>CSVFusion ยฉ 2025 - A powerful tool for data professionals</p>
|
| 145 |
+
</div>
|
| 146 |
+
""")
|
| 147 |
+
|
| 148 |
+
merge_button.click(
|
| 149 |
+
fn=merge_csv_files,
|
| 150 |
+
inputs=[input_files],
|
| 151 |
+
outputs=[output_file, status]
|
| 152 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Run the app
|
|
|
|
|
|
|
| 155 |
if __name__ == "__main__":
|
| 156 |
+
app.launch()
|