Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,53 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
import streamlit as st
|
| 4 |
-
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
|
| 10 |
-
# Streamlitアプリケーションの
|
| 11 |
-
st.title("Groq API
|
| 12 |
-
st.subheader("生成されたテキストが
|
| 13 |
|
| 14 |
-
# 会話履歴
|
| 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 |
-
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"model": "gemma2-9b-it", # モデルの選択(適宜変更)
|
| 29 |
-
"prompt": prompt,
|
| 30 |
-
"stream": True # ストリーミングを有効にする
|
| 31 |
-
}
|
| 32 |
-
response = requests.post("https://api.groq.com/v1/completions", headers=headers, json=data, stream=True)
|
| 33 |
return response
|
| 34 |
|
| 35 |
# 過去の会話履歴を表示
|
| 36 |
for entry in st.session_state['conversation_history']:
|
| 37 |
-
st.text_area("
|
| 38 |
|
| 39 |
-
# ユーザーが入力を送信した
|
| 40 |
if user_input:
|
|
|
|
| 41 |
st.session_state['conversation_history'].append(f"あなた: {user_input}")
|
|
|
|
|
|
|
| 42 |
response = get_groq_response(user_input)
|
| 43 |
response_text = ""
|
| 44 |
-
|
| 45 |
-
# ストリーミングで部分的な応答を
|
| 46 |
-
for chunk in response
|
| 47 |
if chunk:
|
| 48 |
-
|
| 49 |
-
response_text +=
|
| 50 |
-
st.
|
| 51 |
-
|
| 52 |
-
# 完全な応答を
|
| 53 |
st.session_state['conversation_history'].append(f"AI: {response_text}")
|
|
|
|
|
|
|
| 54 |
st.text_area("AIからの応答:", value=response_text, height=200)
|
| 55 |
|
| 56 |
# 会話履歴をリセットするボタン
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
|
| 4 |
+
# Hugging Face Spacesの設定でAPIキーを設定していると仮定
|
| 5 |
+
openai.api_key = st.secrets["GROQ_API_KEY"]
|
|
|
|
| 6 |
|
| 7 |
+
# Streamlitアプリケーションのタイトルと説明
|
| 8 |
+
st.title("Groq APIによるAIチャットボット")
|
| 9 |
+
st.subheader("生成されたテキストがリアルタイムで表示されます")
|
| 10 |
|
| 11 |
+
# 会話履歴の初期化(セッションごとに維持)
|
| 12 |
if 'conversation_history' not in st.session_state:
|
| 13 |
st.session_state['conversation_history'] = []
|
| 14 |
|
| 15 |
+
# ユーザー入力の受付
|
| 16 |
+
user_input = st.text_input("質問を入力してください:")
|
| 17 |
|
| 18 |
+
# Groq APIにリクエストを送信し、ストリーミング応答を取得する関数
|
| 19 |
def get_groq_response(prompt):
|
| 20 |
+
response = openai.ChatCompletion.create(
|
| 21 |
+
model="gemma2-9b-it", # 適切なモデル名に置き換えてください
|
| 22 |
+
messages=[{"role": "user", "content": prompt}],
|
| 23 |
+
stream=True # ストリーミングモードを有効にする
|
| 24 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return response
|
| 26 |
|
| 27 |
# 過去の会話履歴を表示
|
| 28 |
for entry in st.session_state['conversation_history']:
|
| 29 |
+
st.text_area("会話履歴", value=entry, height=100, max_chars=None, key=None)
|
| 30 |
|
| 31 |
+
# ユーザーが入力を送信した場合
|
| 32 |
if user_input:
|
| 33 |
+
# ユーザーの入力を会話履歴に追加
|
| 34 |
st.session_state['conversation_history'].append(f"あなた: {user_input}")
|
| 35 |
+
|
| 36 |
+
# Groq APIからの応答を取得
|
| 37 |
response = get_groq_response(user_input)
|
| 38 |
response_text = ""
|
| 39 |
+
|
| 40 |
+
# ストリーミングで部分的な応答をリアルタイムで表示
|
| 41 |
+
for chunk in response:
|
| 42 |
if chunk:
|
| 43 |
+
chunk_text = chunk.choices[0].delta.content
|
| 44 |
+
response_text += chunk_text
|
| 45 |
+
st.write(response_text) # 部分的な応答を表示
|
| 46 |
+
|
| 47 |
+
# 完全な応答を会話履歴に追加
|
| 48 |
st.session_state['conversation_history'].append(f"AI: {response_text}")
|
| 49 |
+
|
| 50 |
+
# 最終的な応答も表示
|
| 51 |
st.text_area("AIからの応答:", value=response_text, height=200)
|
| 52 |
|
| 53 |
# 会話履歴をリセットするボタン
|