seawolf2357 commited on
Commit
55ec4cb
ยท
verified ยท
1 Parent(s): 8fae160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -68
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import os
2
  import streamlit as st
3
  import json
@@ -7,8 +10,12 @@ import anthropic
7
  api_key = os.environ.get("API_KEY")
8
  client = anthropic.Anthropic(api_key=api_key)
9
 
 
 
 
10
  def get_system_prompt():
11
- return """์ œ ์ด๋ฆ„์€ Mouse-II์ž…๋‹ˆ๋‹ค.
 
12
 
13
  [๋ชฉ์ ๊ณผ ์ „๋ฌธ์„ฑ]
14
  - AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ์— ํŠนํ™”๋œ ์ „๋ฌธ ์–ด์‹œ์Šคํ„ดํŠธ๋กœ์„œ, Python๊ณผ Gradio ๊ธฐ๋ฐ˜ AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
@@ -67,75 +74,99 @@ def get_system_prompt():
67
 
68
  ์ €๋Š” ํ•ญ์ƒ Mouse-II๋กœ์„œ ์ „๋ฌธ์ ์ด๊ณ  ์ •ํ™•ํ•œ AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."""
69
 
 
 
70
  def chatbot_interface():
71
- st.title("Mouse-II์™€ ๋Œ€ํ™”ํ•˜๊ธฐ")
72
-
73
- # ๋ชจ๋ธ ๊ณ ์ • ์„ค์ •
74
- if "ai_model" not in st.session_state:
75
- st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
76
-
77
- # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
78
- if "messages" not in st.session_state:
79
- st.session_state.messages = []
80
-
81
- # ๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
82
- st.sidebar.title("๋Œ€ํ™” ๊ธฐ๋ก")
83
- uploaded_file = st.sidebar.file_uploader("๋Œ€ํ™” ๊ธฐ๋ก JSON ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
84
- if uploaded_file is not None:
85
- try:
86
- content = uploaded_file.getvalue().decode()
87
- if content.strip(): # ํŒŒ์ผ์ด ๋น„์–ด์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธ
88
- st.session_state.messages = json.loads(content)
89
- else:
90
- st.warning("์—…๋กœ๋“œ๋œ ํŒŒ์ผ์ด ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
91
- except json.JSONDecodeError:
92
- st.error("์˜ฌ๋ฐ”๋ฅธ JSON ํ˜•์‹์˜ ํŒŒ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.")
93
- except Exception as e:
94
- st.error(f"ํŒŒ์ผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
95
-
96
- # ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
97
- for message in st.session_state.messages:
98
- with st.chat_message(message["role"]):
99
- st.markdown(message["content"])
100
-
101
- # ์‚ฌ์šฉ์ž ์ž…๋ ฅ
102
- if prompt := st.chat_input("๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?"):
103
- st.session_state.messages.append({"role": "user", "content": prompt})
104
- with st.chat_message("user"):
105
- st.markdown(prompt)
106
-
107
- # AI ์‘๋‹ต ์ƒ์„ฑ
108
- with st.chat_message("assistant"):
109
- message_placeholder = st.empty()
110
- full_response = ""
111
-
112
- # API ํ˜ธ์ถœ
113
- with client.messages.stream(
114
- max_tokens=1024,
115
- system=get_system_prompt(),
116
- messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
117
- model=st.session_state["ai_model"]
118
- ) as stream:
119
- for text in stream.text_stream:
120
- full_response += str(text) if text is not None else ""
121
- message_placeholder.markdown(full_response + "โ–Œ")
122
-
123
- message_placeholder.markdown(full_response)
124
-
125
- st.session_state.messages.append({"role": "assistant", "content": full_response})
126
-
127
- # ๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ
128
- if st.button("๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ"):
129
- json_history = json.dumps(st.session_state.messages, indent=4, ensure_ascii=False)
130
- st.download_button(
131
- label="๋Œ€ํ™” ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ",
132
- data=json_history,
133
- file_name="chat_history.json",
134
- mime="application/json"
135
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  def main():
138
- chatbot_interface()
139
 
140
  if __name__ == "__main__":
141
- main()
 
1
+
2
+
3
+
4
  import os
5
  import streamlit as st
6
  import json
 
10
  api_key = os.environ.get("API_KEY")
11
  client = anthropic.Anthropic(api_key=api_key)
12
 
13
+ # ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜ ์„ค์ • (Claude-3 Sonnet์˜ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜)
14
+ MAX_TOKENS = 200000
15
+
16
  def get_system_prompt():
17
+
18
+ return """์ œ ์ด๋ฆ„์€ Mouse-II์ž…๋‹ˆ๋‹ค.
19
 
20
  [๋ชฉ์ ๊ณผ ์ „๋ฌธ์„ฑ]
21
  - AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ์— ํŠนํ™”๋œ ์ „๋ฌธ ์–ด์‹œ์Šคํ„ดํŠธ๋กœ์„œ, Python๊ณผ Gradio ๊ธฐ๋ฐ˜ AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
 
74
 
75
  ์ €๋Š” ํ•ญ์ƒ Mouse-II๋กœ์„œ ์ „๋ฌธ์ ์ด๊ณ  ์ •ํ™•ํ•œ AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."""
76
 
77
+
78
+
79
  def chatbot_interface():
80
+ st.title("Mouse-II์™€ ๋Œ€ํ™”ํ•˜๊ธฐ")
81
+
82
+ # ๋ชจ๋ธ ๊ณ ์ • ์„ค์ •
83
+ if "ai_model" not in st.session_state:
84
+ st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
85
+ # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
86
+ if "messages" not in st.session_state:
87
+ st.session_state.messages = []
88
+
89
+ # ์ž๋™ ์ €์žฅ ๊ธฐ๋Šฅ
90
+ if "auto_save" not in st.session_state:
91
+ st.session_state.auto_save = True
92
+
93
+ # ๋Œ€ํ™” ๊ธฐ๋ก ๊ด€๋ฆฌ (์‚ฌ์ด๋“œ๋ฐ”)
94
+ st.sidebar.title("๋Œ€ํ™” ๊ธฐ๋ก ๊ด€๋ฆฌ")
95
+
96
+ # ์ž๋™ ์ €์žฅ ํ† ๊ธ€
97
+ st.session_state.auto_save = st.sidebar.toggle("์ž๋™ ์ €์žฅ", value=st.session_state.auto_save)
98
+
99
+ # ๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
100
+ uploaded_file = st.sidebar.file_uploader("๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ", type=['json'])
101
+ if uploaded_file is not None:
102
+ try:
103
+ content = uploaded_file.getvalue().decode()
104
+ if content.strip():
105
+ st.session_state.messages = json.loads(content)
106
+ st.sidebar.success("๋Œ€ํ™” ๊ธฐ๋ก์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค!")
107
+ else:
108
+ st.sidebar.warning("์—…๋กœ๋“œ๋œ ํŒŒ์ผ์ด ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
109
+ except json.JSONDecodeError:
110
+ st.sidebar.error("์˜ฌ๋ฐ”๋ฅธ JSON ํ˜•์‹์˜ ํŒŒ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.")
111
+ except Exception as e:
112
+ st.sidebar.error(f"ํŒŒ์ผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
113
+
114
+ # ๋Œ€ํ™” ๊ธฐ๋ก ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
115
+ if st.sidebar.button("๋Œ€ํ™” ๊ธฐ๋ก ์ดˆ๊ธฐํ™”"):
116
+ st.session_state.messages = []
117
+ st.sidebar.success("๋Œ€ํ™” ๊ธฐ๋ก์ด ์ดˆ๊ธฐํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
118
+
119
+ # ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
120
+ for message in st.session_state.messages:
121
+ with st.chat_message(message["role"]):
122
+ st.markdown(message["content"])
123
+
124
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ
125
+ if prompt := st.chat_input("๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?"):
126
+ st.session_state.messages.append({"role": "user", "content": prompt})
127
+ with st.chat_message("user"):
128
+ st.markdown(prompt)
129
+
130
+ # AI ์‘๋‹ต ์ƒ์„ฑ
131
+ with st.chat_message("assistant"):
132
+ message_placeholder = st.empty()
133
+ full_response = ""
134
+
135
+ # API ํ˜ธ์ถœ
136
+ with client.messages.stream(
137
+ max_tokens=MAX_TOKENS,
138
+ system=get_system_prompt(),
139
+ messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
140
+ model=st.session_state["ai_model"]
141
+ ) as stream:
142
+ for text in stream.text_stream:
143
+ full_response += str(text) if text is not None else ""
144
+ message_placeholder.markdown(full_response + "โ–Œ")
145
+
146
+ message_placeholder.markdown(full_response)
147
+
148
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
149
+
150
+ # ์ž๋™ ์ €์žฅ ๊ธฐ๋Šฅ
151
+ if st.session_state.auto_save:
152
+ try:
153
+ with open('chat_history_auto_save.json', 'w', encoding='utf-8') as f:
154
+ json.dump(st.session_state.messages, f, ensure_ascii=False, indent=4)
155
+ except Exception as e:
156
+ st.sidebar.error(f"์ž๋™ ์ €์žฅ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
157
+
158
+ # ๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ
159
+ if st.sidebar.button("๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ"):
160
+ json_history = json.dumps(st.session_state.messages, indent=4, ensure_ascii=False)
161
+ st.sidebar.download_button(
162
+ label="๋Œ€ํ™” ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ",
163
+ data=json_history,
164
+ file_name="chat_history.json",
165
+ mime="application/json"
166
+ )
167
 
168
  def main():
169
+ chatbot_interface()
170
 
171
  if __name__ == "__main__":
172
+ main()