Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -2,7 +2,8 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
-
|
|
|
|
| 6 |
import os
|
| 7 |
import traceback
|
| 8 |
|
|
@@ -16,7 +17,7 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# --- ์์ฒญ ๋ชจ๋ธ ---
|
| 20 |
class TarotRequest(BaseModel):
|
| 21 |
cards: List[str]
|
| 22 |
topic: str
|
|
@@ -26,8 +27,10 @@ class TarotRequest(BaseModel):
|
|
| 26 |
class FengShuiRequest(BaseModel):
|
| 27 |
year: int
|
| 28 |
gender: str
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
query: str
|
| 32 |
lang: str = "ํ๊ตญ์ด"
|
| 33 |
|
|
@@ -37,49 +40,67 @@ class SajuRequest(BaseModel):
|
|
| 37 |
day: int
|
| 38 |
hour: int
|
| 39 |
minute: int
|
| 40 |
-
|
| 41 |
query: str
|
| 42 |
lang: str = "ํ๊ตญ์ด"
|
| 43 |
|
| 44 |
-
# ---
|
| 45 |
def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
prompt_text = ""
|
| 48 |
|
| 49 |
if role_type == "tarot":
|
| 50 |
if is_korean:
|
| 51 |
-
prompt_text = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
elif role_type == "fengshui":
|
| 61 |
if is_korean:
|
| 62 |
-
prompt_text = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
else:
|
| 64 |
-
prompt_text =
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
elif role_type == "shaman":
|
| 70 |
if is_korean:
|
| 71 |
-
prompt_text = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
-
prompt_text =
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
else:
|
| 78 |
prompt_text = f"Answer in {lang}."
|
| 79 |
|
| 80 |
return {"system_prompt": prompt_text}
|
| 81 |
|
| 82 |
-
# --- ์๋ํฌ์ธํธ ---
|
| 83 |
|
| 84 |
@app.get("/")
|
| 85 |
def read_root():
|
|
@@ -92,14 +113,21 @@ def get_tarot_deck():
|
|
| 92 |
@app.post("/tarot/read")
|
| 93 |
def read_tarot(req: TarotRequest):
|
| 94 |
try:
|
|
|
|
| 95 |
selected_cards = [card for card in tarot.MAJOR_ARCANA if card["name"] in req.cards]
|
| 96 |
|
| 97 |
persona = get_dynamic_persona("tarot", req.lang)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
card_infos = []
|
|
|
|
|
|
|
| 101 |
for c in selected_cards:
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
card_infos.append(f"- {c['name']}: {desc}")
|
| 104 |
|
| 105 |
card_str = "\n".join(card_infos)
|
|
@@ -118,32 +146,63 @@ def read_tarot(req: TarotRequest):
|
|
| 118 |
print(traceback.format_exc())
|
| 119 |
return {"result": f"Server Error: {str(e)}"}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
try:
|
| 124 |
gender_kor = "๋จ์ฑ" if req.gender in ["Male", "๋จ์ฑ"] else "์ฌ์ฑ"
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
persona = get_dynamic_persona("fengshui", req.lang)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
response = llm.get_ai_response(full_query, persona)
|
|
|
|
| 131 |
return {"data": fs_data, "result": response}
|
| 132 |
except Exception as e:
|
|
|
|
| 133 |
return {"result": f"Error: {str(e)}"}
|
| 134 |
|
| 135 |
-
|
|
|
|
| 136 |
def read_saju(req: SajuRequest):
|
| 137 |
try:
|
| 138 |
import datetime
|
| 139 |
b_date = datetime.date(req.year, req.month, req.day)
|
| 140 |
b_time = datetime.time(req.hour, req.minute)
|
| 141 |
-
|
|
|
|
|
|
|
| 142 |
|
| 143 |
persona = get_dynamic_persona("shaman", req.lang)
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
response = llm.get_ai_response(full_query, persona)
|
| 147 |
return {"data": saju_data, "result": response}
|
| 148 |
except Exception as e:
|
|
|
|
| 149 |
return {"result": f"Error: {str(e)}"}
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
+
# utils ํด๋์ ํด๋น ํ์ผ๋ค์ด ์ค์ ๋ก ์์ด์ผ ํฉ๋๋ค!
|
| 6 |
+
from utils import tarot, calculations, prompts, llm
|
| 7 |
import os
|
| 8 |
import traceback
|
| 9 |
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# --- 1. ์์ฒญ ๋ชจ๋ธ (์ฑ์์ ๋ณด๋ด๋ ๋ณ์๋ช
๊ณผ 100% ์ผ์น์ํด) ---
|
| 21 |
class TarotRequest(BaseModel):
|
| 22 |
cards: List[str]
|
| 23 |
topic: str
|
|
|
|
| 27 |
class FengShuiRequest(BaseModel):
|
| 28 |
year: int
|
| 29 |
gender: str
|
| 30 |
+
doorDir: str # ๐ฅ [์์ ] door_dir -> doorDir (์ฑ์ด๋ ๋ง์ถค)
|
| 31 |
+
headDir: str # ๐ฅ [์์ ] head_dir -> headDir (์ฑ์ด๋ ๋ง์ถค)
|
| 32 |
+
address: str # ๐ฅ [์ถ๊ฐ] ์ฑ์์ address ๋ณด๋ด๋๊น ๋ฐ์์ผ ํจ
|
| 33 |
+
familyInfo: str # ๐ฅ [์ถ๊ฐ] ์ฑ์์ familyInfo ๋ณด๋ด๋๊น ๋ฐ์์ผ ํจ
|
| 34 |
query: str
|
| 35 |
lang: str = "ํ๊ตญ์ด"
|
| 36 |
|
|
|
|
| 40 |
day: int
|
| 41 |
hour: int
|
| 42 |
minute: int
|
| 43 |
+
calendarType: str # ๐ฅ [์์ ] calendar_type -> calendarType (์ฑ์ด๋ ๋ง์ถค)
|
| 44 |
query: str
|
| 45 |
lang: str = "ํ๊ตญ์ด"
|
| 46 |
|
| 47 |
+
# --- 2. ๋์ ํ๋ฅด์๋ ์์ฑ๊ธฐ (๊ฐ๋ ฅํ ์์ด ๋ชจ๋ ์ ์ฉ) ---
|
| 48 |
def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
| 49 |
+
# ํ๊ตญ์ด ๊ฐ์ง ๋ก์ง
|
| 50 |
+
is_korean = "ํ๊ตญ" in lang or "Korean" in lang or "ko" == lang
|
| 51 |
+
|
| 52 |
prompt_text = ""
|
| 53 |
|
| 54 |
if role_type == "tarot":
|
| 55 |
if is_korean:
|
| 56 |
+
prompt_text = """
|
| 57 |
+
๋น์ ์ ์ ๋น๋ก์ด ํ๋ก ๋ฆฌ๋ '์๋ฐ๋ฆฌ'์
๋๋ค.
|
| 58 |
+
์ฌ์ฉ์์ ๊ณ ๋ฏผ์ ๋ํด ๋ฐ๋ปํ๊ณ ์ง๊ด์ ์ธ ํต์ฐฐ๋ ฅ์ ์ ๊ณตํ์ธ์.
|
| 59 |
+
๋งํฌ: ๋ถ๋๋ฝ๊ณ ์ ๋น๋ก์ด ์กด๋๋ง (ํด์์ฒด).
|
| 60 |
+
"""
|
| 61 |
else:
|
| 62 |
+
prompt_text = """
|
| 63 |
+
You are 'Emily', a mysterious Tarot Reader.
|
| 64 |
+
CRITICAL: Even if the user input contains Korean, you must ANSWER STRICTLY IN ENGLISH.
|
| 65 |
+
Do not say "I will translate". Just provide the reading directly.
|
| 66 |
+
Tone: Mystical, soft, empathetic.
|
| 67 |
+
"""
|
| 68 |
|
| 69 |
elif role_type == "fengshui":
|
| 70 |
if is_korean:
|
| 71 |
+
prompt_text = """
|
| 72 |
+
๋น์ ์ 30๋
๊ฒฝ๋ ฅ์ ํ์์ง๋ฆฌ ๋๊ฐ(Master)์
๋๋ค.
|
| 73 |
+
์์์คํ๊ณผ ๋ฐฉ์ํ์ ๊ธฐ๋ฐ์ผ๋ก ์ ๋ฌธ์ ์ธ ์กฐ์ธ์ ์ ๊ณตํ์ธ์.
|
| 74 |
+
๋งํฌ: ์ ๋ฌธ์ ์ด๊ณ ์ ๋ขฐ๊ฐ ์๋ ์ด์กฐ.
|
| 75 |
+
"""
|
| 76 |
else:
|
| 77 |
+
prompt_text = """
|
| 78 |
+
You are a world-renowned Feng Shui Master.
|
| 79 |
+
CRITICAL: Even if the user input contains Korean, you must ANSWER STRICTLY IN ENGLISH.
|
| 80 |
+
Do not mention translation. Speak with authority as a Master.
|
| 81 |
+
Tone: Professional, authoritative, wise.
|
| 82 |
+
"""
|
| 83 |
|
| 84 |
+
elif role_type == "shaman": # ๐ฅ role_type ํต์ผ
|
| 85 |
if is_korean:
|
| 86 |
+
prompt_text = """
|
| 87 |
+
๋น์ ์ ์ ํต๋ฐฉํตํ ๋ฌด๋น '์ฒ๋ช
'์
๋๋ค.
|
| 88 |
+
๊ฑฐ์นจ์๋ ๋งํฌ๋ก ์ด๋ช
์ ๊ฟฐ๋ซ์ด ๋ณด๋ฉฐ ์กฐ์ธํ์ธ์.
|
| 89 |
+
๋งํฌ: ์นด๋ฆฌ์ค๋ง ์๋ ๋ฐ๋ง (~ํด๋ผ, ~๊ตฌ๋, ~๋ค).
|
| 90 |
+
"""
|
| 91 |
else:
|
| 92 |
+
prompt_text = """
|
| 93 |
+
You are 'Shaman Cheon-Myeong'. You have spiritual powers.
|
| 94 |
+
CRITICAL: Even if the user input contains Korean, you must ANSWER STRICTLY IN ENGLISH.
|
| 95 |
+
Do not say "I will translate". Speak directly to the user's soul.
|
| 96 |
+
Tone: Mystical, Charismatic, slightly archaic/shamanic style.
|
| 97 |
+
"""
|
| 98 |
else:
|
| 99 |
prompt_text = f"Answer in {lang}."
|
| 100 |
|
| 101 |
return {"system_prompt": prompt_text}
|
| 102 |
|
| 103 |
+
# --- 3. ์๋ํฌ์ธํธ (์ฑ ์ฃผ์์ 100% ์ผ์น์ํด) ---
|
| 104 |
|
| 105 |
@app.get("/")
|
| 106 |
def read_root():
|
|
|
|
| 113 |
@app.post("/tarot/read")
|
| 114 |
def read_tarot(req: TarotRequest):
|
| 115 |
try:
|
| 116 |
+
# DB์์ ์นด๋ ์ ๋ณด ์ฐพ๊ธฐ (์ด๋ฆ ๋งค์นญ)
|
| 117 |
selected_cards = [card for card in tarot.MAJOR_ARCANA if card["name"] in req.cards]
|
| 118 |
|
| 119 |
persona = get_dynamic_persona("tarot", req.lang)
|
| 120 |
|
| 121 |
+
# ์นด๋ ์ค๋ช
์ถ์ถ (์ธ์ด๋ณ)
|
| 122 |
card_infos = []
|
| 123 |
+
is_kor = "ํ๊ตญ" in req.lang or "Korean" in req.lang
|
| 124 |
+
|
| 125 |
for c in selected_cards:
|
| 126 |
+
# ํ๊ตญ์ด๋ฉด desc_kor ์ฐ์ , ์๋๋ฉด desc_eng ์ฐ์
|
| 127 |
+
if is_kor:
|
| 128 |
+
desc = c.get('desc_kor') or c.get('desc') or "์ค๋ช
์์"
|
| 129 |
+
else:
|
| 130 |
+
desc = c.get('desc_eng') or c.get('desc') or "No description"
|
| 131 |
card_infos.append(f"- {c['name']}: {desc}")
|
| 132 |
|
| 133 |
card_str = "\n".join(card_infos)
|
|
|
|
| 146 |
print(traceback.format_exc())
|
| 147 |
return {"result": f"Server Error: {str(e)}"}
|
| 148 |
|
| 149 |
+
# ๐ฅ [์์ ] ์ฃผ์: /fengshui/analyze -> /fengshui/read (์ฑ์ด๋ ๋ง์ถค)
|
| 150 |
+
@app.post("/fengshui/read")
|
| 151 |
+
def read_fengshui(req: FengShuiRequest):
|
| 152 |
try:
|
| 153 |
gender_kor = "๋จ์ฑ" if req.gender in ["Male", "๋จ์ฑ"] else "์ฌ์ฑ"
|
| 154 |
+
# ๊ณ์ฐ ๋ก์ง ํธ์ถ (utils/calculations.py์ ํด๋น ํจ์๊ฐ ์์ด์ผ ํจ)
|
| 155 |
+
fs_data = calculations.get_fengshui_advanced(req.year, gender_kor, req.doorDir, req.headDir)
|
| 156 |
|
| 157 |
persona = get_dynamic_persona("fengshui", req.lang)
|
| 158 |
+
|
| 159 |
+
# AI์๊ฒ ๋์ง ์ ๋ณด ์กฐํฉ
|
| 160 |
+
full_query = f"""
|
| 161 |
+
[User Info]
|
| 162 |
+
- Birth Year: {req.year}
|
| 163 |
+
- Gender: {req.gender}
|
| 164 |
+
- Front Door: {req.doorDir}
|
| 165 |
+
- Head Direction: {req.headDir}
|
| 166 |
+
- Address: {req.address}
|
| 167 |
+
- Family: {req.familyInfo}
|
| 168 |
+
|
| 169 |
+
[FengShui Calculation Data]
|
| 170 |
+
{fs_data}
|
| 171 |
+
|
| 172 |
+
[User Query]
|
| 173 |
+
{req.query}
|
| 174 |
+
"""
|
| 175 |
|
| 176 |
response = llm.get_ai_response(full_query, persona)
|
| 177 |
+
# ์ฑ์ JSON์์ "result" ํค๋ฅผ ์ฐพ์ผ๋ฏ๋ก result์ ๋ด์์ค
|
| 178 |
return {"data": fs_data, "result": response}
|
| 179 |
except Exception as e:
|
| 180 |
+
print(traceback.format_exc())
|
| 181 |
return {"result": f"Error: {str(e)}"}
|
| 182 |
|
| 183 |
+
# ๐ฅ [์์ ] ์ฃผ์: /shaman/read -> /saju/read (์ฑ์ด๋ ๋ง์ถค)
|
| 184 |
+
@app.post("/saju/read")
|
| 185 |
def read_saju(req: SajuRequest):
|
| 186 |
try:
|
| 187 |
import datetime
|
| 188 |
b_date = datetime.date(req.year, req.month, req.day)
|
| 189 |
b_time = datetime.time(req.hour, req.minute)
|
| 190 |
+
|
| 191 |
+
# ๊ณ์ฐ ๋ก์ง ํธ์ถ
|
| 192 |
+
saju_data = calculations.get_ganji_full(b_date, b_time, req.calendarType)
|
| 193 |
|
| 194 |
persona = get_dynamic_persona("shaman", req.lang)
|
| 195 |
+
|
| 196 |
+
full_query = f"""
|
| 197 |
+
[Saju/Ganji Data]
|
| 198 |
+
{saju_data}
|
| 199 |
+
|
| 200 |
+
[User Query]
|
| 201 |
+
{req.query}
|
| 202 |
+
"""
|
| 203 |
|
| 204 |
response = llm.get_ai_response(full_query, persona)
|
| 205 |
return {"data": saju_data, "result": response}
|
| 206 |
except Exception as e:
|
| 207 |
+
print(traceback.format_exc())
|
| 208 |
return {"result": f"Error: {str(e)}"}
|