ll7098ll commited on
Commit
1e16f85
·
verified ·
1 Parent(s): c932250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -11,7 +11,7 @@ openai.api_key = openai_api_key
11
  def openai_chat(text):
12
  try:
13
  response = openai.ChatCompletion.create(
14
- model="gpt-4",
15
  messages=[
16
  {"role": "system", "content": "초등학교 6학년 수준 범위 내에서 학생의 질문에 친절하게 답하는 교사 역할, 설명은 구체적인 예시를 들어서, 어려운 단어는 별도로 설명해줘. 설명글의 분량은 1000~2000자 정도로 해줘."},
17
  {"role": "user", "content": text}
@@ -41,11 +41,8 @@ st.markdown(
41
  if "history" not in st.session_state:
42
  st.session_state.history = []
43
 
44
- # 사용자 받기
45
- user_input = st.text_input(
46
- "질문", key="user_input", placeholder="질문을 넣어주세요.",
47
- help="AI 선생님에게 궁금한 것을 물어보세요."
48
- )
49
 
50
  def respond(user_input):
51
  if user_input:
@@ -53,17 +50,36 @@ def respond(user_input):
53
  response = openai_chat(user_input)
54
  st.session_state.history[-1]["ai"] = response
55
 
56
- # 응답 생성 및 출력
57
- if user_input:
58
- respond(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- # 채팅 메시지 출
61
- for chat in st.session_state.history:
62
- if "user" in chat:
63
- message(chat["user"], is_user=True, key=f"user_{chat['user']}")
64
- if "ai" in chat:
65
- message(chat["ai"], is_user=False, key=f"ai_{chat['ai']}")
 
 
 
 
66
 
67
  # 초기화 버튼 클릭 시
68
  if st.button("초기화"):
69
- st.session_state.history = []
 
 
11
  def openai_chat(text):
12
  try:
13
  response = openai.ChatCompletion.create(
14
+ model="gpt-4o",
15
  messages=[
16
  {"role": "system", "content": "초등학교 6학년 수준 범위 내에서 학생의 질문에 친절하게 답하는 교사 역할, 설명은 구체적인 예시를 들어서, 어려운 단어는 별도로 설명해줘. 설명글의 분량은 1000~2000자 정도로 해줘."},
17
  {"role": "user", "content": text}
 
41
  if "history" not in st.session_state:
42
  st.session_state.history = []
43
 
44
+ # 채팅 메시지 출영역
45
+ chat_container = st.container()
 
 
 
46
 
47
  def respond(user_input):
48
  if user_input:
 
50
  response = openai_chat(user_input)
51
  st.session_state.history[-1]["ai"] = response
52
 
53
+ # 사용자 입력 처리 및 응답 생성
54
+ with st.container():
55
+ # 채팅창 출력
56
+ with chat_container:
57
+ chat_scrollable = st.empty()
58
+ chat_content = ""
59
+ for chat in st.session_state.history:
60
+ if "user" in chat:
61
+ chat_content += f"<div style='text-align: right; margin: 5px 0;'><b>사용자:</b> {chat['user']}</div>"
62
+ if "ai" in chat:
63
+ chat_content += f"<div style='text-align: left; margin: 5px 0;'><b>AI:</b> {chat['ai']}</div>"
64
+ chat_scrollable.markdown(
65
+ f"""
66
+ <div style='height: 400px; overflow-y: auto; padding: 10px; border: 1px solid #ccc;'>{chat_content}</div>
67
+ """,
68
+ unsafe_allow_html=True
69
+ )
70
 
71
+ # 사용자 받기
72
+ user_input = st.text_input(
73
+ "질문", key="user_input", placeholder="질문을 넣어주세요.",
74
+ help="AI 선생님에게 궁금한 것을 물어보세요."
75
+ )
76
+
77
+ # 응답 생성 및 출력
78
+ if user_input:
79
+ respond(user_input)
80
+ st.experimental_rerun()
81
 
82
  # 초기화 버튼 클릭 시
83
  if st.button("초기화"):
84
+ st.session_state.history = []
85
+ st.experimental_rerun()