Sangwon78 commited on
Commit
99d05a2
·
verified ·
1 Parent(s): 0349af2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -2
app.py CHANGED
@@ -99,8 +99,8 @@ def create_app():
99
  def search_data(file, store_name):
100
  if not file:
101
  return "엑셀 파일을 업로드 해주세요."
102
- # 여기서는 첫 행을 헤더로 가정합니다 (A1=가맹점명, B1=주소, ...)
103
- df2 = pd.read_excel(file.name)
104
  # A열(첫 번째 컬럼)에서 store_name과 같은 행을 찾음
105
  row = df2[df2[df2.columns[0]] == store_name]
106
  if row.empty:
@@ -126,6 +126,55 @@ def create_app():
126
  outputs=text_output
127
  )
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  return demo
130
 
131
  if __name__ == "__main__":
 
99
  def search_data(file, store_name):
100
  if not file:
101
  return "엑셀 파일을 업로드 해주세요."
102
+ # 여기서는 첫 행을 헤더로 가정합니다 (A1=가맹점명, B1=주소, C1=업종명, D1=데이터일자)
103
+ df2 = pd.read_excel(file.name)
104
  # A열(첫 번째 컬럼)에서 store_name과 같은 행을 찾음
105
  row = df2[df2[df2.columns[0]] == store_name]
106
  if row.empty:
 
126
  outputs=text_output
127
  )
128
 
129
+ #####################################################
130
+ # CSV 파일을 읽고 다운로드할 수 있게 해주는 추가 기능 #
131
+ #####################################################
132
+
133
+ # CSV 업로드
134
+ file_input_csv = gr.File(label="CSV 파일 업로드", file_types=[".csv"])
135
+ generate_button_csv = gr.Button("데이터 추출 및 결과 생성(CSV)")
136
+
137
+ output_df_csv = gr.DataFrame(label="처리된 결과 미리보기(CSV)")
138
+ download_file_csv = gr.File(label="결과 CSV 다운로드")
139
+
140
+ def process_csv(file):
141
+ # CSV 읽기
142
+ df = pd.read_csv(file.name, header=None)
143
+ df.columns = ["고객ID", "리뷰날짜", "리뷰내용", "리뷰점수"] + list(df.columns[4:])
144
+ df["리뷰내용"] = df["리뷰내용"].astype(str).apply(remove_special_characters)
145
+ df_use = df[["고객ID", "리뷰날짜", "리뷰내용", "리뷰점수"]]
146
+
147
+ all_ids = df_use["고객ID"].dropna().unique()
148
+ combined_id = ", ".join(map(str, all_ids))
149
+ all_dates = df_use["리뷰날짜"].dropna().unique()
150
+ combined_dates = ", ".join(map(str, sorted(all_dates)))
151
+
152
+ keywords = extract_keywords(df_use["리뷰내용"].tolist())
153
+ df_use["리뷰점수"] = pd.to_numeric(df_use["리뷰점수"], errors='coerce')
154
+ score_mean = df_use["리뷰점수"].mean()
155
+
156
+ # CSV로 저장
157
+ output_csv = io.StringIO()
158
+ df_use.to_csv(output_csv, index=False, encoding='utf-8')
159
+ output_csv.seek(0)
160
+
161
+ # 임시 CSV 파일로 저장
162
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
163
+ tmp.write(output_csv.getvalue().encode('utf-8'))
164
+ tmp_path_csv = tmp.name
165
+
166
+ return df_use, tmp_path_csv
167
+
168
+ def on_click_csv(file):
169
+ df_preview, tmp_path_csv = process_csv(file)
170
+ return df_preview, tmp_path_csv
171
+
172
+ generate_button_csv.click(
173
+ fn=on_click_csv,
174
+ inputs=file_input_csv,
175
+ outputs=[output_df_csv, download_file_csv]
176
+ )
177
+
178
  return demo
179
 
180
  if __name__ == "__main__":