Spaces:
Running
Running
File size: 4,968 Bytes
e82962e db9828e e82962e db9828e ddbecb7 e82962e db9828e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 e82962e ddbecb7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import os
import streamlit as st
import json
import anthropic
# API ์ค์
api_key = os.environ.get("API_KEY")
client = anthropic.Anthropic(api_key=api_key)
def get_system_prompt():
return """์ ์ด๋ฆ์ Mouse-II์
๋๋ค.
[๋ชฉ์ ๊ณผ ์ ๋ฌธ์ฑ]
- AI ์๋น์ค ๊ฐ๋ฐ์ ํนํ๋ ์ ๋ฌธ ์ด์์คํดํธ๋ก์, Python๊ณผ Gradio ๊ธฐ๋ฐ AI ์๋น์ค ๊ฐ๋ฐ์ ์ง์ํฉ๋๋ค
- Hugging Face๋ฅผ ์ฃผ์ ๋ฐฐํฌ ํ๋ซํผ์ผ๋ก ํ๋ ๊ฐ๋ฐ ๊ฐ์ด๋๋ฅผ ์ ๊ณตํฉ๋๋ค
[๊ฐ๋ฐ ํ๋ก์ธ์ค ์ง์]
1. ์๊ตฌ์ฌํญ ๋ถ์
- ์ฌ์ฉ์์ AI ์๋น์ค ๊ฐ๋ฐ ๋์ฆ ํ์
- ๊ตฌํ ๊ฐ๋ฅ์ฑ ๋ฐ ๊ธฐ์ ์คํ ๊ฒํ
- Hugging Face ๋ฐฐํฌ ์ ํฉ์ฑ ํ๊ฐ
2. ์ค๊ณ ๋จ๊ณ
- Gradio UI/UX ์ค๊ณ
- ํ์ํ AI ๋ชจ๋ธ ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์
- ๋ฐ์ดํฐ ํ์ดํ๋ผ์ธ ์ค๊ณ
- ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์์ธ ์ํฉ ๋์ ๋ฐฉ์
3. ๊ตฌํ ๊ฐ์ด๋
- Python ์ฝ๋ ์์ฑ ์ง์
- Gradio ์ปดํฌ๋ํธ ๊ตฌํ
- requirements.txt ์์ธ ๋ช
์ธ
- ํ๊ฒฝ ์ค์ ๊ฐ์ด๋
4. ํ
์คํธ ๋ฐ ๊ฒ์ฆ
- ๋จ์ ํ
์คํธ ์ฝ๋ ์ ๊ณต
- ์ฑ๋ฅ ์ต์ ํ ์ ์
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๋ถ์
- ์๋ต ์๊ฐ ๊ฐ์ ๋ฐฉ์
5. ๋ฐฐํฌ ์ง์
- Hugging Face Spaces ๋ฐฐํฌ ์ ์ฐจ ์๋ด
- ๋ณด์ ์ค์ ๊ฐ์ด๋
- ๋ฆฌ์์ค ์ฌ์ฉ๋ ์ต์ ํ
- ๋ชจ๋ํฐ๋ง ๋ฐฉ์ ์ ์
[์ฝ๋ ์์ฑ ๊ท์น]
- ๋ชจ๋ ์ฝ๋ ์์ฑ์ requirements.txt ๋ด์ฉ ํ์ ํฌํจ
- ๋ฒ์ ํธํ์ฑ์ ๊ณ ๋ คํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ช
์ธ
- ์คํ ํ๊ฒฝ ์ ์ฝ์ฌํญ ๋ช
์
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๊ณ ๋ ค์ฌํญ ํ๊ธฐ
[์๋ต ์์น]
- ์ ํํ๊ณ ์คํ ๊ฐ๋ฅํ ์ฝ๋ ์ ๊ณต
- ๋ช
ํํ ์ค๋ช
๊ณผ ์ฃผ์ ํฌํจ
- ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์์ธ ์ํฉ ๊ณ ๋ ค
- ๋จ๊ณ๋ณ ๊ตฌํ ๊ฐ์ด๋ ์ ๊ณต
- ํ๊ตญ์ด๋ก ์์ฐ์ค๋ฌ์ด ์ํต
- ๋ณด์ ๋ฐ ๊ฐ์ธ์ ๋ณด ๋ณดํธ ์ค์
[ํ์ง ๋ณด์ฆ]
- ์ฝ๋ ํ
์คํธ ๊ฒฐ๊ณผ ์ ๊ณต
- ์ฑ๋ฅ ์ต์ ํ ๋ฐฉ์ ์ ์
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ๋ถ์
- ์๋ต์๊ฐ ์ธก์ ๊ฒฐ๊ณผ
- ์๋ฌ ๋ฐ์ ๊ฐ๋ฅ์ฑ ๊ฒํ
์ ๋ ํญ์ Mouse-II๋ก์ ์ ๋ฌธ์ ์ด๊ณ ์ ํํ AI ์๋น์ค ๊ฐ๋ฐ ๊ฐ์ด๋๋ฅผ ์ ๊ณตํ๊ฒ ์ต๋๋ค."""
def chatbot_interface():
st.title("Mouse-II์ ๋ํํ๊ธฐ")
# ๋ชจ๋ธ ๊ณ ์ ์ค์
if "ai_model" not in st.session_state:
st.session_state["ai_model"] = "claude-3-5-sonnet-20241022"
# ์ธ์
์ํ ์ด๊ธฐํ
if "messages" not in st.session_state:
st.session_state.messages = []
# ๋ํ ๊ธฐ๋ก ๋ถ๋ฌ์ค๊ธฐ
st.sidebar.title("๋ํ ๊ธฐ๋ก")
uploaded_file = st.sidebar.file_uploader("๋ํ ๊ธฐ๋ก JSON ํ์ผ ๋ถ๋ฌ์ค๊ธฐ")
if uploaded_file is not None:
try:
content = uploaded_file.getvalue().decode()
if content.strip(): # ํ์ผ์ด ๋น์ด์์ง ์์์ง ํ์ธ
st.session_state.messages = json.loads(content)
else:
st.warning("์
๋ก๋๋ ํ์ผ์ด ๋น์ด ์์ต๋๋ค.")
except json.JSONDecodeError:
st.error("์ฌ๋ฐ๋ฅธ JSON ํ์์ ํ์ผ์ด ์๋๋๋ค.")
except Exception as e:
st.error(f"ํ์ผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
# ๋ฉ์์ง ํ์
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# ์ฌ์ฉ์ ์
๋ ฅ
if prompt := st.chat_input("๋ฌด์์ ๋์๋๋ฆด๊น์?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# AI ์๋ต ์์ฑ
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# API ํธ์ถ
with client.messages.stream(
max_tokens=1024,
system=get_system_prompt(),
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
model=st.session_state["ai_model"]
) as stream:
for text in stream.text_stream:
full_response += str(text) if text is not None else ""
message_placeholder.markdown(full_response + "โ")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# ๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋
if st.button("๋ํ ๊ธฐ๋ก ๋ค์ด๋ก๋"):
json_history = json.dumps(st.session_state.messages, indent=4, ensure_ascii=False)
st.download_button(
label="๋ํ ๊ธฐ๋ก ์ ์ฅํ๊ธฐ",
data=json_history,
file_name="chat_history.json",
mime="application/json"
)
def main():
chatbot_interface()
if __name__ == "__main__":
main() |