seawolf2357 commited on
Commit
ddbecb7
ยท
verified ยท
1 Parent(s): e62103a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -60
app.py CHANGED
@@ -8,75 +8,134 @@ api_key = os.environ.get("API_KEY")
8
  client = anthropic.Anthropic(api_key=api_key)
9
 
10
  def get_system_prompt():
11
- return """๋‹น์‹ ์€ ์นœ์ ˆํ•˜๊ณ  ์ „๋ฌธ์ ์ธ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.
12
- ๋‹ค์Œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ๋”ฐ๋ผ์ฃผ์„ธ์š”:
13
- - ์ •ํ™•ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค
14
- - ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ ๊ทธ ์‚ฌ์‹ค์„ ์ธ์ •ํ•ฉ๋‹ˆ๋‹ค
15
- - ์œ„ํ—˜ํ•˜๊ฑฐ๋‚˜ ํ•ด๋กœ์šด ์กฐ์–ธ์€ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค
16
- - ๊ฐœ์ธ์ •๋ณด๋Š” ๋ณดํ˜ธํ•ฉ๋‹ˆ๋‹ค
17
- - ์˜ˆ์˜ ๋ฐ”๋ฅด๊ณ  ๊ณต์†ํ•˜๊ฒŒ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค
18
- - ํ•œ๊ตญ์–ด๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def chatbot_interface():
21
- st.title("AI ์–ด์‹œ์Šคํ„ดํŠธ์™€ ๋Œ€ํ™”ํ•˜๊ธฐ")
22
-
23
- # ๋ชจ๋ธ ๊ณ ์ • ์„ค์ • (์„ ํƒ ๋ฐ•์Šค ์ œ๊ฑฐ)
24
- if "ai_model" not in st.session_state:
25
- st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
26
 
27
- # ์„ธ์…˜ ์ƒํƒœ ์ดˆ๊ธฐํ™”
28
- if "messages" not in st.session_state:
29
- st.session_state.messages = []
30
 
31
- # ๋Œ€ํ™” ๊ธฐ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
32
- st.sidebar.title("๋Œ€ํ™” ๊ธฐ๋ก")
33
- uploaded_file = st.sidebar.file_uploader("๋Œ€ํ™” ๊ธฐ๋ก JSON ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
34
- if uploaded_file is not None:
35
- st.session_state.messages = json.load(uploaded_file)
 
 
 
 
 
 
 
 
 
36
 
37
- # ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
38
- for message in st.session_state.messages:
39
- with st.chat_message(message["role"]):
40
- st.markdown(message["content"])
41
 
42
- # ์‚ฌ์šฉ์ž ์ž…๋ ฅ
43
- if prompt := st.chat_input("๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?"):
44
- st.session_state.messages.append({"role": "user", "content": prompt})
45
- with st.chat_message("user"):
46
- st.markdown(prompt)
47
 
48
- # AI ์‘๋‹ต ์ƒ์„ฑ
49
- with st.chat_message("assistant"):
50
- message_placeholder = st.empty()
51
- full_response = ""
52
-
53
- # Claude API ํ˜ธ์ถœ
54
- with client.messages.stream(
55
- max_tokens=1024,
56
- system=get_system_prompt(),
57
- messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
58
- model=st.session_state["ai_model"]
59
- ) as stream:
60
- for text in stream.text_stream:
61
- full_response += str(text) if text is not None else ""
62
- message_placeholder.markdown(full_response + "โ–Œ")
63
-
64
- message_placeholder.markdown(full_response)
65
-
66
- st.session_state.messages.append({"role": "assistant", "content": full_response})
67
 
68
- # ๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ
69
- if st.button("๋Œ€ํ™” ๊ธฐ๋ก ๋‹ค์šด๋กœ๋“œ"):
70
- json_history = json.dumps(st.session_state.messages, indent=4)
71
- st.download_button(
72
- label="๋Œ€ํ™” ๊ธฐ๋ก ์ €์žฅํ•˜๊ธฐ",
73
- data=json_history,
74
- file_name="chat_history.json",
75
- mime="application/json"
76
- )
77
 
78
  def main():
79
- chatbot_interface()
80
 
81
  if __name__ == "__main__":
82
- main()
 
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 ์„œ๋น„์Šค ๊ฐœ๋ฐœ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค
15
+ - Hugging Face๋ฅผ ์ฃผ์š” ๋ฐฐํฌ ํ”Œ๋žซํผ์œผ๋กœ ํ•˜๋Š” ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค
16
+
17
+ [๊ฐœ๋ฐœ ํ”„๋กœ์„ธ์Šค ์ง€์›]
18
+ 1. ์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„
19
+ - ์‚ฌ์šฉ์ž์˜ AI ์„œ๋น„์Šค ๊ฐœ๋ฐœ ๋‹ˆ์ฆˆ ํŒŒ์•…
20
+ - ๊ตฌํ˜„ ๊ฐ€๋Šฅ์„ฑ ๋ฐ ๊ธฐ์ˆ  ์Šคํƒ ๊ฒ€ํ† 
21
+ - Hugging Face ๋ฐฐํฌ ์ ํ•ฉ์„ฑ ํ‰๊ฐ€
22
+
23
+ 2. ์„ค๊ณ„ ๋‹จ๊ณ„
24
+ - Gradio UI/UX ์„ค๊ณ„
25
+ - ํ•„์š”ํ•œ AI ๋ชจ๋ธ ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ ์ •
26
+ - ๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ ์„ค๊ณ„
27
+ - ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๋ฐ ์˜ˆ์™ธ ์ƒํ™ฉ ๋Œ€์‘ ๋ฐฉ์•ˆ
28
+
29
+ 3. ๊ตฌํ˜„ ๊ฐ€์ด๋“œ
30
+ - Python ์ฝ”๋“œ ์ž‘์„ฑ ์ง€์›
31
+ - Gradio ์ปดํฌ๋„ŒํŠธ ๊ตฌํ˜„
32
+ - requirements.txt ์ƒ์„ธ ๋ช…์„ธ
33
+ - ํ™˜๊ฒฝ ์„ค์ • ๊ฐ€์ด๋“œ
34
+
35
+ 4. ํ…Œ์ŠคํŠธ ๋ฐ ๊ฒ€์ฆ
36
+ - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ œ๊ณต
37
+ - ์„ฑ๋Šฅ ์ตœ์ ํ™” ์ œ์•ˆ
38
+ - ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๋ถ„์„
39
+ - ์‘๋‹ต ์‹œ๊ฐ„ ๊ฐœ์„  ๋ฐฉ์•ˆ
40
+
41
+ 5. ๋ฐฐํฌ ์ง€์›
42
+ - Hugging Face Spaces ๋ฐฐํฌ ์ ˆ์ฐจ ์•ˆ๋‚ด
43
+ - ๋ณด์•ˆ ์„ค์ • ๊ฐ€์ด๋“œ
44
+ - ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ๋Ÿ‰ ์ตœ์ ํ™”
45
+ - ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐฉ์•ˆ ์ œ์‹œ
46
+
47
+ [์ฝ”๋“œ ์ƒ์„ฑ ๊ทœ์น™]
48
+ - ๋ชจ๋“  ์ฝ”๋“œ ์ƒ์„ฑ์‹œ requirements.txt ๋‚ด์šฉ ํ•„์ˆ˜ ํฌํ•จ
49
+ - ๋ฒ„์ „ ํ˜ธํ™˜์„ฑ์„ ๊ณ ๋ คํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ช…์„ธ
50
+ - ์‹คํ–‰ ํ™˜๊ฒฝ ์ œ์•ฝ์‚ฌํ•ญ ๋ช…์‹œ
51
+ - ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๊ณ ๋ ค์‚ฌํ•ญ ํ‘œ๊ธฐ
52
+
53
+ [์‘๋‹ต ์›์น™]
54
+ - ์ •ํ™•ํ•˜๊ณ  ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์ œ๊ณต
55
+ - ๋ช…ํ™•ํ•œ ์„ค๋ช…๊ณผ ์ฃผ์„ ํฌํ•จ
56
+ - ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๋ฐ ์˜ˆ์™ธ ์ƒํ™ฉ ๊ณ ๋ ค
57
+ - ๋‹จ๊ณ„๋ณ„ ๊ตฌํ˜„ ๊ฐ€์ด๋“œ ์ œ๊ณต
58
+ - ํ•œ๊ตญ์–ด๋กœ ์ž์—ฐ์Šค๋Ÿฌ์šด ์†Œํ†ต
59
+ - ๋ณด์•ˆ ๋ฐ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ ์ค€์ˆ˜
60
+
61
+ [ํ’ˆ์งˆ ๋ณด์ฆ]
62
+ - ์ฝ”๋“œ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ ์ œ๊ณต
63
+ - ์„ฑ๋Šฅ ์ตœ์ ํ™” ๋ฐฉ์•ˆ ์ œ์‹œ
64
+ - ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๋ถ„์„
65
+ - ์‘๋‹ต์‹œ๊ฐ„ ์ธก์ • ๊ฒฐ๊ณผ
66
+ - ์—๋Ÿฌ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ ๊ฒ€ํ† 
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()