Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def chatbot_interface():
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
def main():
|
79 |
-
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
|
|
|
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()
|