File size: 2,573 Bytes
1df3b7e
 
 
 
 
fc2340b
ff1107a
fc2340b
ff1107a
 
 
 
 
 
fc2340b
 
1df3b7e
 
 
 
 
 
 
 
 
 
 
 
 
 
afe293b
1df3b7e
 
 
 
 
 
 
 
 
 
 
 
 
 
afe293b
 
1df3b7e
 
 
 
 
afe293b
1df3b7e
 
 
 
 
 
 
eaf004d
 
 
 
1df3b7e
 
 
 
afe293b
 
112dba7
afe293b
 
1df3b7e
 
 
a734156
1
2
3
4
5
6
7
8
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
import pandas as pd
from collections import Counter
import matplotlib.pyplot as plt
import re
import matplotlib.font_manager as fm
import urllib.request

# Hugging Face Space์—์„œ ํฐํŠธ ๋‹ค์šด๋กœ๋“œ
font_url = "https://huggingface.co/spaces/SG34/Keyword_Analyzer/resolve/main/NotoSansKR-Medium.ttf"
font_path = "/tmp/NotoSansKR-Medium.ttf"
urllib.request.urlretrieve(font_url, font_path)

# ๋‹ค์šด๋กœ๋“œํ•œ ํฐํŠธ๋ฅผ matplotlib์— ์„ค์ •
fontprop = fm.FontProperties(fname=font_path)
plt.rc('font', family=fontprop.get_name())

def extract_keywords(product_name):
    # ๋ชจ๋ธ๋ช… ์ œ๊ฑฐ๋ฅผ ์œ„ํ•œ ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด ์„ค์ •
    model_pattern = r'\b[A-Z]{1,3}-\w+\b'
    
    # ๋ชจ๋ธ๋ช…์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‚จ์€ ํ…์ŠคํŠธ ์ถ”์ถœ
    cleaned_name = re.sub(model_pattern, '', product_name)
    
    # ๊ตฌ๋‘์  ์ œ๊ฑฐ ๋ฐ ํ…์ŠคํŠธ๋ฅผ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
    cleaned_name = re.sub(r'[^\w\s]', '', cleaned_name)
    keywords = [word.strip() for word in cleaned_name.split() if word]
    
    return keywords

def process_file(file, num_keywords):
    # ์—‘์…€ ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ธฐ
    df = pd.read_excel(file)
    
    # ์ƒํ’ˆ๋ช… ์—ด ์ถ”์ถœ
    product_names = df.iloc[2:, 3]  # ์ƒํ’ˆ๋ช…์ด ์žˆ๋Š” ์—ด์„ ๊ฐ€์ ธ์˜ด
    
    # ๋ชจ๋“  ์ƒํ’ˆ๋ช…์—์„œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ
    keywords = []
    for name in product_names:
        keywords.extend(extract_keywords(name))
    
    # ํ‚ค์›Œ๋“œ ๋นˆ๋„์ˆ˜ ๊ณ„์‚ฐ
    keyword_counts = Counter(keywords)
    
    # ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๊ฐœ์ˆ˜๋งŒํผ ์ƒ์œ„ ํ‚ค์›Œ๋“œ ์ถ”์ถœ
    common_keywords = keyword_counts.most_common(num_keywords)
    
    # ํ‚ค์›Œ๋“œ ๋นˆ๋„์ˆ˜ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
    keywords, counts = zip(*common_keywords)
    plt.figure(figsize=(10, 6))
    plt.bar(keywords, counts)
    plt.title(f'Top {num_keywords} Keywords by Frequency')
    plt.xlabel('Keywords')
    plt.ylabel('Frequency')
    plt.xticks(rotation=45)
    
    # ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ฐ˜ํ™˜
    plt.savefig('keyword_frequency.png')
    
    # ๊ฒฐ๊ณผ ์ถœ๋ ฅ์šฉ ํ…์ŠคํŠธ ์ƒ์„ฑ
    result_text = "\n".join([f"{keyword}: {count}" for keyword, count in common_keywords])
    
    return result_text, 'keyword_frequency.png'

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
demo = gr.Interface(
    fn=process_file, 
    inputs=[
        gr.File(label="Upload Excel File"),
        gr.Dropdown(choices=[10, 20, 30, 40, 50], value=10, label="Number of Top Keywords")
    ], 
    outputs=[gr.Textbox(label="Top Keywords"), gr.Image(type="filepath", label="Keyword Frequency Graph")]
)

if __name__ == "__main__":
    demo.launch()