benjamin5607 commited on
Commit
fa6dca9
ยท
verified ยท
1 Parent(s): 288a085

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +94 -35
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
- from utils import tarot, calculations, prompts, llm
 
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
- door_dir: str
30
- head_dir: str
 
 
31
  query: str
32
  lang: str = "ํ•œ๊ตญ์–ด"
33
 
@@ -37,49 +40,67 @@ class SajuRequest(BaseModel):
37
  day: int
38
  hour: int
39
  minute: int
40
- calendar_type: str
41
  query: str
42
  lang: str = "ํ•œ๊ตญ์–ด"
43
 
44
- # --- ๐Ÿ”ฅ [์—ฌ๊ธฐ๊ฐ€ ํ•ต์‹ฌ ์ˆ˜์ •] ํŽ˜๋ฅด์†Œ๋‚˜ ์ƒ์„ฑ๊ธฐ ---
45
  def get_dynamic_persona(role_type: str, lang: str) -> Dict[str, str]:
46
- is_korean = lang in ["ํ•œ๊ตญ์–ด", "Korean", "ko", "Korea"]
 
 
47
  prompt_text = ""
48
 
49
  if role_type == "tarot":
50
  if is_korean:
51
- prompt_text = "๋‹น์‹ ์€ ํƒ€๋กœ ๋ฆฌ๋” ์—๋ฐ€๋ฆฌ์ž…๋‹ˆ๋‹ค. ์นœ์ ˆํ•˜๊ณ  ์‹ ๋น„๋กญ๊ฒŒ ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”."
 
 
 
 
52
  else:
53
- # ๐Ÿ”ฅ [์ˆ˜์ •] "์ž…๋ ฅ์ด ํ•œ๊ตญ์–ด์—ฌ๋„, ๋‹ต๋ณ€์€ ์˜์–ด๋กœ ํ•ด๋ผ"๋ผ๊ณ  ๋ช…ํ™•ํžˆ ์ง€์‹œ
54
- prompt_text = (
55
- f"You are Tarot Reader Emily. "
56
- f"IMPORTANT: The user input might contain Korean text. "
57
- f"Do not refuse to answer. Instead, TRANSLATE your reading and answer STRICTLY in {lang}."
58
- )
59
 
60
  elif role_type == "fengshui":
61
  if is_korean:
62
- prompt_text = "๋‹น์‹ ์€ ํ’์ˆ˜์ง€๋ฆฌ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ์ ์œผ๋กœ ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”."
 
 
 
 
63
  else:
64
- prompt_text = (
65
- f"You are a Feng Shui Master. "
66
- f"Even if the input is Korean, TRANSLATE your analysis and answer STRICTLY in {lang}."
67
- )
 
 
68
 
69
- elif role_type == "shaman":
70
  if is_korean:
71
- prompt_text = "๋‹น์‹ ์€ ๋ฌด๋‹น ์ฒœ๋ช…์ž…๋‹ˆ๋‹ค. ๊ฑฐ์นœ ๋ฐ˜๋ง์„ ์“ฐ๋ฉฐ ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”."
 
 
 
 
72
  else:
73
- prompt_text = (
74
- f"You are Shaman Cheon-Myeong. "
75
- f"Even if the user asks in Korean, TRANSLATE your insights and answer STRICTLY in {lang}."
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
- desc = c.get('desc_eng') or c.get('desc') or c.get('desc_kor') or "No description"
 
 
 
 
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
- @app.post("/fengshui/analyze")
122
- def analyze_fengshui(req: FengShuiRequest):
 
123
  try:
124
  gender_kor = "๋‚จ์„ฑ" if req.gender in ["Male", "๋‚จ์„ฑ"] else "์—ฌ์„ฑ"
125
- fs_data = calculations.get_fengshui_advanced(req.year, gender_kor, req.door_dir, req.head_dir)
 
126
 
127
  persona = get_dynamic_persona("fengshui", req.lang)
128
- full_query = f"[FengShui Data]: {fs_data}\nUser Query: {req.query}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- @app.post("/shaman/read")
 
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
- saju_data = calculations.get_ganji_full(b_date, b_time, req.calendar_type)
 
 
142
 
143
  persona = get_dynamic_persona("shaman", req.lang)
144
- full_query = f"[Saju Data]: {saju_data}\nUser Query: {req.query}"
 
 
 
 
 
 
 
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)}"}