Spaces:
Build error
Build error
Download app.py from SG34/Keyword_Analyzer: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/SG34/Keyword_Analyzer/resolve/main/app.py
- Command line
-
hf download hf://spaces/SG34/Keyword_Analyzer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SG34/Keyword_Analyzer/resolve/main/app.py
2.57 kB
| 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() | |