Spaces:
Sleeping
Sleeping
Update backend/main.py
Browse files- backend/main.py +86 -234
backend/main.py
CHANGED
|
@@ -1,247 +1,99 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from utils import tarot,
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
st.set_page_config(page_title="Mystic AI Pantheon", page_icon="๐ฎ", layout="wide")
|
| 8 |
|
| 9 |
-
# --- [
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
/* ์ ์ฒด ์ฑ ๋ฐฐ๊ฒฝ ์ค์ */
|
| 17 |
-
.stApp {
|
| 18 |
-
background-color: #0F0F0F;
|
| 19 |
-
font-family: 'Nanum Myeongjo', serif;
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
/* ๋ฒํผ(์นด๋) ๋์์ธ ์ปค์คํ
- ํต์ฌ โญ */
|
| 23 |
-
div[data-testid="stGrid"] button {
|
| 24 |
-
height: 140px; /* ์นด๋ ๋น์จ (์ธ๋ก๋ก ๊ธธ๊ฒ) */
|
| 25 |
-
background-color: #1e1e2e;
|
| 26 |
-
/* ์นด๋ ๋ท๋ฉด ํจํด (CSS ํจํด) */
|
| 27 |
-
background-image: repeating-linear-gradient(45deg, #2b2b40 25%, transparent 25%, transparent 75%, #2b2b40 75%, #2b2b40),
|
| 28 |
-
repeating-linear-gradient(45deg, #2b2b40 25%, #1e1e2e 25%, #1e1e2e 75%, #2b2b40 75%, #2b2b40);
|
| 29 |
-
background-position: 0 0, 10px 10px;
|
| 30 |
-
background-size: 20px 20px;
|
| 31 |
-
border: 2px solid #d4af37; /* ๊ธํ
*/
|
| 32 |
-
border-radius: 10px;
|
| 33 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.5);
|
| 34 |
-
transition: transform 0.2s, box-shadow 0.2s;
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
/* ๋ง์ฐ์ค ์ฌ๋ ธ์ ๋ ์นด๋ ๋ถ ๋จ๋ ํจ๊ณผ */
|
| 38 |
-
div[data-testid="stGrid"] button:hover {
|
| 39 |
-
transform: translateY(-10px) scale(1.05);
|
| 40 |
-
box-shadow: 0 10px 20px rgba(212, 175, 55, 0.4); /* ๊ธ์ ๊ด์ฑ */
|
| 41 |
-
border-color: #fff;
|
| 42 |
-
z-index: 10;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
/* ์ ํ๋ ์นด๋ (๋นํ์ฑ) ์คํ์ผ */
|
| 46 |
-
div[data-testid="stGrid"] button:disabled {
|
| 47 |
-
background-image: none;
|
| 48 |
-
background-color: #4b0082; /* ์ ํ๋๋ฉด ๋ณด๋ผ์์ผ๋ก ๋ณํจ */
|
| 49 |
-
border: 2px solid #00ff00;
|
| 50 |
-
opacity: 0.8;
|
| 51 |
-
transform: scale(0.95);
|
| 52 |
-
}
|
| 53 |
-
</style>
|
| 54 |
-
""", unsafe_allow_html=True)
|
| 55 |
-
|
| 56 |
-
# ๋ชจ๋๋ณ ๋ฐฐ๊ฒฝ ๋ฏธ์ธ ์กฐ์
|
| 57 |
-
themes = {
|
| 58 |
-
"tarot": """
|
| 59 |
-
<style>
|
| 60 |
-
.stApp {
|
| 61 |
-
/* ๋ฒจ๋ฒณ ํ
์ด๋ธ ๋๋ */
|
| 62 |
-
background: radial-gradient(circle, #240b36 0%, #100517 100%);
|
| 63 |
-
color: #e0d4fc;
|
| 64 |
-
}
|
| 65 |
-
h1 { color: #d4af37 !important; text-shadow: 0 0 15px #d4af37; font-family: 'Cinzel', serif; }
|
| 66 |
-
</style>
|
| 67 |
-
""",
|
| 68 |
-
"fengshui": """<style>.stApp { background: #fdfbf7; color: #3e2723; }</style>""",
|
| 69 |
-
"shaman": """<style>.stApp { background: #1a0000; color: #ffaaaa; }</style>"""
|
| 70 |
-
}
|
| 71 |
-
st.markdown(themes.get(mode, themes["tarot"]), unsafe_allow_html=True)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
selected_mode = st.sidebar.radio(TXT["select_persona"], ("tarot", "fengshui", "shaman"), format_func=lambda x: TXT["personas"][x])
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
topic = st.selectbox(TXT["tarot_topic"], ["Love (์ฐ์ )", "Money (๊ธ์ )", "Career (์ง์ฅ)", "Destiny (์ด๋ช
)"])
|
| 107 |
-
query = st.text_area(TXT["input_label"], height=150, placeholder=TXT["tarot_placeholder"])
|
| 108 |
-
|
| 109 |
-
# ๋ฒํผ๋ ์คํ์ผ๋ง
|
| 110 |
-
submit = st.form_submit_button(TXT["btn_submit"], type="primary", use_container_width=True)
|
| 111 |
-
|
| 112 |
-
if submit and query:
|
| 113 |
-
st.session_state.user_query = f"[{topic}] {query}"
|
| 114 |
-
st.session_state.tarot_stage = "picking"
|
| 115 |
-
st.session_state.picked_indices = []
|
| 116 |
-
st.rerun()
|
| 117 |
-
|
| 118 |
-
# [Phase 2] ์นด๋ ์ ํ (ํ
์ด๋ธ์ ์ซ ๊น๋ฆฐ ์นด๋๋ค)
|
| 119 |
-
elif st.session_state.tarot_stage == "picking":
|
| 120 |
-
st.markdown(f"<h3 style='text-align: center;'>{TXT['tarot_pick_inst']}</h3>", unsafe_allow_html=True)
|
| 121 |
-
|
| 122 |
-
# ์ ํ ํํฉ ๋ฐ
|
| 123 |
-
count = len(st.session_state.picked_indices)
|
| 124 |
-
st.progress(count / 3, text=f"Chosen: {count} / 3")
|
| 125 |
-
|
| 126 |
-
# 22์ฅ ์นด๋๋ฅผ 6์ด๋ก ๋ฐฐ์น (CSS๋ก ์นด๋ ๋ท๋ฉด ํจํด ์ ์ฉ๋จ)
|
| 127 |
-
cols = st.columns(6)
|
| 128 |
-
for i in range(22):
|
| 129 |
-
is_picked = i in st.session_state.picked_indices
|
| 130 |
-
# ์นด๋๊ฐ ์ ํ๋๋ฉด 'โ
' ์๋๋ฉด ํฌ๋ช
(CSS๋ฐฐ๊ฒฝ๋ณด์)
|
| 131 |
-
label = "โจ" if is_picked else " "
|
| 132 |
-
|
| 133 |
-
if cols[i % 6].button(label, key=f"card_{i}", disabled=is_picked):
|
| 134 |
-
if count < 3:
|
| 135 |
-
st.session_state.picked_indices.append(i)
|
| 136 |
-
st.rerun()
|
| 137 |
-
|
| 138 |
-
# 3์ฅ ์ ํ ์๋ฃ ์ ๋ฒํผ ๋ฑ์ฅ
|
| 139 |
-
if count >= 3:
|
| 140 |
-
st.markdown("---")
|
| 141 |
-
if st.button("๐ฎ ์ด๋ช
๊ณต๊ฐ (Reveal Destiny)", type="primary", use_container_width=True):
|
| 142 |
-
st.session_state.picked_cards = [tarot.MAJOR_ARCANA[i] for i in st.session_state.picked_indices[:3]]
|
| 143 |
-
st.session_state.tarot_stage = "reveal_animation" # ์ ๋๋ฉ์ด์
๋จ๊ณ๋ก ์ด๋
|
| 144 |
-
st.rerun()
|
| 145 |
-
|
| 146 |
-
# [Phase 3] ๋ค์ง๊ธฐ ์ ๋๋ฉ์ด์
(์ฌ๊ธฐ๊ฐ ์ง์ง!)
|
| 147 |
-
elif st.session_state.tarot_stage == "reveal_animation":
|
| 148 |
-
st.subheader("์ด๋ช
์ ์นด๋๋ฅผ ๋ค์ง์ต๋๋ค...")
|
| 149 |
-
|
| 150 |
-
# ์นด๋ 3์ฅ์ ๋์ ๋น ๊ณต๊ฐ ํ๋ณด
|
| 151 |
-
cols = st.columns(3)
|
| 152 |
-
placeholders = [col.empty() for col in cols]
|
| 153 |
-
|
| 154 |
-
# 1. ์ผ๋จ 3์ฅ ๋ค ๋ท๋ฉด์ผ๋ก ๋ณด์ฌ์ค
|
| 155 |
-
back_card_img = "https://placehold.co/300x500/2d1b4e/d4af37/png?text=?"
|
| 156 |
-
for ph in placeholders:
|
| 157 |
-
ph.image(back_card_img, caption="???")
|
| 158 |
-
|
| 159 |
-
# 2. ํ๋์ฉ ์๊ฐ์ฐจ๋ฅผ ๋๊ณ ์คํ (๊ธด์ฅ๊ฐ ์กฐ์ฑ)
|
| 160 |
-
time.sleep(1) # ๋ธ ๋ค์ด๊ธฐ
|
| 161 |
-
|
| 162 |
-
positions = ["๊ณผ๊ฑฐ (Past)", "ํ์ฌ (Present)", "๋ฏธ๋ (Future)"]
|
| 163 |
-
|
| 164 |
-
for i in range(3):
|
| 165 |
-
time.sleep(1.2) # 1.2์ด ๋๋ ์ด (์ชผ๋ ๋ง)
|
| 166 |
-
card = st.session_state.picked_cards[i]
|
| 167 |
-
# ํ๋ ์ด์คํ๋๋ฅผ ์ค์ ์นด๋ ์ด๋ฏธ์ง๋ก ๊ต์ฒด (Flip ํจ๊ณผ์ฒ๋ผ ๋ณด์)
|
| 168 |
-
placeholders[i].image(
|
| 169 |
-
tarot.get_card_image(card["name"]),
|
| 170 |
-
caption=f"{positions[i]}: {card['name']}"
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
# 3. ๋ค ๋ค์งํ๋ฉด ํด์ ๋จ๊ณ๋ก ์๋ ์ด๋
|
| 174 |
-
time.sleep(1)
|
| 175 |
-
st.session_state.tarot_stage = "reading"
|
| 176 |
-
st.rerun()
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
# ๊ฒฐ๊ณผ ๋ฐ์ค ๋์์ธ
|
| 196 |
-
st.markdown(f"""
|
| 197 |
-
<div style="background-color: rgba(255,255,255,0.1); padding: 20px; border-radius: 10px; border-left: 5px solid gold;">
|
| 198 |
-
<h3 style="margin-top:0;">{TXT['result_title']}</h3>
|
| 199 |
-
<p style="font-size: 1.1em; line-height: 1.6;">{st.session_state.ai_response}</p>
|
| 200 |
-
</div>
|
| 201 |
-
""", unsafe_allow_html=True)
|
| 202 |
-
|
| 203 |
-
st.markdown("<br>", unsafe_allow_html=True)
|
| 204 |
-
if st.button(TXT["retry_btn"], use_container_width=True):
|
| 205 |
-
st.session_state.tarot_stage = "input"
|
| 206 |
-
del st.session_state["ai_response"]
|
| 207 |
-
st.rerun()
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
fs_data = calculations.get_fengshui_advanced(birth_year, gender, door_dir, head_dir)
|
| 224 |
-
full_query = f"[FengShui]: {fs_data}\nQuery: {prompt}"
|
| 225 |
-
with st.chat_message("assistant"):
|
| 226 |
-
with st.spinner(TXT["analyzing"]):
|
| 227 |
-
st.write(llm.get_ai_response(full_query, current_persona, user_lang=current_lang))
|
| 228 |
|
| 229 |
-
#
|
| 230 |
-
# ๐น ์ฌ์ฃผ ๋ชจ๋ (๋์์ธ ์ ์ง)
|
| 231 |
-
# =========================================================
|
| 232 |
-
elif selected_mode == "shaman":
|
| 233 |
-
with st.expander("๐ฏ๏ธ Saju Data", expanded=True):
|
| 234 |
-
st.caption(TXT["shaman_warn"])
|
| 235 |
-
c1, c2 = st.columns(2)
|
| 236 |
-
with c1:
|
| 237 |
-
b_date = st.date_input(TXT["birth_date"], datetime.date(1990, 1, 1))
|
| 238 |
-
cal_type = st.radio(TXT["calendar_type"], ["Solar", "Lunar"])
|
| 239 |
-
with c2:
|
| 240 |
-
b_time = st.time_input(TXT["birth_time"], datetime.time(12, 00))
|
| 241 |
-
|
| 242 |
-
if prompt_input := st.chat_input(TXT["input_label"]):
|
| 243 |
-
saju_data = calculations.get_ganji_full(b_date, b_time, cal_type)
|
| 244 |
-
full_query = f"[Saju]: {saju_data}\nQuery: {prompt_input}"
|
| 245 |
-
with st.chat_message("assistant"):
|
| 246 |
-
with st.spinner(TXT["loading"]):
|
| 247 |
-
st.markdown(llm.get_ai_response(full_query, current_persona, user_lang=current_lang))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Optional, Any
|
| 4 |
+
from utils import tarot, calculations, prompts, llm
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
app = FastAPI(title="AI Pantheon API")
|
|
|
|
| 8 |
|
| 9 |
+
# --- [์์ฒญ ๋ฐ์ดํฐ ๋ชจ๋ธ ์ ์ (ํ๋ฐฐ ์ก์ฅ ์์)] ---
|
| 10 |
+
class TarotRequest(BaseModel):
|
| 11 |
+
cards: List[str] # ๋ฝ์ ์นด๋ ์ด๋ฆ๋ค
|
| 12 |
+
topic: str
|
| 13 |
+
query: str
|
| 14 |
+
lang: str = "ํ๊ตญ์ด"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
class FengShuiRequest(BaseModel):
|
| 17 |
+
year: int
|
| 18 |
+
gender: str
|
| 19 |
+
door_dir: str
|
| 20 |
+
head_dir: str
|
| 21 |
+
query: str
|
| 22 |
+
lang: str = "ํ๊ตญ์ด"
|
| 23 |
|
| 24 |
+
class SajuRequest(BaseModel):
|
| 25 |
+
year: int
|
| 26 |
+
month: int
|
| 27 |
+
day: int
|
| 28 |
+
hour: int
|
| 29 |
+
minute: int
|
| 30 |
+
calendar_type: str # Solar/Lunar
|
| 31 |
+
query: str
|
| 32 |
+
lang: str = "ํ๊ตญ์ด"
|
| 33 |
|
| 34 |
+
# --- [API ์๋ํฌ์ธํธ (์ฐฝ๊ตฌ)] ---
|
|
|
|
| 35 |
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def read_root():
|
| 38 |
+
return {"message": "Welcome to AI Pantheon API Server ๐ฎ"}
|
| 39 |
|
| 40 |
+
@app.get("/tarot/deck")
|
| 41 |
+
def get_tarot_deck():
|
| 42 |
+
"""ํ๋ก ์นด๋ 22์ฅ ๋ฐ์ดํฐ๋ฅผ Flutter์๊ฒ ๋ณด๋ด์ค"""
|
| 43 |
+
return tarot.MAJOR_ARCANA
|
| 44 |
|
| 45 |
+
@app.post("/tarot/read")
|
| 46 |
+
def read_tarot(req: TarotRequest):
|
| 47 |
+
"""ํ๋ก ํด์ ์์ฒญ"""
|
| 48 |
+
# 1. ์นด๋ ์ด๋ฆ์ผ๋ก ์์ธ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
|
| 49 |
+
selected_cards = [card for card in tarot.MAJOR_ARCANA if card["name"] in req.cards]
|
| 50 |
|
| 51 |
+
# 2. ํ๋ฅด์๋ ๋ก๋
|
| 52 |
+
persona = prompts.get_persona("tarot")
|
| 53 |
+
|
| 54 |
+
# 3. AI์๊ฒ ์์ฒญ
|
| 55 |
+
full_query = f"[{req.topic}] {req.query}"
|
| 56 |
+
response = llm.get_ai_response(full_query, persona, selected_cards, req.lang)
|
| 57 |
+
|
| 58 |
+
return {"result": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
@app.post("/fengshui/analyze")
|
| 61 |
+
def analyze_fengshui(req: FengShuiRequest):
|
| 62 |
+
"""ํ์์ง๋ฆฌ ๋ถ์ ์์ฒญ"""
|
| 63 |
+
# 1. ๊ณ์ฐ ๋ก์ง ์คํ
|
| 64 |
+
# ์ฑ๋ณ ๋ณํ (Male/Female -> ๋จ์ฑ/์ฌ์ฑ)
|
| 65 |
+
gender_kor = "๋จ์ฑ" if req.gender in ["Male", "๋จ์ฑ"] else "์ฌ์ฑ"
|
| 66 |
+
|
| 67 |
+
fs_data = calculations.get_fengshui_advanced(req.year, gender_kor, req.door_dir, req.head_dir)
|
| 68 |
+
|
| 69 |
+
# 2. AI ์์ฒญ
|
| 70 |
+
persona = prompts.get_persona("fengshui")
|
| 71 |
+
sys_msg = f"[FengShui Data]: {fs_data}"
|
| 72 |
+
full_query = f"{sys_msg}\nUser Query: {req.query}"
|
| 73 |
+
|
| 74 |
+
response = llm.get_ai_response(full_query, persona, user_lang=req.lang)
|
| 75 |
+
|
| 76 |
+
return {"data": fs_data, "result": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
@app.post("/shaman/read")
|
| 79 |
+
def read_saju(req: SajuRequest):
|
| 80 |
+
"""์ ์ (์ฌ์ฃผ) ์์ฒญ"""
|
| 81 |
+
import datetime
|
| 82 |
+
|
| 83 |
+
# 1. ๋ ์ง ๊ฐ์ฒด ์์ฑ
|
| 84 |
+
b_date = datetime.date(req.year, req.month, req.day)
|
| 85 |
+
b_time = datetime.time(req.hour, req.minute)
|
| 86 |
+
|
| 87 |
+
# 2. ์ฌ์ฃผ ๊ณ์ฐ
|
| 88 |
+
saju_data = calculations.get_ganji_full(b_date, b_time, req.calendar_type)
|
| 89 |
+
|
| 90 |
+
# 3. AI ์์ฒญ
|
| 91 |
+
persona = prompts.get_persona("shaman")
|
| 92 |
+
sys_msg = f"[Saju Data]: {saju_data}"
|
| 93 |
+
full_query = f"{sys_msg}\nUser Worry: {req.query}"
|
| 94 |
+
|
| 95 |
+
response = llm.get_ai_response(full_query, persona, user_lang=req.lang)
|
| 96 |
|
| 97 |
+
return {"data": saju_data, "result": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# ์คํ๋ฒ: uvicorn main:app --reload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|