Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,139 +1,110 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cohere
|
|
|
|
| 3 |
|
| 4 |
-
st.set_page_config(page_title="Cohere Chat
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
"command-r7b-12-2024": "/media/pfp/cohere-pfp.png",
|
| 9 |
-
"command-r-plus-04-2024": "/media/pfp/cohere-pfp.png",
|
| 10 |
-
"command-r-plus": "/media/pfp/cohere-pfp.png",
|
| 11 |
-
"command-r-08-2024": "/media/pfp/cohere-pfp.png",
|
| 12 |
-
"command-r-03-2024": "/media/pfp/cohere-pfp.png",
|
| 13 |
-
"command-r": "/media/pfp/cohere-pfp.png",
|
| 14 |
-
"command": "/media/pfp/cohere-pfp.png",
|
| 15 |
-
"command-nightly": "/media/pfp/cohere-pfp.png",
|
| 16 |
-
"command-light": "/media/pfp/cohere-pfp.png",
|
| 17 |
-
"command-light-nightly": "/media/pfp/cohere-pfp.png"
|
| 18 |
-
}
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
"command-a-03-2025": {
|
| 24 |
-
"description": "Command A is our most performant model to date, excelling at tool use, agents, retrieval augmented generation (RAG), and multilingual use cases.
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
},
|
| 28 |
"command-r7b-12-2024": {
|
| 29 |
-
"description": "
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
},
|
| 33 |
"command-r-plus-04-2024": {
|
| 34 |
-
"description": "
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
},
|
| 38 |
"command-r-plus": {
|
| 39 |
-
"description": "
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
},
|
| 43 |
"command-r-08-2024": {
|
| 44 |
-
"description": "
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
},
|
| 48 |
"command-r-03-2024": {
|
| 49 |
-
"description": "
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
},
|
| 53 |
"command-r": {
|
| 54 |
-
"description": "
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
},
|
| 58 |
"command": {
|
| 59 |
-
"description": "
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
},
|
| 63 |
"command-nightly": {
|
| 64 |
-
"description": "
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
},
|
| 68 |
"command-light": {
|
| 69 |
-
"description": "
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
},
|
| 73 |
"command-light-nightly": {
|
| 74 |
-
"description": "
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
| 80 |
with st.sidebar:
|
| 81 |
-
st.title("
|
| 82 |
api_key = st.text_input("Cohere API Key", type="password")
|
| 83 |
-
|
| 84 |
-
selected_model = st.selectbox(
|
| 85 |
-
"Select Model",
|
| 86 |
-
options=list(MODEL_INFO.keys()),
|
| 87 |
-
format_func=lambda x: x.upper()
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
st.divider()
|
| 91 |
-
st.
|
| 92 |
-
st.
|
| 93 |
-
st.markdown(
|
| 94 |
-
st.
|
| 95 |
-
st.
|
| 96 |
-
st.markdown(f"**Max Output:** {MODEL_INFO[selected_model]['output_tokens']}")
|
| 97 |
|
| 98 |
-
st.title(f"Chat
|
| 99 |
-
st.image(MODEL_PFPS[selected_model], width=50)
|
| 100 |
|
| 101 |
if "messages" not in st.session_state:
|
| 102 |
st.session_state.messages = []
|
| 103 |
|
| 104 |
-
for
|
| 105 |
-
avatar
|
| 106 |
-
|
| 107 |
-
st.markdown(message["content"])
|
| 108 |
|
| 109 |
-
if prompt := st.chat_input("
|
| 110 |
if not api_key:
|
| 111 |
-
st.error("API key required
|
| 112 |
st.stop()
|
| 113 |
|
| 114 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 115 |
with st.chat_message("user", avatar=USER_PFP):
|
| 116 |
st.markdown(prompt)
|
| 117 |
-
|
| 118 |
try:
|
| 119 |
co = cohere.ClientV2(api_key)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
if hasattr(response, 'text'):
|
| 128 |
-
full_response = response.text
|
| 129 |
-
else:
|
| 130 |
-
full_response = "Error: Unexpected API response format"
|
| 131 |
-
|
| 132 |
-
st.markdown(full_response)
|
| 133 |
-
|
| 134 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 135 |
|
| 136 |
-
except cohere.CohereError as e:
|
| 137 |
-
st.error(f"Cohere API Error: {str(e)}")
|
| 138 |
except Exception as e:
|
| 139 |
-
st.error(f"
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cohere
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="Cohere Chat", layout="wide")
|
| 6 |
|
| 7 |
+
AI_PFP = "media/pfps/cohere-pfp.png"
|
| 8 |
+
USER_PFP = "media/pfps/user-pfp.jpg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
if not os.path.exists(AI_PFP) or not os.path.exists(USER_PFP):
|
| 11 |
+
st.error("Missing profile pictures in media/pfps directory")
|
| 12 |
+
st.stop()
|
| 13 |
|
| 14 |
+
model_info = {
|
| 15 |
"command-a-03-2025": {
|
| 16 |
+
"description": "Command A is our most performant model to date, excelling at tool use, agents, retrieval augmented generation (RAG), and multilingual use cases.",
|
| 17 |
+
"context": "256K",
|
| 18 |
+
"output": "8K"
|
| 19 |
},
|
| 20 |
"command-r7b-12-2024": {
|
| 21 |
+
"description": "Small, fast update excelling at RAG, tool use, and complex reasoning tasks.",
|
| 22 |
+
"context": "128K",
|
| 23 |
+
"output": "4K"
|
| 24 |
},
|
| 25 |
"command-r-plus-04-2024": {
|
| 26 |
+
"description": "Instruction-following model for complex RAG workflows and multi-step tool use.",
|
| 27 |
+
"context": "128K",
|
| 28 |
+
"output": "4K"
|
| 29 |
},
|
| 30 |
"command-r-plus": {
|
| 31 |
+
"description": "Alias for command-r-plus-04-2024.",
|
| 32 |
+
"context": "128K",
|
| 33 |
+
"output": "4K"
|
| 34 |
},
|
| 35 |
"command-r-08-2024": {
|
| 36 |
+
"description": "Updated Command R model from August 2024.",
|
| 37 |
+
"context": "128K",
|
| 38 |
+
"output": "4K"
|
| 39 |
},
|
| 40 |
"command-r-03-2024": {
|
| 41 |
+
"description": "Instruction-following model for code generation, RAG, and agents.",
|
| 42 |
+
"context": "128K",
|
| 43 |
+
"output": "4K"
|
| 44 |
},
|
| 45 |
"command-r": {
|
| 46 |
+
"description": "Alias for command-r-03-2024.",
|
| 47 |
+
"context": "128K",
|
| 48 |
+
"output": "4K"
|
| 49 |
},
|
| 50 |
"command": {
|
| 51 |
+
"description": "Conversational model with long context capabilities.",
|
| 52 |
+
"context": "4K",
|
| 53 |
+
"output": "4K"
|
| 54 |
},
|
| 55 |
"command-nightly": {
|
| 56 |
+
"description": "Experimental nightly build (not for production).",
|
| 57 |
+
"context": "128K",
|
| 58 |
+
"output": "4K"
|
| 59 |
},
|
| 60 |
"command-light": {
|
| 61 |
+
"description": "Faster lightweight version of command.",
|
| 62 |
+
"context": "4K",
|
| 63 |
+
"output": "4K"
|
| 64 |
},
|
| 65 |
"command-light-nightly": {
|
| 66 |
+
"description": "Experimental nightly build of command-light.",
|
| 67 |
+
"context": "128K",
|
| 68 |
+
"output": "4K"
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
with st.sidebar:
|
| 73 |
+
st.title("Settings")
|
| 74 |
api_key = st.text_input("Cohere API Key", type="password")
|
| 75 |
+
selected_model = st.selectbox("Model", options=list(model_info.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
st.divider()
|
| 77 |
+
st.image(AI_PFP, width=60)
|
| 78 |
+
st.subheader(selected_model)
|
| 79 |
+
st.markdown(model_info[selected_model]["description"])
|
| 80 |
+
st.caption(f"Context: {model_info[selected_model]['context']}")
|
| 81 |
+
st.caption(f"Output: {model_info[selected_model]['output']}")
|
|
|
|
| 82 |
|
| 83 |
+
st.title(f"Chat - {selected_model}")
|
|
|
|
| 84 |
|
| 85 |
if "messages" not in st.session_state:
|
| 86 |
st.session_state.messages = []
|
| 87 |
|
| 88 |
+
for msg in st.session_state.messages:
|
| 89 |
+
with st.chat_message(msg["role"], avatar=USER_PFP if msg["role"] == "user" else AI_PFP):
|
| 90 |
+
st.markdown(msg["content"])
|
|
|
|
| 91 |
|
| 92 |
+
if prompt := st.chat_input("Message..."):
|
| 93 |
if not api_key:
|
| 94 |
+
st.error("API key required")
|
| 95 |
st.stop()
|
| 96 |
|
| 97 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 98 |
with st.chat_message("user", avatar=USER_PFP):
|
| 99 |
st.markdown(prompt)
|
| 100 |
+
|
| 101 |
try:
|
| 102 |
co = cohere.ClientV2(api_key)
|
| 103 |
+
with st.chat_message("assistant", avatar=AI_PFP):
|
| 104 |
+
response = co.chat(model=selected_model, messages=st.session_state.messages)
|
| 105 |
+
reply = response.text if hasattr(response, 'text') else "Error processing response"
|
| 106 |
+
st.markdown(reply)
|
| 107 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
+
st.error(f"Error: {str(e)}")
|