import streamlit as st
import google.generativeai as genai
import textwrap # For wrapping long text
# --- API Key Setup (Replace with your actual keys) ---
API_KEYS = [
"AIzaSyCnvlaZII4rP6UUy9LHL2u5Ab7CpST7U9g",
"AIzaSyBXg8nVnRCrLcbBTby7j5yDoKaQvfo9rFk",
"AIzaSyCbPsdkZ8DGCAOSlfrXhh5JP2OLYEDHcJg"
]
# --- Helper Functions ---
def call_gemini_api(prompt):
for api_key in API_KEYS:
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(prompt)
return response.text.strip()
except Exception as e:
error_message = str(e)
if "insufficient_quota" in error_message or "Quota exceeded" in error_message:
continue
else:
return f"❌ เกิดข้อผิดพลาด: {error_message}"
return "⚠️ API ทั้งหมดหมดโควต้าแล้ว กรุณาตรวจสอบบัญชีของคุณ"
def process_menus(response_text):
menu_list = response_text.split("🍽️ เมนูที่")
menu_list = [menu.strip() for menu in menu_list if menu.strip()]
if not menu_list:
menu_list = response_text.split("\n- ")
menu_list = [menu.strip() for menu in menu_list if menu.strip()]
if not menu_list:
menu_list = response_text.split("\n• ")
menu_list = [menu.strip() for menu in menu_list if menu.strip()]
return menu_list
# --- Custom CSS ---
st.markdown("""
""", unsafe_allow_html=True)
# --- App UI ---
st.markdown("
🍽️ Smart Cooking App 😎
", unsafe_allow_html=True)
with st.container(border=True):
option = st.radio("🔹 เลือกโหมด:", ["สร้างเมนูจากวัตถุดิบ", "ค้นหาเมนูสำหรับซื้อ"],
horizontal=True, key="mode_select")
if option == "สร้างเมนูจากวัตถุดิบ":
st.subheader("✨ สร้างเมนูแบบกำหนดเอง")
with st.expander("📝 กรอกวัตถุดิบของคุณ", expanded=True):
ingredients = st.text_area("วัตถุดิบ (คั่นด้วยจุลภาค):",
placeholder="เช่น ไข่, หมูสับ, ผักกาด...",
height=120)
with st.expander("⚙️ ปรับแต่งเมนูของคุณ", expanded=False):
col1, col2 = st.columns(2)
with col1:
num_ingredients = st.number_input("จำนวนวัตถุดิบหลัก", min_value=1, max_value=20, value=3, step=1)
category = st.selectbox("ประเภทอาหาร",
["อาหารทั่วไป", "มังสวิรัติ", "อาหารคลีน", "อาหารไทย", "อาหารญี่ปุ่น", "อาหารตะวันตก"])
calories = st.slider("แคลอรี่ที่ต้องการ (kcal)", 100, 1500, 500, step=50)
with col2:
difficulty = st.radio("ระดับความยาก", ["ง่าย", "ปานกลาง", "ยาก"], horizontal=True)
cook_time = st.slider("เวลาทำอาหาร (นาที)", 5, 180, 30, step=5)
if st.button("🍳 สร้างเมนู", use_container_width=True):
if ingredients:
prompt = (f"ฉันมี: {ingredients} ({num_ingredients} วัตถุดิบหลัก) "
f"แนะนำเมนู {category} เวลาทำไม่เกิน {cook_time} นาที "
f"ประมาณ {calories} kcal ระดับความยาก {difficulty} "
f"พร้อมวิธีทำอย่างละเอียด เสนอ 3 ตัวเลือก คั่นด้วย '🍽️ เมนูที่'")
with st.spinner("กำลังสร้างสรรค์ไอเดียอร่อยๆ..."):
menu_list = process_menus(call_gemini_api(prompt))
if menu_list:
st.subheader("🧑🍳 เมนูแนะนำ:")
cols = st.columns(3)
for i, menu in enumerate(menu_list[:3]):
with cols[i]:
st.markdown(f"🍽️ เมนูที่ {i+1}
", unsafe_allow_html=True)
else:
st.warning("⚠️ ไม่พบเมนูที่ตรงกับเกณฑ์ของคุณ โปรดลองปรับการตั้งค่า")
else:
st.warning("⚠️ กรุณากรอกวัตถุดิบของคุณ")
elif option == "ค้นหาเมนูสำหรับซื้อ":
st.subheader("✨ ค้นหาเมนูที่ใช่สำหรับคุณ")
with st.expander("⚙️ ตั้งค่าการค้นหา", expanded=True):
col1, col2 = st.columns(2)
with col1:
country = st.selectbox("ประเทศ", ["ไทย", "ญี่ปุ่น", "เกาหลีใต้", "สหรัฐอเมริกา", "อังกฤษ", "ฝรั่งเศส", "เยอรมนี"])
category = st.selectbox("ประเภทอาหาร", ["อาหารไทย", "อาหารญี่ปุ่น", "อาหารเกาหลี", "ฟาสต์ฟู้ด", "อาหารสุขภาพ"])
with col2:
taste = st.radio("รสชาติ", ["เผ็ด", "หวาน", "เค็ม", "เปรี้ยว"], horizontal=True)
budget = st.radio("งบประมาณ", ["ต่ำกว่า 100 บาท", "100 - 300 บาท", "มากกว่า 300 บาท"], horizontal=True)
if st.button("🔎 ค้นหาเมนู", use_container_width=True):
prompt = (f"ฉันต้องการซื้ออาหาร {category} รสชาติ {taste} งบประมาณ {budget} ใน {country} "
f"แนะนำ 3 ตัวเลือกเมนู {category} ที่มีขายใน {country} คั่นด้วย '🍽️ เมนูที่'")
with st.spinner("กำลังค้นหาตัวเลือกที่ดีที่สุด..."):
menu_list = process_menus(call_gemini_api(prompt))
if menu_list:
st.subheader("🧑🍳 เมนูแนะนำ:")
cols = st.columns(3)
for i, menu in enumerate(menu_list[:3]):
with cols[i]:
st.markdown(f"🍽️ เมนูที่ {i+1}
", unsafe_allow_html=True)
else:
st.warning("⚠️ ไม่พบเมนู โปรดลองอีกครั้ง")
# --- About Section ---
st.markdown("---")
if st.button("📜 เกี่ยวกับผู้พัฒนา", use_container_width=True):
with st.expander("🤝 พบกับทีมงาน"):
st.markdown("""
- 1. นาย กัลปพฤกษ์ วิเชียรรัตน์ - ชั้น 6/13 เลขที่ 3(คนแบกครับอิๆ)
- 2. นาย ธีราธร มุกดาเพชรรัตน์ - ชั้น 6/13 เลขที่ 13
- 3. นาย อภิวิชญ์ อดุลธรรมวิทย์ - ชั้น 6/13 เลขที่ 28
- 4. นาย ปัณณวิชญ์ หลีกภัย - ชั้น 6/13 เลขที่ 29
""", unsafe_allow_html=True)