Spaces:
Running
Running
Update app.py and README.md
Browse files
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
---
|
| 2 |
-
title: Cohere Chat Ui
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: gray
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.33.0
|
|
@@ -15,7 +15,7 @@ User Guide for Cohere Chat UI
|
|
| 15 |
This application provides an interactive chat interface using Cohere's chat models. The following is an explanation of the main features and how to use the application.
|
| 16 |
|
| 17 |
1. Entering the API Key
|
| 18 |
-
- Enter your Cohere API key in the text box
|
| 19 |
- You can obtain your API key from here: https://dashboard.cohere.com/api-keys
|
| 20 |
- Once the API key is entered correctly, the application will become available for use.
|
| 21 |
|
|
@@ -23,10 +23,11 @@ This application provides an interactive chat interface using Cohere's chat mode
|
|
| 23 |
- Type your message in the chat input box and submit it to receive a response from the Cohere model.
|
| 24 |
- The chat history is displayed on the page, with user and Cohere model messages alternating.
|
| 25 |
|
| 26 |
-
3. Editing and
|
| 27 |
-
- Below each message, there are "Edit" and "
|
| 28 |
-
- Clicking the "Edit" button displays a text area where you can edit the content of the message. After making changes, click the "Save" button to save your edits.
|
| 29 |
- Clicking the "Delete" button removes the corresponding message from the chat history.
|
|
|
|
| 30 |
|
| 31 |
4. Copying Messages
|
| 32 |
- Below each message, there is a "Copy" button.
|
|
@@ -54,7 +55,7 @@ Cohere Chat UIの使用説明書
|
|
| 54 |
このアプリケーションは、Cohereのチャットモデルを使用して対話型のチャットインターフェースを提供します。以下は、アプリケーションの主要な機能と使用方法の説明です。
|
| 55 |
|
| 56 |
1. API キーの入力
|
| 57 |
-
-
|
| 58 |
- ここからAPIキーを入手してください: https://dashboard.cohere.com/api-keys
|
| 59 |
- APIキーが正しく入力されると、アプリケーションが使用可能になります。
|
| 60 |
|
|
@@ -62,10 +63,11 @@ Cohere Chat UIの使用説明書
|
|
| 62 |
- チャット入力ボックスにメッセージを入力し、送信するとCohereモデルからの応答が表示されます。
|
| 63 |
- チャットの履歴はページ上に表示され、ユーザーとCohereモデルのメッセージが交互に表示されます。
|
| 64 |
|
| 65 |
-
3.
|
| 66 |
-
- 各メッセージの下には、「Edit
|
| 67 |
-
- 「Edit」ボタンをクリックすると、メッセージの内容を編集できるテキストエリアが表示されます。変更を加えた後、「Save
|
| 68 |
- 「Delete」ボタンをクリックすると、対応するメッセージがチャットの履歴から削除されます。
|
|
|
|
| 69 |
|
| 70 |
4. メッセージのコピー
|
| 71 |
- 各メッセージの下には、「Copy」ボタンがあります。
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Cohere Chat Ui Dev
|
| 3 |
+
emoji: 💻
|
| 4 |
+
colorFrom: purple
|
| 5 |
colorTo: gray
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.33.0
|
|
|
|
| 15 |
This application provides an interactive chat interface using Cohere's chat models. The following is an explanation of the main features and how to use the application.
|
| 16 |
|
| 17 |
1. Entering the API Key
|
| 18 |
+
- Enter your Cohere API key in the text box.
|
| 19 |
- You can obtain your API key from here: https://dashboard.cohere.com/api-keys
|
| 20 |
- Once the API key is entered correctly, the application will become available for use.
|
| 21 |
|
|
|
|
| 23 |
- Type your message in the chat input box and submit it to receive a response from the Cohere model.
|
| 24 |
- The chat history is displayed on the page, with user and Cohere model messages alternating.
|
| 25 |
|
| 26 |
+
3. Editing, Deleting, and Retrying Messages
|
| 27 |
+
- Below each message, there are "Edit", "Delete", and "Retry" buttons.
|
| 28 |
+
- Clicking the "Edit" button displays a text area where you can edit the content of the message. After making changes, click the "Save" button to save your edits, or click the "Cancel" button to discard the changes.
|
| 29 |
- Clicking the "Delete" button removes the corresponding message from the chat history.
|
| 30 |
+
- Clicking the "Retry" button, which appears only for the last message from the AI, allows you to regenerate the AI's response. This is useful if you want to get a different or updated response without modifying the input message.
|
| 31 |
|
| 32 |
4. Copying Messages
|
| 33 |
- Below each message, there is a "Copy" button.
|
|
|
|
| 55 |
このアプリケーションは、Cohereのチャットモデルを使用して対話型のチャットインターフェースを提供します。以下は、アプリケーションの主要な機能と使用方法の説明です。
|
| 56 |
|
| 57 |
1. API キーの入力
|
| 58 |
+
- テキストボックスにCohere APIキーを入力してください。
|
| 59 |
- ここからAPIキーを入手してください: https://dashboard.cohere.com/api-keys
|
| 60 |
- APIキーが正しく入力されると、アプリケーションが使用可能になります。
|
| 61 |
|
|
|
|
| 63 |
- チャット入力ボックスにメッセージを入力し、送信するとCohereモデルからの応答が表示されます。
|
| 64 |
- チャットの履歴はページ上に表示され、ユーザーとCohereモデルのメッセージが交互に表示されます。
|
| 65 |
|
| 66 |
+
3. メッセージの編集、削除、リトライ
|
| 67 |
+
- 各メッセージの下には、「Edit」、「Delete」、「Retry」のボタンがあります。
|
| 68 |
+
- 「Edit」ボタンをクリックすると、メッセージの内容を編集できるテキストエリアが表示されます。変更を加えた後、「Save」ボタンをクリックして編集を保存するか、「Cancel」ボタンをクリックして変更を破棄します。
|
| 69 |
- 「Delete」ボタンをクリックすると、対応するメッセージがチャットの履歴から削除されます。
|
| 70 |
+
- 「Retry」ボタンは、AIからの最後のメッセージにのみ表示されます。このボタンをクリックすると、AIの応答を再生成できます。これは、入力メッセージを変更せずに、別の応答や更新された応答を取得したい場合に便利です。
|
| 71 |
|
| 72 |
4. メッセージのコピー
|
| 73 |
- 各メッセージの下には、「Copy」ボタンがあります。
|
app.py
CHANGED
|
@@ -4,22 +4,51 @@ import re
|
|
| 4 |
|
| 5 |
st.title("Cohere Chat UI")
|
| 6 |
|
| 7 |
-
api_key
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
else:
|
| 11 |
-
|
| 12 |
-
st.stop()
|
| 13 |
|
| 14 |
if "messages" not in st.session_state:
|
| 15 |
st.session_state.messages = []
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def display_messages():
|
| 18 |
for i, message in enumerate(st.session_state.messages):
|
| 19 |
name = "user" if message["role"] == "USER" else "ai"
|
| 20 |
with st.chat_message(name):
|
| 21 |
st.markdown(message["text"])
|
| 22 |
-
col1, col2, col3 = st.columns([1, 1, 1])
|
| 23 |
with col1:
|
| 24 |
if st.button("Edit", key=f"edit_{i}_{len(st.session_state.messages)}"):
|
| 25 |
st.session_state.edit_index = i
|
|
@@ -37,14 +66,33 @@ def display_messages():
|
|
| 37 |
copy_text = message["text"]
|
| 38 |
st.code(copy_text, language='plain')
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if "edit_index" in st.session_state and st.session_state.edit_index == i:
|
| 41 |
with st.form(key=f"edit_form_{i}_{len(st.session_state.messages)}"):
|
| 42 |
new_content = st.text_area("Edit message", value=st.session_state.messages[i]["text"])
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Add sidebar for advanced settings
|
| 50 |
with st.sidebar:
|
|
@@ -92,32 +140,25 @@ with st.sidebar:
|
|
| 92 |
st.session_state.messages.append({"role": role, "text": text.strip()})
|
| 93 |
st.rerun()
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if prompt := st.chat_input("What is up?"):
|
| 96 |
chat_history = st.session_state.messages.copy()
|
| 97 |
|
| 98 |
with st.chat_message("user"):
|
| 99 |
st.write(prompt)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
penalty_kwargs = {
|
| 103 |
-
"frequency_penalty" if penalty_type == "Frequency Penalty" else "presence_penalty": penalty_value
|
| 104 |
-
}
|
| 105 |
-
stream = client.chat_stream(
|
| 106 |
-
message=prompt,
|
| 107 |
-
model=model,
|
| 108 |
-
preamble=preamble,
|
| 109 |
-
chat_history=chat_history,
|
| 110 |
-
temperature=temperature,
|
| 111 |
-
**penalty_kwargs
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
response = ""
|
| 115 |
-
placeholder = st.empty()
|
| 116 |
-
for event in stream:
|
| 117 |
-
if event.event_type == "text-generation":
|
| 118 |
-
content = event.text
|
| 119 |
-
response += content
|
| 120 |
-
placeholder.markdown(response)
|
| 121 |
|
| 122 |
st.session_state.messages.append({"role": "USER", "text": prompt})
|
| 123 |
st.session_state.messages.append({"role": "CHATBOT", "text": response})
|
|
|
|
| 4 |
|
| 5 |
st.title("Cohere Chat UI")
|
| 6 |
|
| 7 |
+
if "api_key" not in st.session_state:
|
| 8 |
+
api_key = st.text_input("Enter your API Key", type="password")
|
| 9 |
+
if api_key:
|
| 10 |
+
st.session_state.api_key = api_key
|
| 11 |
+
client = cohere.Client(api_key=api_key)
|
| 12 |
+
st.rerun()
|
| 13 |
+
else:
|
| 14 |
+
st.warning("Please enter your API key to use the app. You can obtain your API key from here: https://dashboard.cohere.com/api-keys")
|
| 15 |
+
st.stop()
|
| 16 |
else:
|
| 17 |
+
client = cohere.Client(api_key=st.session_state.api_key)
|
|
|
|
| 18 |
|
| 19 |
if "messages" not in st.session_state:
|
| 20 |
st.session_state.messages = []
|
| 21 |
|
| 22 |
+
def get_ai_response(prompt, chat_history):
|
| 23 |
+
with st.chat_message("ai"):
|
| 24 |
+
penalty_kwargs = {
|
| 25 |
+
"frequency_penalty" if penalty_type == "Frequency Penalty" else "presence_penalty": penalty_value
|
| 26 |
+
}
|
| 27 |
+
stream = client.chat_stream(
|
| 28 |
+
message=prompt,
|
| 29 |
+
model=model,
|
| 30 |
+
preamble=preamble,
|
| 31 |
+
chat_history=chat_history,
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
**penalty_kwargs
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response = ""
|
| 37 |
+
placeholder = st.empty()
|
| 38 |
+
for event in stream:
|
| 39 |
+
if event.event_type == "text-generation":
|
| 40 |
+
content = event.text
|
| 41 |
+
response += content
|
| 42 |
+
placeholder.markdown(response)
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
def display_messages():
|
| 47 |
for i, message in enumerate(st.session_state.messages):
|
| 48 |
name = "user" if message["role"] == "USER" else "ai"
|
| 49 |
with st.chat_message(name):
|
| 50 |
st.markdown(message["text"])
|
| 51 |
+
col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
|
| 52 |
with col1:
|
| 53 |
if st.button("Edit", key=f"edit_{i}_{len(st.session_state.messages)}"):
|
| 54 |
st.session_state.edit_index = i
|
|
|
|
| 66 |
copy_text = message["text"]
|
| 67 |
st.code(copy_text, language='plain')
|
| 68 |
|
| 69 |
+
if i == len(st.session_state.messages) - 1 and message["role"] == "CHATBOT":
|
| 70 |
+
with col4:
|
| 71 |
+
if st.button("Retry", key=f"retry_{i}_{len(st.session_state.messages)}"):
|
| 72 |
+
del st.session_state.messages[-1]
|
| 73 |
+
st.session_state.retry_flag = True
|
| 74 |
+
st.rerun()
|
| 75 |
+
|
| 76 |
if "edit_index" in st.session_state and st.session_state.edit_index == i:
|
| 77 |
with st.form(key=f"edit_form_{i}_{len(st.session_state.messages)}"):
|
| 78 |
new_content = st.text_area("Edit message", value=st.session_state.messages[i]["text"])
|
| 79 |
+
col1, col2 = st.columns([1, 1])
|
| 80 |
+
with col1:
|
| 81 |
+
if st.form_submit_button("Save"):
|
| 82 |
+
st.session_state.messages[i]["text"] = new_content
|
| 83 |
+
del st.session_state.edit_index
|
| 84 |
+
st.rerun()
|
| 85 |
+
with col2:
|
| 86 |
+
if st.form_submit_button("Cancel"):
|
| 87 |
+
del st.session_state.edit_index
|
| 88 |
+
st.rerun()
|
| 89 |
+
|
| 90 |
+
if "retry_flag" in st.session_state and st.session_state.retry_flag == True:
|
| 91 |
+
prompt = st.session_state.messages[-1]["text"]
|
| 92 |
+
response = get_ai_response(prompt, st.session_state.messages[:-1])
|
| 93 |
+
st.session_state.messages.append({"role": "CHATBOT", "text": response})
|
| 94 |
+
st.session_state.retry_flag = False
|
| 95 |
+
st.rerun()
|
| 96 |
|
| 97 |
# Add sidebar for advanced settings
|
| 98 |
with st.sidebar:
|
|
|
|
| 140 |
st.session_state.messages.append({"role": role, "text": text.strip()})
|
| 141 |
st.rerun()
|
| 142 |
|
| 143 |
+
st.header("Change API Key")
|
| 144 |
+
new_api_key = st.text_input("Enter new API Key", type="password")
|
| 145 |
+
if st.button("Update API Key"):
|
| 146 |
+
if new_api_key:
|
| 147 |
+
st.session_state.api_key = new_api_key
|
| 148 |
+
client = cohere.Client(api_key=new_api_key)
|
| 149 |
+
st.success("API Key updated successfully!")
|
| 150 |
+
else:
|
| 151 |
+
st.warning("Please enter a valid API Key.")
|
| 152 |
+
|
| 153 |
+
display_messages()
|
| 154 |
+
|
| 155 |
if prompt := st.chat_input("What is up?"):
|
| 156 |
chat_history = st.session_state.messages.copy()
|
| 157 |
|
| 158 |
with st.chat_message("user"):
|
| 159 |
st.write(prompt)
|
| 160 |
|
| 161 |
+
response = get_ai_response(prompt, chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
st.session_state.messages.append({"role": "USER", "text": prompt})
|
| 164 |
st.session_state.messages.append({"role": "CHATBOT", "text": response})
|