File size: 7,240 Bytes
8fe4260 39d47ff 8fe4260 4c7ed94 8fe4260 4c7ed94 8fe4260 c81c64b 8fe4260 39d47ff 8fe4260 | 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import gradio as gr
import pandas as pd
from sklearn.cluster import KMeans
import numpy as np
import os
from huggingface_hub import hf_hub_download
# Hugging Face ํ ํฐ ์ค์
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
print("ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค. Repository Secrets์ HF_TOKEN์ด ์๋์ง ํ์ธํ์ธ์.")
else:
print("ํ ํฐ์ด ์ค์ ๋์์ต๋๋ค. ํ์ผ์ ๋ค์ด๋ก๋ํฉ๋๋ค.")
# ์ ํํ repo_id, ํ์ผ ์ด๋ฆ, repo_type ์ค์
file_path = hf_hub_download(
repo_id="HCIlab-SMWU/final_petfood_dataset",
filename="new_data_with_features.xlsx",
repo_type="dataset",
use_auth_token=hf_token
)
print(f"ํ์ผ์ด ๋ค์ด๋ก๋๋์์ต๋๋ค: {file_path}")
# Excel ํ์ผ์ pandas DataFrame์ผ๋ก ์ฝ๊ธฐ
df = pd.read_excel(file_path)
# DataFrame ํ์ธ
print(df.head())
# ํ ๊ฐ์ ํ์ธ
row_count = df.shape[0]
print(f"DataFrame์ ํ ๊ฐ์๋: {row_count}")
key1 = os.getenv("secret1")
key2 = os.getenv("secret2")
# ํํฐ๋ง ํจ์
def filter_feed(age_input, allergies_input, health_concerns_input, pet_type_input, sort_option):
data = df.copy()
error_message = "" # ์๋ฌ ๋ฉ์์ง ์ด๊ธฐํ
# ๋ฐ๋ ค๋๋ฌผ ์ข
๋ฅ ํํฐ๋ง
if pet_type_input:
data = data[data['์ข
'] == pet_type_input]
# ์ฐ๋ น ํํฐ๋ง
if age_input == "1์ด ๋ฏธ๋ง":
age_filter = ["ํผํผ", "ํคํผ", "์ ์ฐ๋ น"]
elif age_input == "1์ด ์ด์, 7์ด ์ดํ":
age_filter = ["์ด๋ํธ", "์ ์ฐ๋ น"]
else:
age_filter = ["์๋์ด", "์ ์ฐ๋ น"]
data = data[data['๊ธ์ฌ๋์'].str.contains('|'.join(age_filter), na=False)]
if allergies_input:
allergy_pattern = '|'.join(allergies_input)
data = data[~data[key2].str.contains(allergy_pattern, na=False)]
data = data[data[key2].notna() & (data[key2] != "")]
# ๊ฑด๊ฐ ๊ณ ๋ฏผ ํํฐ๋ง
health_mapping = {
"์น์/๊ตฌ๊ฐ": ["์น์์ ๊ฑฐ", "๊ตฌ๊ฐ๊ด๋ฆฌ"],
"๋ผ/๊ด์ ": ["๊ด์ ๊ฐํ"],
"ํผ๋ถ/๋ชจ์ง": ["ํผ๋ชจ๊ด๋ฆฌ"],
"์๋ฌ์ง": ["์ ์๋ฌ์ง"],
"๋น๋ง": ["๋ค์ด์ดํธ/์ค์ฑํ", "์ฒด์ค์ ์ง"],
"๋น๋จ๊ธฐ": ["์ ๋ฆฌ๋๋ฆฌ(๋น๋จ๊ณ)", "๊ฒฐ์์๋ฐฉ", "์ ์ฅ/์๋ก", "์์๋์ฆ์ง"],
"๋": ["๋๊ฑด๊ฐ"],
"์ํ๊ธฐ": ["์ํ๊ฐ์ "],
"ํ๋": ["๋ถ๋ฆฌ๋ถ์ํด์", "์คํธ๋ ์ค์ํ"],
"์ฌ์ฅ": ["์ฌ์ฅ๊ฑด๊ฐ"],
"ํธํก๊ธฐ": ["ํธํก๊ธฐ๊ด๋ฆฌ"],
"๋
ธํ": ["ํญ์ฐํ"],
"ํค์ด๋ณผ": ["ํค์ด๋ณผ"]
}
health_patterns = []
# ๊ฑด๊ฐ ๊ณ ๋ฏผ์ด 3๊ฐ๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ
if len(health_concerns_input) > 3:
error_message = "๊ฑด๊ฐ ๊ณ ๋ฏผ์ ์ต๋ 3๊ฐ๊น์ง ์ ํ ๊ฐ๋ฅํฉ๋๋ค."
return pd.DataFrame(), error_message # ๋น ๋ฐ์ดํฐํ๋ ์๊ณผ ์๋ฌ ๋ฉ์์ง ๋ฐํ
for concern in health_concerns_input:
health_patterns.append(health_mapping.get(concern, []))
# ๋ชจ๋ ๊ฑด๊ฐ ๊ณ ๋ฏผ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ฐ์ดํฐ ํํฐ๋ง
filtered_data = data.copy()
for patterns in health_patterns:
if patterns: # ๋น ํจํด์ด ์๋ ๊ฒฝ์ฐ
filtered_data = filtered_data[filtered_data[key1].str.contains('|'.join(patterns), na=False)]
# ๊ฒฐ๊ณผ์ ์ข
, ๊ธ์ฌ๋์, ๊ธฐ๋ฅ, ์ฃผ์๋ฃ ์ถ๊ฐ
results = filtered_data[['Cleaned_Product_Name', '์ข
', '๊ธ์ฌ๋์', '๊ธฐ๋ฅ', '์ฃผ์๋ฃ']]
# KMeans๋ฅผ ์ ์ฉํ์ฌ ํด๋ฌ์คํฐ๋ง
if len(results) > 10:
# ์-ํซ ์ธ์ฝ๋ฉ ์ํ
one_hot_ingredients = pd.get_dummies(results[key2].str.split('|').explode()).groupby(level=0).max()
one_hot_features = pd.get_dummies(results[key1].str.split('|').explode()).groupby(level=0).max()
# ์-ํซ ์ธ์ฝ๋ฉ๋ ๋ฐ์ดํฐ์ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉ
features = pd.concat([one_hot_ingredients, one_hot_features], axis=1)
# KMeans ๋ชจ๋ธ ์์ฑ ๋ฐ ํ์ต
kmeans = KMeans(n_clusters=10, random_state=0)
kmeans.fit(features)
# ๊ฐ ํด๋ฌ์คํฐ์ ์ค์ฌ์ ์ ๋ํ ๊ฑฐ๋ฆฌ ๊ณ์ฐ
distances = kmeans.transform(features)
# ๊ฐ ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ์ํ ํด๋ฌ์คํฐ์ ๊ทธ ์ค์ฌ์ ๊ณผ์ ๊ฑฐ๋ฆฌ ์ถ๊ฐ
results['cluster'] = kmeans.labels_
results['distance_to_centroid'] = [distances[i][label] for i, label in enumerate(kmeans.labels_)]
# ๊ฐ ํด๋ฌ์คํฐ์์ ์ค์ฌ์ ์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ์ดํฐ๋ง ์ ํ
closest_to_centroid = results.loc[results.groupby('cluster')['distance_to_centroid'].idxmin()]
# ์ ๋ ฌ ์ต์
์ ๋ฐ๋ผ ์ ๋ ฌ ์ํ
if sort_option == "๊ฐ๋๋ค์":
closest_to_centroid = closest_to_centroid.sort_values(by=['Cleaned_Product_Name']).reset_index(drop=True)
results = closest_to_centroid[['Cleaned_Product_Name', '์ข
', '๊ธ์ฌ๋์', '๊ธฐ๋ฅ', '์ฃผ์๋ฃ']]
# 'Cleaned_Product_Name' ์ด ์ด๋ฆ์ '์ฌ๋ฃ์ด๋ฆ'์ผ๋ก ๋ณ๊ฒฝ
results = results.rename(columns={'Cleaned_Product_Name': '์ฌ๋ฃ์ด๋ฆ'})
# ์๋ฌ ๋ฉ์์ง ์ฒ๋ฆฌ
if results.empty:
error_message = "์๋ฌ์ง์ ๊ฑด๊ฐ๊ณ ๋ฏผ ์กฐ๊ฑด์ ๋ชจ๋ ๋ง์กฑํ๋ ์ฌ๋ฃ๊ฐ ์์ต๋๋ค."
return results, error_message
# Gradio ์ธํฐํ์ด์ค ์ค์
with gr.Blocks() as demo:
gr.Markdown("# ๋ฐ๋ ค๋๋ฌผ ์ฌ๋ฃ ์ถ์ฒ")
age_input = gr.Dropdown(["1์ด ๋ฏธ๋ง", "1์ด ์ด์, 7์ด ์ดํ", "7์ด ์ด์"], label="์ฐ๋ น๋")
allergies_input = gr.CheckboxGroup(
["์", "๋ผ์ง", "๋ญ", "์ค๋ฆฌ", "์", "์น ๋ฉด์กฐ", "์์ /ํด์ฐ๋ฌผ", "์ฌ์ด", "์ฐ์ด", "์น์ฆ/์ ์ง๋ฐฉ", "์ฐธ์น", "๋ฐ", "์","๊ณ ๊ตฌ๋ง","๊ณก๋ฌผ","๊ณค์ถฉ","๊ณผ์ผ/์ผ์ฑ","๋ถ์ด","์ฒญ์ด"],
label="์ฃผ์๋ฃ ์๋ฌ์ง"
)
health_concerns_input = gr.CheckboxGroup(
["์น์/๊ตฌ๊ฐ", "๋ผ/๊ด์ ", "ํผ๋ถ/๋ชจ์ง", "์๋ฌ์ง", "๋น๋ง", "๋น๋จ๊ธฐ", "๋", "์ํ๊ธฐ", "ํ๋", "์ฌ์ฅ", "ํธํก๊ธฐ", "๋
ธํ", "ํค์ด๋ณผ"],
label="๊ฑด๊ฐ ๊ณ ๋ฏผ (์ต๋ 3๊ฐ ์ ํ ๊ฐ๋ฅ)"
)
pet_type_input = gr.Dropdown(["๊ฐ์์ง", "๊ณ ์์ด"], label="๋ฐ๋ ค๋๋ฌผ ์ข
๋ฅ")
sort_option = gr.Radio(["์ถ์ฒ์", "๊ฐ๋๋ค์"], label="์ ๋ ฌ ๋ฐฉ์", value="์ถ์ฒ์")
submit_button = gr.Button("์ถ์ฒ ์ฌ๋ฃ ๋ณด๊ธฐ")
output = gr.Dataframe()
error_output = gr.Textbox(label="์๋ฌ ๋ฉ์์ง", interactive=False)
sort_option.change(
fn=filter_feed,
inputs=[age_input, allergies_input, health_concerns_input, pet_type_input, sort_option],
outputs=[output, error_output]
)
submit_button.click(
fn=filter_feed,
inputs=[age_input, allergies_input, health_concerns_input, pet_type_input, sort_option],
outputs=[output, error_output]
)
def check_health_concerns(health_concerns_input):
if len(health_concerns_input) > 3:
return "๊ฑด๊ฐ ๊ณ ๋ฏผ์ ์ต๋ 3๊ฐ๊น์ง ์ ํ ๊ฐ๋ฅํฉ๋๋ค."
return ""
health_concerns_input.change(fn=check_health_concerns, inputs=health_concerns_input, outputs=error_output)
demo.launch(share=True) |