Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from streamlit.components.v1 import html
|
|
| 4 |
from streamlit_extras.stylable_container import stylable_container
|
| 5 |
import re
|
| 6 |
import urllib.parse
|
|
|
|
| 7 |
|
| 8 |
st.title("Nvidia Chat UI")
|
| 9 |
|
|
@@ -19,7 +20,7 @@ if "api_key" not in st.session_state:
|
|
| 19 |
st.warning("Please enter your API key correctly.")
|
| 20 |
st.stop()
|
| 21 |
else:
|
| 22 |
-
st.warning("Please enter your API key to use the app. You can obtain your API key from here: https://
|
| 23 |
st.stop()
|
| 24 |
else:
|
| 25 |
client = openai.OpenAI(base_url = "https://integrate.api.nvidia.com/v1",
|
|
@@ -34,44 +35,48 @@ def get_ai_response(prompt, chat_history):
|
|
| 34 |
|
| 35 |
chat_history.insert(0, {"role": "system", "content": system_prompt})
|
| 36 |
chat_history.append({"role": "user", "content": prompt})
|
| 37 |
-
|
| 38 |
-
with st.chat_message("assistant", avatar=st.session_state.assistant_avatar):
|
| 39 |
-
stream = client.chat.completions.create(
|
| 40 |
-
model="nvidia/nemotron-4-340b-instruct",
|
| 41 |
-
messages=chat_history,
|
| 42 |
-
temperature=temperature,
|
| 43 |
-
top_p=top_p,
|
| 44 |
-
max_tokens=1024,
|
| 45 |
-
stream=True,
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
placeholder = st.empty()
|
| 49 |
-
|
| 50 |
-
with stylable_container(
|
| 51 |
-
key="stop_generating",
|
| 52 |
-
css_styles="""
|
| 53 |
-
button {
|
| 54 |
-
position: fixed;
|
| 55 |
-
bottom: 100px;
|
| 56 |
-
left: 50%;
|
| 57 |
-
transform: translateX(-50%);
|
| 58 |
-
z-index: 1;
|
| 59 |
-
}
|
| 60 |
-
""",
|
| 61 |
-
):
|
| 62 |
-
st.button("Stop generating")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
st.session_state.is_streaming = False
|
| 76 |
return st.session_state.response
|
| 77 |
|
|
@@ -199,7 +204,7 @@ with st.sidebar:
|
|
| 199 |
st.session_state.is_delete_mode = st.toggle("Enable Delete button")
|
| 200 |
|
| 201 |
st.header("Advanced Settings")
|
| 202 |
-
model = st.selectbox("Model", options=["
|
| 203 |
system_prompt = st.text_area("System Prompt", height=200)
|
| 204 |
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.3, step=0.1)
|
| 205 |
top_p = st.slider("Top-P", min_value=0.01, max_value=1.00, value=1.00, step=0.01)
|
|
|
|
| 4 |
from streamlit_extras.stylable_container import stylable_container
|
| 5 |
import re
|
| 6 |
import urllib.parse
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
st.title("Nvidia Chat UI")
|
| 10 |
|
|
|
|
| 20 |
st.warning("Please enter your API key correctly.")
|
| 21 |
st.stop()
|
| 22 |
else:
|
| 23 |
+
st.warning("Please enter your API key to use the app. You can obtain your API key from here: https://build.nvidia.com/nvidia/nemotron-4-340b-instruct")
|
| 24 |
st.stop()
|
| 25 |
else:
|
| 26 |
client = openai.OpenAI(base_url = "https://integrate.api.nvidia.com/v1",
|
|
|
|
| 35 |
|
| 36 |
chat_history.insert(0, {"role": "system", "content": system_prompt})
|
| 37 |
chat_history.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
try:
|
| 40 |
+
with st.chat_message("assistant", avatar=st.session_state.assistant_avatar):
|
| 41 |
+
stream = client.chat.completions.create(
|
| 42 |
+
model=model,
|
| 43 |
+
messages=chat_history,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
top_p=top_p,
|
| 46 |
+
max_tokens=1024,
|
| 47 |
+
stream=True,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
placeholder = st.empty()
|
| 51 |
+
|
| 52 |
+
with stylable_container(
|
| 53 |
+
key="stop_generating",
|
| 54 |
+
css_styles="""
|
| 55 |
+
button {
|
| 56 |
+
position: fixed;
|
| 57 |
+
bottom: 100px;
|
| 58 |
+
left: 50%;
|
| 59 |
+
transform: translateX(-50%);
|
| 60 |
+
z-index: 1;
|
| 61 |
+
}
|
| 62 |
+
""",
|
| 63 |
+
):
|
| 64 |
+
st.button("Stop generating")
|
| 65 |
+
|
| 66 |
+
shown_message = ""
|
| 67 |
+
|
| 68 |
+
for chunk in stream:
|
| 69 |
+
if chunk.choices[0].delta.content is not None:
|
| 70 |
+
content = chunk.choices[0].delta.content
|
| 71 |
+
st.session_state.response += content
|
| 72 |
+
shown_message += content.replace("\n", " \n")\
|
| 73 |
+
.replace("<", "\\<")\
|
| 74 |
+
.replace(">", "\\>")
|
| 75 |
+
placeholder.markdown(shown_message)
|
| 76 |
|
| 77 |
+
except:
|
| 78 |
+
st.session_state.response += "\n!Exception\n" + traceback.format_exc()
|
| 79 |
+
|
| 80 |
st.session_state.is_streaming = False
|
| 81 |
return st.session_state.response
|
| 82 |
|
|
|
|
| 204 |
st.session_state.is_delete_mode = st.toggle("Enable Delete button")
|
| 205 |
|
| 206 |
st.header("Advanced Settings")
|
| 207 |
+
model = st.selectbox("Model", options=["nvidia/nemotron-4-340b-instruct"], index=0)
|
| 208 |
system_prompt = st.text_area("System Prompt", height=200)
|
| 209 |
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.3, step=0.1)
|
| 210 |
top_p = st.slider("Top-P", min_value=0.01, max_value=1.00, value=1.00, step=0.01)
|