Spaces:
Build error
Build error
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()
|