Yasu777 commited on
Commit
5852990
·
verified ·
1 Parent(s): e483831

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -27
app.py CHANGED
@@ -26,28 +26,15 @@ if prompt := st.chat_input("質問を入力してください:"):
26
  st.session_state['conversation_history'].append({"role": "user", "content": prompt})
27
  st.chat_message("user").markdown(prompt)
28
 
29
- # Groq APIからの応答を取得(英語で生成)
30
  response_text = ""
31
- translated_text = ""
 
32
  with st.chat_message("assistant"):
33
  message_placeholder = st.empty() # 応答の一時表示用の空要素
34
 
35
  try:
36
- # llama3-70b-8192で英語の応答生成
37
- response = client.chat.completions.create(
38
- model="llama3-70b-8192", # Groq APIで利用可能な正確なモデル名に更新
39
- messages=[{"role": "user", "content": prompt}],
40
- stream=True # ストリーミングモードを有効にする
41
- )
42
-
43
- # ストリーミングで英語の応答を受信(表示しない)
44
- for chunk in response:
45
- if hasattr(chunk.choices[0].delta, 'content'):
46
- chunk_text = chunk.choices[0].delta.content
47
- if chunk_text:
48
- response_text += chunk_text
49
-
50
- # gemma2-9b-itのシステムプロンプトを設定
51
  system_message = """
52
  あなたは流暢な日本語を話すAIアシスタントです。以下の指示に従ってください:
53
 
@@ -64,31 +51,60 @@ if prompt := st.chat_input("質問を入力してください:"):
64
 
65
  これらの設定に基づいて、自然で適切、かつ状況に応じて柔軟な日本語での応答を行ってください。
66
  """
67
-
68
- # llama3-70b-8192の応答を日本語に翻訳(gemma2-9b-itを使用)
69
  translation_response = client.chat.completions.create(
70
- model="gemma2-9b-it", # Groq APIで利用可能な日本語翻訳用のモデル名に更新
71
  messages=[
72
  {"role": "system", "content": system_message},
73
- {"role": "user", "content": response_text}
74
  ],
75
  stream=True,
76
  temperature=0.8,
77
  top_p=0.92
78
  )
79
- st.success("gemma2-9b-itでの翻訳APIリクエストが正常に送信されました。")
80
 
81
- # 翻訳されたテキストをリアルタイムで表示
82
  for chunk in translation_response:
83
  if hasattr(chunk.choices[0].delta, 'content'):
84
  chunk_text = chunk.choices[0].delta.content
85
  if chunk_text:
86
- translated_text += chunk_text
87
- message_placeholder.markdown(translated_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  # 最終的な翻訳された応答を確定表示
90
- message_placeholder.markdown(translated_text)
91
- st.session_state['conversation_history'].append({"role": "assistant", "content": translated_text})
92
  st.success("APIからの応答が正常に処理されました。")
93
 
94
  except Exception as e:
 
26
  st.session_state['conversation_history'].append({"role": "user", "content": prompt})
27
  st.chat_message("user").markdown(prompt)
28
 
29
+ translated_prompt = ""
30
  response_text = ""
31
+ translated_response_text = ""
32
+
33
  with st.chat_message("assistant"):
34
  message_placeholder = st.empty() # 応答の一時表示用の空要素
35
 
36
  try:
37
+ # 日本語の質問英語に翻訳(gemma2-9b-itを使用)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  system_message = """
39
  あなたは流暢な日本語を話すAIアシスタントです。以下の指示に従ってください:
40
 
 
51
 
52
  これらの設定に基づいて、自然で適切、かつ状況に応じて柔軟な日本語での応答を行ってください。
53
  """
54
+
 
55
  translation_response = client.chat.completions.create(
56
+ model="gemma2-9b-it",
57
  messages=[
58
  {"role": "system", "content": system_message},
59
+ {"role": "user", "content": prompt}
60
  ],
61
  stream=True,
62
  temperature=0.8,
63
  top_p=0.92
64
  )
 
65
 
66
+ # 日本語の質問を英語に翻訳表示しない)
67
  for chunk in translation_response:
68
  if hasattr(chunk.choices[0].delta, 'content'):
69
  chunk_text = chunk.choices[0].delta.content
70
  if chunk_text:
71
+ translated_prompt += chunk_text
72
+
73
+ # 英語に翻訳された質問をllama3-70b-8192で応答生成
74
+ response = client.chat.completions.create(
75
+ model="llama3-70b-8192",
76
+ messages=[{"role": "user", "content": translated_prompt}],
77
+ stream=True
78
+ )
79
+
80
+ for chunk in response:
81
+ if hasattr(chunk.choices[0].delta, 'content'):
82
+ chunk_text = chunk.choices[0].delta.content
83
+ if chunk_text:
84
+ response_text += chunk_text
85
+
86
+ # 応答を日本語に翻訳
87
+ final_translation_response = client.chat.completions.create(
88
+ model="gemma2-9b-it",
89
+ messages=[
90
+ {"role": "system", "content": system_message},
91
+ {"role": "user", "content": response_text}
92
+ ],
93
+ stream=True,
94
+ temperature=0.8,
95
+ top_p=0.92
96
+ )
97
+
98
+ for chunk in final_translation_response:
99
+ if hasattr(chunk.choices[0].delta, 'content'):
100
+ chunk_text = chunk.choices[0].delta.content
101
+ if chunk_text:
102
+ translated_response_text += chunk_text
103
+ message_placeholder.markdown(translated_response_text)
104
 
105
  # 最終的な翻訳された応答を確定表示
106
+ message_placeholder.markdown(translated_response_text)
107
+ st.session_state['conversation_history'].append({"role": "assistant", "content": translated_response_text})
108
  st.success("APIからの応答が正常に処理されました。")
109
 
110
  except Exception as e: