Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -2,7 +2,7 @@ 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 |
-
# utils ํด๋
|
| 6 |
from utils import tarot, calculations, prompts, llm
|
| 7 |
import os
|
| 8 |
import traceback
|
|
@@ -17,7 +17,7 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# --- 1. ์์ฒญ ๋ชจ๋ธ
|
| 21 |
class TarotRequest(BaseModel):
|
| 22 |
cards: List[str]
|
| 23 |
topic: str
|
|
@@ -27,8 +27,8 @@ class TarotRequest(BaseModel):
|
|
| 27 |
class FengShuiRequest(BaseModel):
|
| 28 |
year: int
|
| 29 |
gender: str
|
| 30 |
-
door_dir: str
|
| 31 |
-
head_dir: str
|
| 32 |
query: str
|
| 33 |
lang: str = "ํ๊ตญ์ด"
|
| 34 |
|
|
@@ -38,14 +38,18 @@ class SajuRequest(BaseModel):
|
|
| 38 |
day: int
|
| 39 |
hour: int
|
| 40 |
minute: int
|
| 41 |
-
calendar_type: str
|
| 42 |
query: str
|
| 43 |
lang: str = "ํ๊ตญ์ด"
|
| 44 |
|
| 45 |
-
# --- 2. ํ๋ฅด์๋ ์์ฑ๊ธฐ (๐ฅ
|
| 46 |
def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
prompt_text = ""
|
| 50 |
|
| 51 |
if role_type == "tarot":
|
|
@@ -56,11 +60,11 @@ def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
|
| 56 |
๋งํฌ: ๋ถ๋๋ฝ๊ณ ์ ๋น๋ก์ด ์กด๋๋ง (ํด์์ฒด).
|
| 57 |
"""
|
| 58 |
else:
|
| 59 |
-
# ์์ด
|
| 60 |
-
prompt_text = """
|
| 61 |
You are 'Emily', a mysterious Tarot Reader.
|
| 62 |
-
CRITICAL INSTRUCTION:
|
| 63 |
-
|
| 64 |
Tone: Mystical, soft, empathetic.
|
| 65 |
"""
|
| 66 |
|
|
@@ -71,11 +75,11 @@ def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
|
| 71 |
๋
ผ๋ฆฌ์ ์ด๊ณ ์ ๋ฌธ์ ์ผ๋ก ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์.
|
| 72 |
"""
|
| 73 |
else:
|
| 74 |
-
#
|
| 75 |
-
prompt_text = """
|
| 76 |
You are a Feng Shui Master.
|
| 77 |
-
CRITICAL INSTRUCTION:
|
| 78 |
-
Do NOT mention translation. Speak with authority as a Master.
|
| 79 |
Tone: Professional, logical, wise.
|
| 80 |
"""
|
| 81 |
|
|
@@ -86,19 +90,19 @@ def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
|
| 86 |
๊ฑฐ์น ๋ฐ๋ง(~ํด๋ผ, ~๊ตฌ๋)์ ์ฐ๋ฉฐ ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์.
|
| 87 |
"""
|
| 88 |
else:
|
| 89 |
-
#
|
| 90 |
-
prompt_text = """
|
| 91 |
You are 'Shaman Cheon-Myeong'.
|
| 92 |
-
CRITICAL INSTRUCTION:
|
| 93 |
-
|
| 94 |
Tone: Mystical, Charismatic, slightly archaic style.
|
| 95 |
"""
|
| 96 |
else:
|
| 97 |
-
prompt_text = f"Answer in {
|
| 98 |
|
| 99 |
return {"system_prompt": prompt_text}
|
| 100 |
|
| 101 |
-
# --- 3. ์๋ํฌ์ธํธ
|
| 102 |
|
| 103 |
@app.get("/")
|
| 104 |
def read_root():
|
|
@@ -111,15 +115,15 @@ def get_tarot_deck():
|
|
| 111 |
@app.post("/tarot/read")
|
| 112 |
def read_tarot(req: TarotRequest):
|
| 113 |
try:
|
| 114 |
-
# DB ๋งค์นญ
|
| 115 |
selected_cards = [card for card in tarot.MAJOR_ARCANA if card["name"] in req.cards]
|
| 116 |
-
|
| 117 |
persona = get_dynamic_persona("tarot", req.lang)
|
| 118 |
|
| 119 |
-
# ์นด๋ ์ค๋ช
(๋จ์ํ)
|
| 120 |
card_infos = []
|
|
|
|
|
|
|
| 121 |
for c in selected_cards:
|
| 122 |
-
desc = c.get('
|
|
|
|
| 123 |
card_infos.append(f"- {c['name']}: {desc}")
|
| 124 |
|
| 125 |
card_str = "\n".join(card_infos)
|
|
@@ -138,12 +142,10 @@ def read_tarot(req: TarotRequest):
|
|
| 138 |
print(traceback.format_exc())
|
| 139 |
return {"result": f"Server Error: {str(e)}"}
|
| 140 |
|
| 141 |
-
# ๐ฅ [๋ณต๊ตฌ] ์ฃผ์: /fengshui/analyze (์ฑ ์์ฒญ ์ฃผ์)
|
| 142 |
@app.post("/fengshui/analyze")
|
| 143 |
def analyze_fengshui(req: FengShuiRequest):
|
| 144 |
try:
|
| 145 |
gender_kor = "๋จ์ฑ" if req.gender in ["Male", "๋จ์ฑ"] else "์ฌ์ฑ"
|
| 146 |
-
# ํ๋ ์ฝ๋ ๋ณ์๋ช
door_dir, head_dir ๊ทธ๋๋ก ์ฌ์ฉ
|
| 147 |
fs_data = calculations.get_fengshui_advanced(req.year, gender_kor, req.door_dir, req.head_dir)
|
| 148 |
|
| 149 |
persona = get_dynamic_persona("fengshui", req.lang)
|
|
@@ -154,14 +156,12 @@ def analyze_fengshui(req: FengShuiRequest):
|
|
| 154 |
except Exception as e:
|
| 155 |
return {"result": f"Error: {str(e)}"}
|
| 156 |
|
| 157 |
-
# ๐ฅ [๋ณต๊ตฌ] ์ฃผ์: /shaman/read (์ฑ ์์ฒญ ์ฃผ์ ํ์ธ ํ์: ํ๋ ์ฝ๋์ shaman/read๋ก ๋์ด์์์)
|
| 158 |
@app.post("/shaman/read")
|
| 159 |
def read_saju(req: SajuRequest):
|
| 160 |
try:
|
| 161 |
import datetime
|
| 162 |
b_date = datetime.date(req.year, req.month, req.day)
|
| 163 |
b_time = datetime.time(req.hour, req.minute)
|
| 164 |
-
# ํ๋ ์ฝ๋ ๋ณ์๋ช
calendar_type ๊ทธ๋๋ก ์ฌ์ฉ
|
| 165 |
saju_data = calculations.get_ganji_full(b_date, b_time, req.calendar_type)
|
| 166 |
|
| 167 |
persona = get_dynamic_persona("shaman", req.lang)
|
|
|
|
| 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
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# --- 1. ์์ฒญ ๋ชจ๋ธ ---
|
| 21 |
class TarotRequest(BaseModel):
|
| 22 |
cards: List[str]
|
| 23 |
topic: str
|
|
|
|
| 27 |
class FengShuiRequest(BaseModel):
|
| 28 |
year: int
|
| 29 |
gender: str
|
| 30 |
+
door_dir: str
|
| 31 |
+
head_dir: str
|
| 32 |
query: str
|
| 33 |
lang: str = "ํ๊ตญ์ด"
|
| 34 |
|
|
|
|
| 38 |
day: int
|
| 39 |
hour: int
|
| 40 |
minute: int
|
| 41 |
+
calendar_type: str
|
| 42 |
query: str
|
| 43 |
lang: str = "ํ๊ตญ์ด"
|
| 44 |
|
| 45 |
+
# --- 2. ํ๋ฅด์๋ ์์ฑ๊ธฐ (๐ฅ ๋ค๊ตญ์ด ์๋ฒฝ ์ง์ ์์ !) ---
|
| 46 |
def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
|
| 47 |
+
# ์ธ์ด๊ฐ์ด ๋น์ด์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์์ด
|
| 48 |
+
target_lang = lang if lang else "English"
|
| 49 |
+
|
| 50 |
+
# ํ๊ตญ์ด ํนํ ์ฒ๋ฆฌ (ํ๊ตญ์ด๋ ๋์์ค๊ฐ ์ค์ํด์ ๋ฐ๋ก ๋บ)
|
| 51 |
+
is_korean = any(k in target_lang for k in ["ํ๊ตญ", "Korean", "ko"])
|
| 52 |
+
|
| 53 |
prompt_text = ""
|
| 54 |
|
| 55 |
if role_type == "tarot":
|
|
|
|
| 60 |
๋งํฌ: ๋ถ๋๋ฝ๊ณ ์ ๋น๋ก์ด ์กด๋๋ง (ํด์์ฒด).
|
| 61 |
"""
|
| 62 |
else:
|
| 63 |
+
# ๐ฅ [์์ ] ์์ด๊ฐ ์๋๋ผ {target_lang}์ผ๋ก ๋ต๋ณํ๋ผ๊ณ ์ง์
|
| 64 |
+
prompt_text = f"""
|
| 65 |
You are 'Emily', a mysterious Tarot Reader.
|
| 66 |
+
CRITICAL INSTRUCTION: You must ANSWER STRICTLY IN {target_lang}.
|
| 67 |
+
Even if the user input is in another language, TRANSLATE your reading to {target_lang}.
|
| 68 |
Tone: Mystical, soft, empathetic.
|
| 69 |
"""
|
| 70 |
|
|
|
|
| 75 |
๋
ผ๋ฆฌ์ ์ด๊ณ ์ ๋ฌธ์ ์ผ๋ก ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์.
|
| 76 |
"""
|
| 77 |
else:
|
| 78 |
+
# ๐ฅ [์์ ] {target_lang} ์ฌ์ฉ
|
| 79 |
+
prompt_text = f"""
|
| 80 |
You are a Feng Shui Master.
|
| 81 |
+
CRITICAL INSTRUCTION: You must ANSWER STRICTLY IN {target_lang}.
|
| 82 |
+
Do NOT mention translation. Speak with authority as a Master in {target_lang}.
|
| 83 |
Tone: Professional, logical, wise.
|
| 84 |
"""
|
| 85 |
|
|
|
|
| 90 |
๊ฑฐ์น ๋ฐ๋ง(~ํด๋ผ, ~๊ตฌ๋)์ ์ฐ๋ฉฐ ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์.
|
| 91 |
"""
|
| 92 |
else:
|
| 93 |
+
# ๐ฅ [์์ ] {target_lang} ์ฌ์ฉ
|
| 94 |
+
prompt_text = f"""
|
| 95 |
You are 'Shaman Cheon-Myeong'.
|
| 96 |
+
CRITICAL INSTRUCTION: You must ANSWER STRICTLY IN {target_lang}.
|
| 97 |
+
Speak directly to the user's soul in {target_lang}.
|
| 98 |
Tone: Mystical, Charismatic, slightly archaic style.
|
| 99 |
"""
|
| 100 |
else:
|
| 101 |
+
prompt_text = f"Answer strictly in {target_lang}."
|
| 102 |
|
| 103 |
return {"system_prompt": prompt_text}
|
| 104 |
|
| 105 |
+
# --- 3. ์๋ํฌ์ธํธ ---
|
| 106 |
|
| 107 |
@app.get("/")
|
| 108 |
def read_root():
|
|
|
|
| 115 |
@app.post("/tarot/read")
|
| 116 |
def read_tarot(req: TarotRequest):
|
| 117 |
try:
|
|
|
|
| 118 |
selected_cards = [card for card in tarot.MAJOR_ARCANA if card["name"] in req.cards]
|
|
|
|
| 119 |
persona = get_dynamic_persona("tarot", req.lang)
|
| 120 |
|
|
|
|
| 121 |
card_infos = []
|
| 122 |
+
# ์นด๋ ์ค๋ช
์ ํ๊ตญ์ด ์๋๋ฉด ์ผ๋จ ์์ด๋ก ๋๊ฐ (์ถํ ๋ฒ ํธ๋จ์ด DB ์์ผ๋ฉด ์ฌ๊ธฐ๋ ๋ถ๊ธฐ ๊ฐ๋ฅ)
|
| 123 |
+
is_kor = "ํ๊ตญ" in req.lang or "Korean" in req.lang
|
| 124 |
for c in selected_cards:
|
| 125 |
+
desc = c.get('desc_kor') if is_kor else c.get('desc_eng') or c.get('desc')
|
| 126 |
+
if not desc: desc = "No description"
|
| 127 |
card_infos.append(f"- {c['name']}: {desc}")
|
| 128 |
|
| 129 |
card_str = "\n".join(card_infos)
|
|
|
|
| 142 |
print(traceback.format_exc())
|
| 143 |
return {"result": f"Server Error: {str(e)}"}
|
| 144 |
|
|
|
|
| 145 |
@app.post("/fengshui/analyze")
|
| 146 |
def analyze_fengshui(req: FengShuiRequest):
|
| 147 |
try:
|
| 148 |
gender_kor = "๋จ์ฑ" if req.gender in ["Male", "๋จ์ฑ"] else "์ฌ์ฑ"
|
|
|
|
| 149 |
fs_data = calculations.get_fengshui_advanced(req.year, gender_kor, req.door_dir, req.head_dir)
|
| 150 |
|
| 151 |
persona = get_dynamic_persona("fengshui", req.lang)
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
return {"result": f"Error: {str(e)}"}
|
| 158 |
|
|
|
|
| 159 |
@app.post("/shaman/read")
|
| 160 |
def read_saju(req: SajuRequest):
|
| 161 |
try:
|
| 162 |
import datetime
|
| 163 |
b_date = datetime.date(req.year, req.month, req.day)
|
| 164 |
b_time = datetime.time(req.hour, req.minute)
|
|
|
|
| 165 |
saju_data = calculations.get_ganji_full(b_date, b_time, req.calendar_type)
|
| 166 |
|
| 167 |
persona = get_dynamic_persona("shaman", req.lang)
|