Yasu777 commited on
Commit
e652a1b
·
verified ·
1 Parent(s): b349b3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -33
app.py CHANGED
@@ -15,46 +15,38 @@ st.subheader("生成されたテキストがリアルタイムで表示されま
15
  if 'conversation_history' not in st.session_state:
16
  st.session_state['conversation_history'] = []
17
 
18
- # ユーザー入力の受付
19
- user_input = st.text_input("質問を入力してください:")
20
-
21
- # Groq APIにリクエストを送信し、ストリーミング応答を取得する関数
22
- def get_groq_response(prompt):
23
- response = client.chat.completions.create(
24
- model="llama3-8b-8192", # 適切なモデル名に置き換えてください
25
- messages=[{"role": "user", "content": prompt}],
26
- stream=True # ストリーミングモードを有効にする
27
- )
28
- return response
29
-
30
  # 過去の会話履歴を表示
31
  for entry in st.session_state['conversation_history']:
32
- st.text_area("会話履歴", value=entry, height=100, max_chars=None, key=None)
33
 
34
- # ユーザー入力を送信した場合
35
- if user_input:
36
- # ユーザーの入力を会話履歴に追加
37
- st.session_state['conversation_history'].append(f"あなた: {user_input}")
38
 
39
  # Groq APIからの応答を取得
40
- response = get_groq_response(user_input)
41
  response_text = ""
42
-
43
- # ストリーミングで部分的な応答をリアルタイムで表示
44
- message_placeholder = st.empty() # 応答の一時表示用の空要素
45
- for chunk in response:
46
- if "choices" in chunk:
47
- chunk_text = chunk['choices'][0]['delta'].get('content', '')
48
- response_text += chunk_text
49
- message_placeholder.text(response_text) # 部分的な応答を表示
50
-
51
- # 完全な応答を会話履歴に追加
52
- st.session_state['conversation_history'].append(f"AI: {response_text}")
53
-
54
- # 最終的な応答も表示
55
- st.text_area("AIからの応答:", value=response_text, height=200)
 
 
 
 
 
56
 
57
  # 会話履歴をリセットするボタン
58
  if st.button("会話をリセット"):
59
  st.session_state['conversation_history'] = []
60
- st.query_params = {}
 
15
  if 'conversation_history' not in st.session_state:
16
  st.session_state['conversation_history'] = []
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # 過去の会話履歴を表示
19
  for entry in st.session_state['conversation_history']:
20
+ st.chat_message(entry['role']).markdown(entry['content'])
21
 
22
+ # ユーザー入力の受付
23
+ if prompt := st.chat_input("質問を入力してください:"):
24
+ st.session_state['conversation_history'].append({"role": "user", "content": prompt})
25
+ st.chat_message("user").markdown(prompt)
26
 
27
  # Groq APIからの応答を取得
 
28
  response_text = ""
29
+ with st.chat_message("assistant"):
30
+ message_placeholder = st.empty() # 応答の一時表示用の空要素
31
+
32
+ response = client.chat.completions.create(
33
+ model="llama3-8b-8192", # 適切なモデル名に置き換えてください
34
+ messages=[{"role": "user", "content": prompt}],
35
+ stream=True # ストリーミングモードを有効にする
36
+ )
37
+
38
+ # ストリーミングで部分的な応答をリアルタイムで表示
39
+ for chunk in response:
40
+ if "choices" in chunk:
41
+ chunk_text = chunk['choices'][0]['delta'].get('content', '')
42
+ response_text += chunk_text
43
+ message_placeholder.markdown(response_text + "|") # 部分的な応答を表示
44
+
45
+ # 最終的な応答を確定表示
46
+ message_placeholder.markdown(response_text)
47
+ st.session_state['conversation_history'].append({"role": "assistant", "content": response_text})
48
 
49
  # 会話履歴をリセットするボタン
50
  if st.button("会話をリセット"):
51
  st.session_state['conversation_history'] = []
52
+ st.experimental_rerun()