Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,80 @@ generation_config = {
|
|
| 14 |
"response_mime_type": "text/plain",
|
| 15 |
}
|
| 16 |
|
| 17 |
-
# Streamlit
|
| 18 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# 모델 선택 옵션 추가
|
| 21 |
model_options = ["gemini-1.5-flash", "learnlm-1.5-pro-experimental"]
|
|
@@ -33,28 +105,35 @@ if "chat_session" not in st.session_state or st.session_state.get("current_model
|
|
| 33 |
st.session_state.chat_session = model.start_chat(history=[])
|
| 34 |
st.session_state.current_model = selected_model_name
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# 채팅 메시지 표시
|
| 37 |
if "messages" not in st.session_state:
|
| 38 |
st.session_state.messages = []
|
| 39 |
|
| 40 |
for message in st.session_state.messages:
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# 사용자 입력 받기
|
| 45 |
-
if prompt := st.chat_input("학습 내용을 입력하면 AI 튜터가 질문을 통해서 개념을 이해하도록 도와줍니다."):
|
| 46 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 47 |
-
|
| 48 |
-
st.markdown(prompt)
|
| 49 |
|
| 50 |
# Gemini API 호출
|
| 51 |
response = st.session_state.chat_session.send_message(prompt)
|
| 52 |
-
with st.chat_message("assistant"):
|
| 53 |
-
st.markdown(response.text)
|
| 54 |
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
|
|
|
| 55 |
|
| 56 |
-
# 초기화 버튼
|
| 57 |
-
|
| 58 |
-
st.
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 14 |
"response_mime_type": "text/plain",
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# Streamlit 앱 설정
|
| 18 |
+
st.set_page_config(page_title="AI 튜터", page_icon="🎓", initial_sidebar_state="expanded")
|
| 19 |
+
|
| 20 |
+
# 페이지 스타일 커스터마이징
|
| 21 |
+
st.markdown(
|
| 22 |
+
"""
|
| 23 |
+
<style>
|
| 24 |
+
/* 전체 배경색 설정 */
|
| 25 |
+
.stApp {
|
| 26 |
+
background-color: #fffafa; /* 은은한 핑크색 배경 */
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/* 타이틀 스타일 */
|
| 30 |
+
.main-title {
|
| 31 |
+
font-size: 3rem;
|
| 32 |
+
color: #000000; /* 검은색 */
|
| 33 |
+
font-weight: 700;
|
| 34 |
+
text-align: center;
|
| 35 |
+
margin-bottom: 20px;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
/* 채팅 메시지 스타일 */
|
| 39 |
+
.chat-message {
|
| 40 |
+
border-radius: 15px;
|
| 41 |
+
padding: 15px;
|
| 42 |
+
margin: 10px 0;
|
| 43 |
+
display: flex;
|
| 44 |
+
align-items: center;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
.chat-message-user {
|
| 48 |
+
background-color: #ffebef; /* 따뜻한 파스텔 핑크 */
|
| 49 |
+
color: #8b4513;
|
| 50 |
+
justify-content: flex-end;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
.chat-message-assistant {
|
| 54 |
+
background-color: #ffe4e6; /* 부드러운 파스텔 복숭아 핑크색 */
|
| 55 |
+
color: #6b4226;
|
| 56 |
+
justify-content: flex-start;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.chat-avatar {
|
| 60 |
+
width: 40px;
|
| 61 |
+
height: 40px;
|
| 62 |
+
border-radius: 50%;
|
| 63 |
+
margin-right: 10px;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.chat-avatar-user {
|
| 67 |
+
margin-left: 10px;
|
| 68 |
+
margin-right: 0;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/* 사용자 입력 창 스타일 */
|
| 72 |
+
.stTextInput input {
|
| 73 |
+
border-radius: 15px;
|
| 74 |
+
border: 2px solid #cd857f; /* 따뜻한 핑크색 */
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/* 버튼 스타일 */
|
| 78 |
+
.stButton button {
|
| 79 |
+
background-color: #cd857f; /* 따뜻한 핑크색 */
|
| 80 |
+
color: #fff;
|
| 81 |
+
border-radius: 15px;
|
| 82 |
+
padding: 10px 20px;
|
| 83 |
+
}
|
| 84 |
+
</style>
|
| 85 |
+
""",
|
| 86 |
+
unsafe_allow_html=True
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# 메인 타이틀
|
| 90 |
+
st.markdown("<div class='main-title'>AI 튜터 🎓</div>", unsafe_allow_html=True)
|
| 91 |
|
| 92 |
# 모델 선택 옵션 추가
|
| 93 |
model_options = ["gemini-1.5-flash", "learnlm-1.5-pro-experimental"]
|
|
|
|
| 105 |
st.session_state.chat_session = model.start_chat(history=[])
|
| 106 |
st.session_state.current_model = selected_model_name
|
| 107 |
|
| 108 |
+
# 사용자와 AI의 아이콘 URL 설정
|
| 109 |
+
user_icon_url = "https://cdn-icons-png.flaticon.com/512/1995/1995531.png" # 학생 아이콘 (책을 든 학생)
|
| 110 |
+
assistant_icon_url = "https://cdn-icons-png.flaticon.com/512/4323/4323008.png" # 튜터 아이콘 (안경 쓴 선생님)
|
| 111 |
+
|
| 112 |
# 채팅 메시지 표시
|
| 113 |
if "messages" not in st.session_state:
|
| 114 |
st.session_state.messages = []
|
| 115 |
|
| 116 |
for message in st.session_state.messages:
|
| 117 |
+
role_class = "chat-message-user" if message["role"] == "user" else "chat-message-assistant"
|
| 118 |
+
avatar_url = user_icon_url if message["role"] == "user" else assistant_icon_url
|
| 119 |
+
avatar_class = "chat-avatar-user" if message["role"] == "user" else "chat-avatar"
|
| 120 |
+
st.markdown(
|
| 121 |
+
f"<div class='chat-message {role_class}'><img src='{avatar_url}' class='chat-avatar {avatar_class}'>{message['content']}</div>",
|
| 122 |
+
unsafe_allow_html=True
|
| 123 |
+
)
|
| 124 |
|
| 125 |
# 사용자 입력 받기
|
| 126 |
+
if prompt := st.chat_input("📝학습 내용을 입력하면 AI 튜터가 질문을 통해서 개념을 이해하도록 도와줍니다."):
|
| 127 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 128 |
+
st.markdown(f"<div class='chat-message chat-message-user'>{prompt}<img src='{user_icon_url}' class='chat-avatar chat-avatar-user'></div>", unsafe_allow_html=True)
|
|
|
|
| 129 |
|
| 130 |
# Gemini API 호출
|
| 131 |
response = st.session_state.chat_session.send_message(prompt)
|
|
|
|
|
|
|
| 132 |
st.session_state.messages.append({"role": "assistant", "content": response.text})
|
| 133 |
+
st.markdown(f"<div class='chat-message chat-message-assistant'><img src='{assistant_icon_url}' class='chat-avatar'>{response.text}</div>", unsafe_allow_html=True)
|
| 134 |
|
| 135 |
+
# 초기화 버튼을 사이드바로 이동
|
| 136 |
+
with st.sidebar:
|
| 137 |
+
if st.button("💡 초기화"):
|
| 138 |
+
del st.session_state.messages # 메시지 세션 상태 삭제
|
| 139 |
+
st.rerun()
|