Spaces:
No application file
No application file
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LogisticRegression | |
| # ------------------------ | |
| # 1. ๋ฐ์ดํฐ ๋ก๋ | |
| # ------------------------ | |
| df = pd.read_excel("Restaurants.xlsx") | |
| df["๊ฐ๊ฒฉ๋"] = df["๊ฐ๊ฒฉ๋"].astype(float) | |
| # Synthetic label ์์ฑ | |
| ideal_budget = 12000 | |
| df["budget_diff"] = abs(df["๊ฐ๊ฒฉ๋"] - ideal_budget) | |
| df["label"] = np.where( | |
| (df["ํ์ "] >= 4.0) & (df["budget_diff"] <= 3000), | |
| 1, | |
| 0 | |
| ) | |
| # ๋จธ์ ๋ฌ๋์ฉ ์ธ์ฝ๋ฉ | |
| df_ml = pd.get_dummies(df, columns=["์์์ข ๋ฅ", "์ฐ๋ น์ธต", "๋ฐฉ๋ฌธ๋ชฉ์ "]) | |
| X = df_ml.drop(columns=["์๋น๋ช ", "๋ฆฌ๋ทฐ", "์์น(์งํ์ฒ ์ญ)", "label"]) | |
| y = df_ml["label"] | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.2, random_state=42 | |
| ) | |
| # ------------------------ | |
| # 2. ML ๋ชจ๋ธ (HuggingFace์์ 100% ์คํ ๊ฐ๋ฅํ LogisticRegression) | |
| # ------------------------ | |
| model = LogisticRegression(max_iter=500) | |
| model.fit(X_train, y_train) | |
| df["predict_score"] = model.predict_proba(X)[:, 1] | |
| # ------------------------ | |
| # 3. ์ถ์ฒ ํจ์ | |
| # ------------------------ | |
| def recommend_ai(region, food_type, budget, age): | |
| # ----- ๊ธฐ๋ณธ ํํฐ ----- | |
| filtered = df[(df["์์น(์งํ์ฒ ์ญ)"] == region) & | |
| (df["์์์ข ๋ฅ"] == food_type)] | |
| if filtered.empty: | |
| return "์กฐ๊ฑด์ ๋ง๋ ์๋น์ด ์์ต๋๋ค." | |
| filtered = filtered.copy() | |
| # ----- ๐ฅ ์์ฐ ํํฐ: budget - 3000 ~ budget + 3000 ----- | |
| min_price = budget - 3000 | |
| max_price = budget + 3000 | |
| filtered = filtered[(filtered["๊ฐ๊ฒฉ๋"] >= min_price) & | |
| (filtered["๊ฐ๊ฒฉ๋"] <= max_price)] | |
| if filtered.empty: | |
| return f"์์ฐ ๋ฒ์({min_price}์ ~ {max_price}์)์ ๋ง๋ ์๋น์ด ์์ต๋๋ค." | |
| # ์์ฐ ์ฐจ์ด | |
| filtered["user_budget_diff"] = abs(filtered["๊ฐ๊ฒฉ๋"] - budget) | |
| # ์ต์ข ์ ์ ๊ณ์ฐ | |
| filtered["final_score"] = ( | |
| filtered["predict_score"] * 0.7 + | |
| (1 / (filtered["user_budget_diff"] + 1)) * 0.3 | |
| ) | |
| # Top 5 | |
| result = filtered.sort_values("final_score", ascending=False).head(5) | |
| top = result.iloc[0] | |
| # ----------------------------- | |
| # ๐ TOP 1 ์ถ๋ ฅ | |
| # ----------------------------- | |
| output = "" | |
| output += "<div style='font-size:20px; font-weight:bold; margin-bottom:10px;'>" | |
| output += "๐โจ <b>AI์ 1๋ฑ ์ถ์ฒ ์๋น!</b> โจ๐" | |
| output += "</div>" | |
| output += "<div style='font-size:16px; line-height:1.7;'>" | |
| output += f"โญ <b>{top['์๋น๋ช ']}</b><br>" | |
| output += f"๐ ์์น: {top['์์น(์งํ์ฒ ์ญ)']}<br>" | |
| output += f"๐ฑ ์ข ๋ฅ: {top['์์์ข ๋ฅ']}<br>" | |
| output += f"๐ฐ ๊ฐ๊ฒฉ๋: {int(top['๊ฐ๊ฒฉ๋'])}์<br>" | |
| output += f"โญ ํ์ : {top['ํ์ ']}<br>" | |
| output += f"๐ฌ ๋ฆฌ๋ทฐ: {top['๋ฆฌ๋ทฐ']}<br>" | |
| output += "</div>" | |
| # ----- ํ ์คํธ ๊ธฐ๋ฐ ๊ตฌ๋ถ์ ----- | |
| output += "<div style='font-family:monospace; margin: 12px 0 10px 0;'>" | |
| output += "=" * 80 | |
| output += "</div>" | |
| # ----------------------------- | |
| # ๐ ๊ทธ ์ธ ์ถ์ฒ ์๋น๋ค | |
| # ----------------------------- | |
| output += "<div style='margin-top:5px; margin-bottom:10px;'>" | |
| output += "<h3 style='margin:0; padding:0;'>๐ ๊ทธ ์ธ ์ถ์ฒ ์๋น๋ค</h3>" | |
| output += "</div>" | |
| for _, row in result.iloc[1:].iterrows(): | |
| output += "<div style='font-size:14px; line-height:1.55; margin-top:8px; margin-bottom:12px;'>" | |
| output += f"โญ {row['์๋น๋ช ']}<br>" | |
| output += f"๐ฑ {row['์์์ข ๋ฅ']} | ๐ฐ {int(row['๊ฐ๊ฒฉ๋'])}์ | โญ {row['ํ์ ']}<br>" | |
| output += f"๐ฌ {row['๋ฆฌ๋ทฐ']}<br>" | |
| output += "<div style='margin:10px 0 6px 0; border-bottom:1px solid #ccc;'></div>" | |
| output += "</div>" | |
| return output | |
| # ------------------------ | |
| # 4. Gradio UI | |
| # ------------------------ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ๐ค AI ๊ธฐ๋ฐ ๋จธ์ ๋ฌ๋ ์์ธ ๋ง์ง ์ถ์ฒ ์์คํ ") | |
| region = gr.Dropdown( | |
| choices=sorted(df["์์น(์งํ์ฒ ์ญ)"].unique()), | |
| label="์งํ์ฒ ์ญ ์ ํ" | |
| ) | |
| food_type = gr.Dropdown( | |
| choices=sorted(df["์์์ข ๋ฅ"].unique()), | |
| label="์์ ์ข ๋ฅ" | |
| ) | |
| budget = gr.Slider( | |
| 5000, 30000, value=12000, step=500, | |
| label="์์ฐ(์): ์ค์ฐจ๋ฒ์ ยฑ 3,000์์ ์ ์ฉํฉ๋๋ค." | |
| ) | |
| age = gr.Dropdown( | |
| choices=sorted(df["์ฐ๋ น์ธต"].unique()), | |
| label="์ฐ๋ น์ธต" | |
| ) | |
| btn = gr.Button("๐ AI ์ถ์ฒ๋ฐ๊ธฐ") | |
| output_box = gr.HTML() | |
| btn.click( | |
| recommend_ai, | |
| inputs=[region, food_type, budget, age], | |
| outputs=output_box | |
| ) | |