benjamin5607 commited on
Commit
ea0f5b7
ยท
verified ยท
1 Parent(s): c091a07

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +110 -0
main.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from utils import tarot, prompts, llm
3
+
4
+ # --- [1. ์•ฑ ์„ค์ •] ---
5
+ st.set_page_config(page_title="AI ๋งŒ์‹ ์ „", page_icon="๐Ÿ”ฎ", layout="wide")
6
+
7
+ # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
8
+ if "messages" not in st.session_state:
9
+ st.session_state.messages = []
10
+ if "tarot_stage" not in st.session_state:
11
+ st.session_state.tarot_stage = "input"
12
+ if "picked_cards" not in st.session_state:
13
+ st.session_state.picked_cards = []
14
+
15
+ # --- [2. ์‚ฌ์ด๋“œ๋ฐ”: ๋ชจ๋“œ ์„ ํƒ] ---
16
+ st.sidebar.title("โ›ฉ๏ธ ๋ชจ๋“œ ์„ ํƒ")
17
+ selected_mode = st.sidebar.radio(
18
+ "๋ˆ„๊ตฌ๋ฅผ ๋งŒ๋‚˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?",
19
+ ("tarot", "fengshui", "shaman"),
20
+ format_func=lambda x: prompts.get_persona(x)["name"]
21
+ )
22
+
23
+ # ํ˜„์žฌ ๋ชจ๋“œ์˜ ํŽ˜๋ฅด์†Œ๋‚˜ ๋กœ๋“œ
24
+ current_persona = prompts.get_persona(selected_mode)
25
+
26
+ st.sidebar.markdown("---")
27
+ st.sidebar.info(current_persona["description"])
28
+ st.sidebar.markdown(f"**ํ˜„์žฌ ๋ชจ๋“œ:** {current_persona['icon']} {current_persona['name']}")
29
+
30
+
31
+ # --- [3. ๋ฉ”์ธ ํ™”๋ฉด ๋กœ์ง] ---
32
+ st.title(f"{current_persona['icon']} {current_persona['name']}")
33
+ st.caption("AI๊ฐ€ ๋‹น์‹ ์˜ ์šด๋ช…์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ฉ๋‹ˆ๋‹ค.")
34
+ st.divider()
35
+
36
+ # ==========================================
37
+ # ๐Ÿƒ ๋ชจ๋“œ A: ํƒ€๋กœ ๋งˆ์Šคํ„ฐ
38
+ # ==========================================
39
+ if selected_mode == "tarot":
40
+ # 1๋‹จ๊ณ„: ๊ณ ๋ฏผ ์ž…๋ ฅ
41
+ if st.session_state.tarot_stage == "input":
42
+ st.subheader("๋ฌด์—‡์ด ๊ถ๊ธˆํ•˜์‹ ๊ฐ€์š”?")
43
+ query = st.text_input("๊ณ ๋ฏผ์„ ์ž…๋ ฅํ•˜์„ธ์š”", key="tarot_query")
44
+
45
+ if st.button("์šด๋ช… ํ™•์ธํ•˜๊ธฐ") and query:
46
+ st.session_state.user_query = query
47
+ st.session_state.tarot_stage = "picking"
48
+ st.rerun()
49
+
50
+ # 2๋‹จ๊ณ„: ์นด๋“œ ๋ฝ‘๊ธฐ
51
+ elif st.session_state.tarot_stage == "picking":
52
+ st.subheader(f"'{st.session_state.user_query}'์— ๋Œ€ํ•œ ๋‹ต์„ ์ฐพ์Šต๋‹ˆ๋‹ค.")
53
+ st.info("์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์นด๋“œ๋ฅผ ๋ฝ‘์œผ์„ธ์š”.")
54
+
55
+ if st.button("๐ŸŽด ์นด๋“œ 3์žฅ ๋ฝ‘๊ธฐ", type="primary", use_container_width=True):
56
+ st.session_state.picked_cards = tarot.draw_tarot_cards(3)
57
+ st.session_state.tarot_stage = "result"
58
+ st.rerun()
59
+
60
+ # 3๋‹จ๊ณ„: ๊ฒฐ๊ณผ ๋ฐ ํ•ด์„
61
+ elif st.session_state.tarot_stage == "result":
62
+ # ์นด๋“œ ๋ณด์—ฌ์ฃผ๊ธฐ
63
+ cols = st.columns(3)
64
+ for idx, card in enumerate(st.session_state.picked_cards):
65
+ with cols[idx]:
66
+ st.image(tarot.get_card_image(card["name"]), caption=card["name"])
67
+ st.write(f"**{card['name']}**: {card['desc']}")
68
+
69
+ st.divider()
70
+
71
+ # AI ํ•ด์„ ์ƒ์„ฑ
72
+ if "ai_response" not in st.session_state:
73
+ with st.spinner("์šด๋ช…์„ ํ•ด์„ํ•˜๋Š” ์ค‘์ž…๋‹ˆ๋‹ค..."):
74
+ response = llm.get_ai_response(
75
+ st.session_state.user_query,
76
+ current_persona,
77
+ context_data=st.session_state.picked_cards
78
+ )
79
+ st.session_state.ai_response = response
80
+
81
+ st.write(st.session_state.ai_response)
82
+
83
+ if st.button("๐Ÿ”„ ๋‹ค์‹œ ์ƒ๋‹ดํ•˜๊ธฐ"):
84
+ st.session_state.tarot_stage = "input"
85
+ del st.session_state["ai_response"] # ์ด์ „ ์‘๋‹ต ์‚ญ์ œ
86
+ st.rerun()
87
+
88
+ # ==========================================
89
+ # ๐Ÿก & ๐Ÿ‘น ๋ชจ๋“œ B/C: ํ’์ˆ˜ & ์ ์Ÿ์ด (์ฑ„ํŒ…ํ˜•)
90
+ # ==========================================
91
+ else:
92
+ # ์ฑ„ํŒ… ๊ธฐ๋ก ํ‘œ์‹œ
93
+ for msg in st.session_state.messages:
94
+ with st.chat_message(msg["role"]):
95
+ st.write(msg["content"])
96
+
97
+ # ์ž…๋ ฅ์ฐฝ
98
+ if prompt := st.chat_input("๊ณ ๋ฏผ์„ ํ„ธ์–ด๋†“์œผ์„ธ์š”..."):
99
+ # ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€
100
+ st.session_state.messages.append({"role": "user", "content": prompt})
101
+ with st.chat_message("user"):
102
+ st.write(prompt)
103
+
104
+ # AI ์‘๋‹ต
105
+ with st.chat_message("assistant"):
106
+ with st.spinner(f"{current_persona['name']}๊ฐ€ ์ ‘์‹  ์ค‘..."):
107
+ response = llm.get_ai_response(prompt, current_persona)
108
+ st.write(response)
109
+
110
+ st.session_state.messages.append({"role": "assistant", "content": response})