Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,139 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import base64
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
|
| 8 |
-
if image_path is None:
|
| 9 |
-
return None
|
| 10 |
-
with Image.open(image_path) as img:
|
| 11 |
-
buffered = io.BytesIO()
|
| 12 |
-
img_format = img.format if img.format else "JPEG"
|
| 13 |
-
img.save(buffered, format=img_format)
|
| 14 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 15 |
-
return f"data:image/{img_format.lower()};base64,{img_str}"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
image_data = None
|
| 27 |
-
if image is not None:
|
| 28 |
-
image_data = image_to_data_url(image)
|
| 29 |
-
elif image_url:
|
| 30 |
-
image_data = image_url
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
]
|
| 41 |
-
}]
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
stream = client.chat.completions.create(
|
| 45 |
-
model=model,
|
| 46 |
-
messages=messages,
|
| 47 |
-
max_tokens=8000,
|
| 48 |
-
stream=True,
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
full_response = ""
|
| 52 |
-
for chunk in stream:
|
| 53 |
-
if hasattr(chunk.choices[0], 'delta') and hasattr(chunk.choices[0].delta, 'content'):
|
| 54 |
-
content = chunk.choices[0].delta.content or ""
|
| 55 |
-
full_response += content
|
| 56 |
-
yield full_response
|
| 57 |
-
elif hasattr(chunk, 'content'):
|
| 58 |
-
content = chunk.content or ""
|
| 59 |
-
full_response += content
|
| 60 |
-
yield full_response
|
| 61 |
-
except Exception as e:
|
| 62 |
-
raise gr.Error(f"API Error: {str(e)}")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
]
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
type="password",
|
| 85 |
-
placeholder="hf_XXXXXXXXXXXXXX",
|
| 86 |
-
info="Token is used temporarily for the request"
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
model_choice = gr.Dropdown(
|
| 90 |
-
label="Model Selection",
|
| 91 |
-
choices=models,
|
| 92 |
-
value=models[0]
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
sources=["upload"]
|
| 100 |
-
)
|
| 101 |
-
with gr.Tab("Image URL"):
|
| 102 |
-
image_url = gr.Textbox(
|
| 103 |
-
label="Image URL",
|
| 104 |
-
placeholder="https://example.com/image.jpg",
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
label="Prompt",
|
| 109 |
-
value="Describe this image in one sentence.",
|
| 110 |
-
lines=3
|
| 111 |
-
)
|
| 112 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
submit_btn.click(
|
| 123 |
-
fn=process_input,
|
| 124 |
-
inputs=[image_input, image_url, prompt, model_choice, hf_token],
|
| 125 |
-
outputs=output,
|
| 126 |
-
concurrency_limit=None
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
gr.Examples(
|
| 130 |
-
examples=[
|
| 131 |
-
[
|
| 132 |
-
None,
|
| 133 |
-
"https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
|
| 134 |
-
"Describe this image in one sentence.",
|
| 135 |
-
models[0],
|
| 136 |
-
""
|
| 137 |
-
],
|
| 138 |
-
[
|
| 139 |
-
None,
|
| 140 |
-
"https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png",
|
| 141 |
-
"What is unique about this image format?",
|
| 142 |
-
models[1],
|
| 143 |
-
""
|
| 144 |
-
]
|
| 145 |
-
],
|
| 146 |
-
inputs=[image_input, image_url, prompt, model_choice, hf_token],
|
| 147 |
-
label="Try these examples:"
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
if __name__ == "__main__":
|
| 151 |
-
demo.launch()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cohere
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
st.set_page_config(page_title="Cohere Chat Interface", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
MODEL_PFPS = {
|
| 7 |
+
"command-a-03-2025": "/media/pfp/cohere-pfp.png",
|
| 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 |
+
USER_PFP = "https://example.com/user-default.png"
|
| 21 |
+
|
| 22 |
+
MODEL_INFO = {
|
| 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. Command A has a context length of 256K, only requires two GPUs to run, and has 150% higher throughput compared to Command R+ 08-2024.",
|
| 25 |
+
"context_window": "256K tokens",
|
| 26 |
+
"output_tokens": "8K tokens"
|
| 27 |
+
},
|
| 28 |
+
"command-r7b-12-2024": {
|
| 29 |
+
"description": "command-r7b-12-2024 is a small, fast update delivered in December 2024. It excels at RAG, tool use, agents, and similar tasks requiring complex reasoning and multiple steps.",
|
| 30 |
+
"context_window": "128K tokens",
|
| 31 |
+
"output_tokens": "4K tokens"
|
| 32 |
+
},
|
| 33 |
+
"command-r-plus-04-2024": {
|
| 34 |
+
"description": "Command R+ is an instruction-following conversational model that performs language tasks at a higher quality, more reliably, and with a longer context than previous models. It is best suited for complex RAG workflows and multi-step tool use.",
|
| 35 |
+
"context_window": "128K tokens",
|
| 36 |
+
"output_tokens": "4K tokens"
|
| 37 |
+
},
|
| 38 |
+
"command-r-plus": {
|
| 39 |
+
"description": "command-r-plus is an alias for command-r-plus-04-2024, so if you use command-r-plus in the API, that's the model you're pointing to.",
|
| 40 |
+
"context_window": "128K tokens",
|
| 41 |
+
"output_tokens": "4K tokens"
|
| 42 |
+
},
|
| 43 |
+
"command-r-08-2024": {
|
| 44 |
+
"description": "command-r-08-2024 is an update of the Command R model, delivered in August 2024.",
|
| 45 |
+
"context_window": "128K tokens",
|
| 46 |
+
"output_tokens": "4K tokens"
|
| 47 |
+
},
|
| 48 |
+
"command-r-03-2024": {
|
| 49 |
+
"description": "Command R is an instruction-following conversational model that performs language tasks at a higher quality, more reliably, and with a longer context than previous models. It can be used for complex workflows like code generation, retrieval augmented generation (RAG), tool use, and agents.",
|
| 50 |
+
"context_window": "128K tokens",
|
| 51 |
+
"output_tokens": "4K tokens"
|
| 52 |
+
},
|
| 53 |
+
"command-r": {
|
| 54 |
+
"description": "command-r is an alias for command-r-03-2024, so if you use command-r in the API, that's the model you're pointing to.",
|
| 55 |
+
"context_window": "128K tokens",
|
| 56 |
+
"output_tokens": "4K tokens"
|
| 57 |
+
},
|
| 58 |
+
"command": {
|
| 59 |
+
"description": "An instruction-following conversational model that performs language tasks with high quality, more reliably and with a longer context than our base generative models.",
|
| 60 |
+
"context_window": "4K tokens",
|
| 61 |
+
"output_tokens": "4K tokens"
|
| 62 |
+
},
|
| 63 |
+
"command-nightly": {
|
| 64 |
+
"description": "Nightly version of command - experimental and unstable. Not recommended for production use.",
|
| 65 |
+
"context_window": "128K tokens",
|
| 66 |
+
"output_tokens": "4K tokens"
|
| 67 |
+
},
|
| 68 |
+
"command-light": {
|
| 69 |
+
"description": "Smaller, faster version of command with similar capabilities.",
|
| 70 |
+
"context_window": "4K tokens",
|
| 71 |
+
"output_tokens": "4K tokens"
|
| 72 |
+
},
|
| 73 |
+
"command-light-nightly": {
|
| 74 |
+
"description": "Nightly version of command-light - experimental and unstable. Not for production use.",
|
| 75 |
+
"context_window": "128K tokens",
|
| 76 |
+
"output_tokens": "4K tokens"
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
with st.sidebar:
|
| 81 |
+
st.title("Configuration")
|
| 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.subheader("Model Details")
|
| 92 |
+
st.image(MODEL_PFPS[selected_model], width=80)
|
| 93 |
+
st.markdown(f"**{selected_model}**")
|
| 94 |
+
st.markdown(MODEL_INFO[selected_model]["description"])
|
| 95 |
+
st.markdown(f"**Context Window:** {MODEL_INFO[selected_model]['context_window']}")
|
| 96 |
+
st.markdown(f"**Max Output:** {MODEL_INFO[selected_model]['output_tokens']}")
|
| 97 |
|
| 98 |
+
st.title(f"Chat with {selected_model.upper()}")
|
| 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 message in st.session_state.messages:
|
| 105 |
+
avatar = USER_PFP if message["role"] == "user" else MODEL_PFPS[selected_model]
|
| 106 |
+
with st.chat_message(message["role"], avatar=avatar):
|
| 107 |
+
st.markdown(message["content"])
|
| 108 |
|
| 109 |
+
if prompt := st.chat_input("Type your message..."):
|
| 110 |
+
if not api_key:
|
| 111 |
+
st.error("API key required - enter in sidebar")
|
| 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 |
+
with st.chat_message("assistant", avatar=MODEL_PFPS[selected_model]):
|
| 122 |
+
response = co.chat(
|
| 123 |
+
model=selected_model,
|
| 124 |
+
messages=st.session_state.messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"General Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|