CSB261 commited on
Commit
c3a7e1f
ยท
verified ยท
1 Parent(s): 937ccdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -49
app.py CHANGED
@@ -1,52 +1,62 @@
1
- import gradio as gr
2
  import pandas as pd
3
  import re
4
  from collections import Counter
5
- from openpyxl import Workbook
6
-
7
- def process_excel(file):
8
- # ์—‘์…€ ํŒŒ์ผ ์ฝ๊ธฐ
9
- df = pd.read_excel(file, sheet_name=0)
10
-
11
- # D4๋ถ€ํ„ฐ D์—ด์˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
12
- product_names = df.iloc[3:, 3].dropna() # D4๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
13
-
14
- # ํ‚ค์›Œ๋“œ ์ถ”์ถœ ๋ฐ ์ •์ œ
15
- all_keywords = []
16
- for name in product_names:
17
- # ํŠน์ˆ˜ ๋ฌธ์ž ์ œ๊ฑฐ ๋ฐ ํ‚ค์›Œ๋“œ ์ถ”์ถœ
18
- keywords = re.findall(r'\b\w+\b', name)
19
- # ์ค‘๋ณต ์ œ๊ฑฐ
20
- unique_keywords = set(keywords)
21
- all_keywords.extend(unique_keywords)
22
-
23
- # ๋นˆ๋„ ๊ณ„์‚ฐ
24
- keyword_counts = Counter(all_keywords)
25
- sorted_keywords = keyword_counts.most_common()
26
-
27
- # ๊ฒฐ๊ณผ๋ฅผ ์—‘์…€๋กœ ์ €์žฅ
28
- wb = Workbook()
29
- ws = wb.active
30
- ws.title = "Keywords"
31
- ws['A4'] = "ํ‚ค์›Œ๋“œ"
32
- ws['B4'] = "๋นˆ๋„"
33
-
34
- for idx, (keyword, count) in enumerate(sorted_keywords, start=5):
35
- ws[f'A{idx}'] = keyword
36
- ws[f'B{idx}'] = count
37
-
38
- result_file = "keyword_counts.xlsx"
39
- wb.save(result_file)
40
-
41
- return result_file
42
-
43
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
44
- iface = gr.Interface(
45
- fn=process_excel,
46
- inputs=gr.File(label="์—‘์…€ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”"),
47
- outputs=gr.File(label="๊ฒฐ๊ณผ ์—‘์…€ ํŒŒ์ผ"),
48
- title="์—‘์…€ ํ‚ค์›Œ๋“œ ๋ถ„์„๊ธฐ"
49
- )
50
-
51
- if __name__ == "__main__":
52
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, send_file
2
  import pandas as pd
3
  import re
4
  from collections import Counter
5
+ from werkzeug.utils import secure_filename
6
+ import os
7
+
8
+ app = Flask(__name__)
9
+ UPLOAD_FOLDER = 'uploads'
10
+ OUTPUT_FOLDER = 'outputs'
11
+
12
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
13
+ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
14
+
15
+ @app.route('/')
16
+ def index():
17
+ return '''
18
+ <h1>์—‘์…€ ํ‚ค์›Œ๋“œ ์ถ”์ถœ๊ธฐ</h1>
19
+ <form action="/process" method="post" enctype="multipart/form-data">
20
+ <input type="file" name="file" accept=".xlsx"/>
21
+ <button type="submit">์—…๋กœ๋“œ ๋ฐ ์ฒ˜๋ฆฌ</button>
22
+ </form>
23
+ '''
24
+
25
+ @app.route('/process', methods=['POST'])
26
+ def process():
27
+ file = request.files['file']
28
+ filename = secure_filename(file.filename)
29
+ filepath = os.path.join(UPLOAD_FOLDER, filename)
30
+ file.save(filepath)
31
+
32
+ # ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
33
+ df = pd.read_excel(filepath, engine='openpyxl')
34
+
35
+ # D4:D์—ด์˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
36
+ data = df.iloc[3:, 3].dropna().astype(str)
37
+
38
+ # ํ‚ค์›Œ๋“œ ์ฒ˜๋ฆฌ
39
+ keywords = []
40
+ for text in data:
41
+ text = re.sub(r'[^\w\s]', '', text) # ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ
42
+ keywords.extend(text.split())
43
+
44
+ # ํ‚ค์›Œ๋“œ ๋นˆ๋„ ๊ณ„์‚ฐ
45
+ keyword_counts = Counter(keywords)
46
+ sorted_keywords = sorted(keyword_counts.items(), key=lambda x: x[1], reverse=True)
47
+
48
+ # ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ž‘์„ฑ
49
+ result_df = pd.DataFrame(sorted_keywords, columns=['ํ‚ค์›Œ๋“œ', '๋นˆ๋„'])
50
+
51
+ # ๊ฒฐ๊ณผ ์—‘์…€๋กœ ์ €์žฅ
52
+ output_path = os.path.join(OUTPUT_FOLDER, 'result.xlsx')
53
+ with pd.ExcelWriter(output_path, engine='openpyxl') as writer:
54
+ result_df.to_excel(writer, index=False, startrow=4, startcol=0, header=False)
55
+ sheet = writer.sheets['Sheet1']
56
+ sheet.cell(row=4, column=1).value = "A5์…€๋ช…"
57
+ sheet.cell(row=4, column=2).value = "B5์…€๋ช…"
58
+
59
+ return send_file(output_path, as_attachment=True, download_name='result.xlsx')
60
+
61
+ if __name__ == '__main__':
62
+ app.run(debug=True)