benjamin5607 commited on
Commit
81678dc
ยท
verified ยท
1 Parent(s): 78de86a

Update backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +86 -234
backend/main.py CHANGED
@@ -1,247 +1,99 @@
1
- import streamlit as st
2
- import time
3
- import datetime
4
- from utils import tarot, prompts, llm, calculations, lang
 
5
 
6
- # --- [1. ์•ฑ ์„ค์ •] ---
7
- st.set_page_config(page_title="Mystic AI Pantheon", page_icon="๐Ÿ”ฎ", layout="wide")
8
 
9
- # --- [2. ๋น„์ฃผ์–ผ & ์• ๋‹ˆ๋ฉ”์ด์…˜ ์—”์ง„ (CSS Hacking)] ---
10
- def apply_visuals(mode):
11
- # 1. ๊ณตํ†ต ํฐํŠธ
12
- st.markdown("""
13
- <style>
14
- @import url('https://fonts.googleapis.com/css2?family=Nanum+Myeongjo:wght@700&family=Cinzel:wght@700&display=swap');
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
- # --- [3. ์ƒํƒœ ๊ด€๋ฆฌ] ---
74
- if "tarot_stage" not in st.session_state: st.session_state.tarot_stage = "input"
75
- if "picked_indices" not in st.session_state: st.session_state.picked_indices = []
76
- if "picked_cards" not in st.session_state: st.session_state.picked_cards = []
 
 
 
77
 
78
- # --- [4. UI ๊ตฌ์„ฑ] ---
79
- st.sidebar.markdown("### ๐ŸŒ Language")
80
- current_lang = st.sidebar.selectbox("Language", ["ํ•œ๊ตญ์–ด", "English", "ไธญๆ–‡", "ๆ—ฅๆœฌ่ชž"], label_visibility="collapsed")
81
- TXT = lang.STRINGS[current_lang]
 
 
 
 
 
82
 
83
- st.sidebar.title(TXT["sidebar_title"])
84
- selected_mode = st.sidebar.radio(TXT["select_persona"], ("tarot", "fengshui", "shaman"), format_func=lambda x: TXT["personas"][x])
85
 
86
- apply_visuals(selected_mode)
87
- current_persona = prompts.get_persona(selected_mode)
88
- st.sidebar.info(TXT["desc"][selected_mode])
89
 
90
- st.title(f"{TXT['personas'][selected_mode]}")
91
- st.markdown("---")
 
 
92
 
93
- # =========================================================
94
- # ๐Ÿƒ ํƒ€๋กœ ๋ชจ๋“œ: ๋ฆฌ์–ผํ•œ ์ธํ„ฐ๋ž™์…˜ & ๋’ค์ง‘๊ธฐ ํšจ๊ณผ
95
- # =========================================================
96
- if selected_mode == "tarot":
 
97
 
98
- # [Phase 1] ์ฃผ์ œ & ๊ณ ๋ฏผ ์ž…๋ ฅ
99
- if st.session_state.tarot_stage == "input":
100
- col1, col2 = st.columns([1, 2])
101
- with col1:
102
- st.image("https://placehold.co/400x600/1a0b2e/d4af37/png?text=Tarot+Deck", caption="The Mystic Deck")
103
- with col2:
104
- with st.form("tarot_form"):
105
- st.subheader("๐ŸŒ™ ์˜ค๋Š˜ ๋ฐค, ๋ณ„๋“ค์—๊ฒŒ ๋ฌด์—‡์„ ๋ฌผ์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?")
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
- # [Phase 4] AI ํ•ด์„
179
- elif st.session_state.tarot_stage == "reading":
180
- # ์ด๋ฏธ ๋’ค์ง‘ํžŒ ์นด๋“œ ๋ณด์—ฌ์ฃผ๊ธฐ
181
- cols = st.columns(3)
182
- positions = ["๊ณผ๊ฑฐ", "ํ˜„์žฌ", "๋ฏธ๋ž˜"]
183
- for idx, card in enumerate(st.session_state.picked_cards):
184
- with cols[idx]:
185
- st.image(tarot.get_card_image(card["name"]), caption=f"{positions[idx]}: {card['name']}")
186
-
187
- st.divider()
188
-
189
- if "ai_response" not in st.session_state:
190
- with st.spinner(TXT["analyzing"]):
191
- st.session_state.ai_response = llm.get_ai_response(
192
- st.session_state.user_query, current_persona, st.session_state.picked_cards, user_lang=current_lang
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
- elif selected_mode == "fengshui":
213
- with st.expander("โ„น๏ธ Input Data", expanded=True):
214
- c1, c2 = st.columns(2)
215
- with c1:
216
- birth_year = st.number_input(TXT["birth_year"], 1950, 2025, 1990)
217
- gender = "Male" if st.radio(TXT["gender"], ["Male", "Female"]) == "Male" else "Female"
218
- with c2:
219
- door_dir = st.selectbox(TXT["door_dir"], ["N", "NE", "E", "SE", "S", "SW", "W", "NW"])
220
- head_dir = st.selectbox(TXT["head_dir"], ["N", "NE", "E", "SE", "S", "SW", "W", "NW"])
 
 
 
 
 
 
221
 
222
- if prompt := st.chat_input(TXT["input_label"]):
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