Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,79 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
-
client =OpenAI(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.title("AWS Well-Architected Review")
|
| 13 |
-
st.write("Get recommendations for optimizing your AWS architecture.")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if "messages" not in st.session_state:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
| 1 |
+
from openai import OpenAI # Assuming Nvidia client is available in the same library, adjust if necessary
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Initialize Nvidia client
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
base_url="https://integrate.api.nvidia.com/v1", # Nvidia API endpoint
|
| 9 |
+
api_key=os.environ["NVIDIA_API_KEY"] # Nvidia API Key from Streamlit secrets
|
| 10 |
)
|
| 11 |
|
| 12 |
+
st.title("ChatGPT-like clone with Nvidia Nemotron 70B Model")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Sidebar with instructions and Clear Session button
|
| 15 |
+
with st.sidebar:
|
| 16 |
+
# Instruction
|
| 17 |
+
st.markdown("### Instructions 🤖\nThis is a basic chatbot. Ask anything, and the AI will try to help you! The app is supported by Yiqiao Yin.")
|
| 18 |
+
|
| 19 |
+
# Add a section to ask the user for the response length
|
| 20 |
+
st.markdown("#### Select the desired length of the AI response:")
|
| 21 |
+
response_length = st.radio(
|
| 22 |
+
"How detailed do you want the response to be?",
|
| 23 |
+
('Efficient', 'Medium', 'Academic')
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Set max_tokens based on user selection
|
| 27 |
+
if response_length == 'Efficient':
|
| 28 |
+
max_tokens = 100
|
| 29 |
+
elif response_length == 'Medium':
|
| 30 |
+
max_tokens = 600
|
| 31 |
+
else: # 'Academic'
|
| 32 |
+
max_tokens = 1024
|
| 33 |
+
|
| 34 |
+
# Clear
|
| 35 |
+
if st.button("Clear Session"):
|
| 36 |
+
st.session_state.clear()
|
| 37 |
+
st.write(f"Copyright © 2010-{datetime.now().year} Present Yiqiao Yin")
|
| 38 |
+
|
| 39 |
+
# Initialize session state variables if not already present
|
| 40 |
+
if "nvidia_model" not in st.session_state:
|
| 41 |
+
st.session_state["nvidia_model"] = "nvidia/llama-3.1-nemotron-70b-instruct"
|
| 42 |
|
| 43 |
if "messages" not in st.session_state:
|
| 44 |
+
# Adding the initial system message
|
| 45 |
+
st.session_state.messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 46 |
+
|
| 47 |
+
# Render the chat history
|
| 48 |
+
for message in st.session_state.messages:
|
| 49 |
+
with st.chat_message(message["role"]):
|
| 50 |
+
st.markdown(message["content"])
|
| 51 |
+
|
| 52 |
+
# Get new user input
|
| 53 |
+
if prompt := st.chat_input("What is up?"):
|
| 54 |
+
# Add user message to the session state
|
| 55 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 56 |
+
with st.chat_message("user"):
|
| 57 |
+
st.markdown(prompt)
|
| 58 |
+
|
| 59 |
+
# Display assistant's message while waiting for the response
|
| 60 |
+
with st.chat_message("assistant"):
|
| 61 |
+
with st.spinner("The assistant is thinking... Please wait."):
|
| 62 |
+
# Create Nvidia completion request with full conversation history
|
| 63 |
+
stream = client.chat.completions.create(
|
| 64 |
+
model=st.session_state["nvidia_model"],
|
| 65 |
+
messages=st.session_state.messages, # Include all previous messages in the API call
|
| 66 |
+
temperature=0.5,
|
| 67 |
+
top_p=0.7,
|
| 68 |
+
max_tokens=max_tokens,
|
| 69 |
+
stream=True,
|
| 70 |
+
)
|
| 71 |
+
response_chunks = []
|
| 72 |
+
for chunk in stream:
|
| 73 |
+
if chunk.choices[0].delta.content is not None:
|
| 74 |
+
response_chunks.append(chunk.choices[0].delta.content)
|
| 75 |
+
response = "".join(response_chunks)
|
| 76 |
+
st.markdown(response)
|
| 77 |
+
|
| 78 |
+
# Store the assistant response in the session state
|
| 79 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|