Update app.py
Browse files
app.py
CHANGED
|
@@ -22,10 +22,11 @@ warnings.filterwarnings("ignore")
|
|
| 22 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 24 |
|
| 25 |
-
# --- 1. 資料讀取 (主要資料庫) ---
|
| 26 |
-
df = pd.DataFrame()
|
| 27 |
try:
|
| 28 |
-
|
|
|
|
| 29 |
df.columns = df.columns.str.strip() # 清理欄位空白
|
| 30 |
|
| 31 |
def classify_mood(row):
|
|
@@ -48,29 +49,33 @@ try:
|
|
| 48 |
|
| 49 |
if 'Mood_Tags' not in df.columns:
|
| 50 |
df['Mood_Tags'] = df.apply(classify_mood, axis=1)
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
print(f"⚠️ 讀取
|
| 53 |
|
| 54 |
-
# --- 2. 資料讀取 (Prompt 資料庫) ---
|
| 55 |
df_prompts = pd.DataFrame()
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
except FileNotFoundError:
|
| 71 |
-
print("⚠️ 找不到 restaurants_prompts_v6.csv")
|
| 72 |
except Exception as e:
|
| 73 |
-
print(f"⚠️
|
| 74 |
|
| 75 |
|
| 76 |
# --- RAG 建置 ---
|
|
@@ -227,7 +232,8 @@ def mood_agent_logic(score_input, food_input, diary_input, quiz_state, debug_mod
|
|
| 227 |
|
| 228 |
if pd.notna(csv_prompt) and str(csv_prompt).strip() != "":
|
| 229 |
img_prompt = str(csv_prompt)
|
| 230 |
-
|
|
|
|
| 231 |
else:
|
| 232 |
prompt_source = "⚠️ CSV 有此餐廳但 Prompt 空白"
|
| 233 |
except Exception as e:
|
|
|
|
| 22 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 24 |
|
| 25 |
+
# --- 1. 資料讀取 (主要資料庫 - 使用 restaurants.csv) ---
|
| 26 |
+
df = pd.DataFrame()
|
| 27 |
try:
|
| 28 |
+
# 修改:讀取 restaurants.csv
|
| 29 |
+
df = pd.read_csv('restaurants.csv', engine='python')
|
| 30 |
df.columns = df.columns.str.strip() # 清理欄位空白
|
| 31 |
|
| 32 |
def classify_mood(row):
|
|
|
|
| 49 |
|
| 50 |
if 'Mood_Tags' not in df.columns:
|
| 51 |
df['Mood_Tags'] = df.apply(classify_mood, axis=1)
|
| 52 |
+
|
| 53 |
+
print("✅ 成功讀取 restaurants.csv 並建立 Mood_Tags")
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"⚠️ 讀取 restaurants.csv 失敗: {e}")
|
| 56 |
|
| 57 |
+
# --- 2. 資料讀取 (Prompt 資料庫 - 使用 restaurants.csv) ---
|
| 58 |
df_prompts = pd.DataFrame()
|
| 59 |
+
# 修改:您的 CSV 中 Prompt 欄位名稱為 Visual_prompt
|
| 60 |
+
PROMPT_COL_NAME = 'Visual_prompt'
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# 直接使用已讀取的 df 來建立 Prompt 索引
|
| 64 |
+
if not df.empty:
|
| 65 |
+
df_prompts = df.copy()
|
| 66 |
+
|
| 67 |
+
if 'Name' in df_prompts.columns:
|
| 68 |
+
df_prompts.set_index('Name', inplace=True)
|
| 69 |
+
else:
|
| 70 |
+
print(f"⚠️ Prompts 處理警告: 找不到 'Name' 欄位")
|
| 71 |
|
| 72 |
+
if PROMPT_COL_NAME not in df_prompts.columns:
|
| 73 |
+
print(f"⚠️ Prompts 處理警告: 找不到 '{PROMPT_COL_NAME}' 欄位")
|
| 74 |
+
else:
|
| 75 |
+
print("⚠️ 主資料庫為空,無法建立 Prompt 索引")
|
| 76 |
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
+
print(f"⚠️ 處理 Prompt 資料發生錯誤: {e}")
|
| 79 |
|
| 80 |
|
| 81 |
# --- RAG 建置 ---
|
|
|
|
| 232 |
|
| 233 |
if pd.notna(csv_prompt) and str(csv_prompt).strip() != "":
|
| 234 |
img_prompt = str(csv_prompt)
|
| 235 |
+
# 修改:更新來源顯示為 restaurants.csv
|
| 236 |
+
prompt_source = "✅ CSV 檔案 (restaurants.csv)"
|
| 237 |
else:
|
| 238 |
prompt_source = "⚠️ CSV 有此餐廳但 Prompt 空白"
|
| 239 |
except Exception as e:
|