sakthivinash
commited on
Commit
β’
62b9e5b
1
Parent(s):
840d4ed
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
|
4 |
+
# Set up the Streamlit page configuration
|
5 |
+
st.set_page_config(page_title="NVIDIA OpenAI ChatGPT", layout="centered")
|
6 |
+
|
7 |
+
# Sidebar for API key input
|
8 |
+
with st.sidebar:
|
9 |
+
st.header("API Key Configuration")
|
10 |
+
api_key = st.text_input("Enter your NVIDIA API Key:")
|
11 |
+
|
12 |
+
st.sidebar.markdown(
|
13 |
+
"""
|
14 |
+
<div style="padding: 20px; background-color: #292b2c; border-radius: 10px; color: #f0f0f0;">
|
15 |
+
<h3 style="text-align: center;">Steps to Get an API Key from NVIDIA</h3>
|
16 |
+
<ul style="font-size: 14px; color: #dcdcdc;">
|
17 |
+
<li><strong>1. Create an NVIDIA Developer Account:</strong> Go to <a href="https://developer.nvidia.com/" target="_blank" style="color: #4caf50;">NVIDIA Developer's website</a>. Sign up or log in.</li>
|
18 |
+
<li><strong>2. Access NVIDIA's Cloud AI Services:</strong> Once logged in, navigate to the <a href="https://www.nvidia.com/en-us/cloud/" target="_blank" style="color: #4caf50;">NVIDIA Cloud page</a>.</li>
|
19 |
+
<li><strong>3. Find the API Access Section:</strong> Look for the API section for models like LLaMA or similar in the "Generative AI" or "NLP" tools category.</li>
|
20 |
+
<li><strong>4. Request Access (if needed):</strong> Some APIs may require joining a waitlist or submitting a request form to get access.</li>
|
21 |
+
<li><strong>5. Get the API Key:</strong> Once granted access, you'll receive an API key for authentication in your requests.</li>
|
22 |
+
</ul>
|
23 |
+
</div>
|
24 |
+
""",
|
25 |
+
unsafe_allow_html=True
|
26 |
+
)
|
27 |
+
|
28 |
+
# Initialize session state for chat history
|
29 |
+
if "messages" not in st.session_state:
|
30 |
+
st.session_state["messages"] = [{"role": "system", "content": "You are a helpful assistant."}]
|
31 |
+
|
32 |
+
# Main Title
|
33 |
+
st.title("NVIDIA OpenAI ChatGPT Interface")
|
34 |
+
|
35 |
+
if not api_key:
|
36 |
+
st.warning("Please enter your API key in the sidebar to start.")
|
37 |
+
else:
|
38 |
+
try:
|
39 |
+
# Configure the OpenAI client
|
40 |
+
openai.api_base = "https://integrate.api.nvidia.com/v1"
|
41 |
+
openai.api_key = api_key
|
42 |
+
|
43 |
+
# Create containers for chat history and input field
|
44 |
+
chat_container = st.container()
|
45 |
+
input_container = st.empty() # Ensures the input stays fixed at the bottom
|
46 |
+
|
47 |
+
# Display the conversation
|
48 |
+
with chat_container:
|
49 |
+
# Display the conversation
|
50 |
+
for message in st.session_state["messages"]:
|
51 |
+
if message["role"] == "user":
|
52 |
+
st.markdown(f"**π§ You:** {message['content']}")
|
53 |
+
elif message["role"] == "assistant":
|
54 |
+
st.markdown(f"**π€ Bot:** {message['content']}")
|
55 |
+
|
56 |
+
with input_container:
|
57 |
+
# Input form for user messages
|
58 |
+
with st.form("chat_form", clear_on_submit=True):
|
59 |
+
user_input = st.text_input("Your message:", placeholder="Type your message here...")
|
60 |
+
submitted = st.form_submit_button("Send")
|
61 |
+
|
62 |
+
if submitted and user_input:
|
63 |
+
# Add the user's message to the chat history
|
64 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
65 |
+
|
66 |
+
# Display the user's message immediately
|
67 |
+
st.markdown(f"**π§ You:** {user_input}")
|
68 |
+
|
69 |
+
# Fetch the assistant's response
|
70 |
+
with st.spinner("π€ Bot is typing..."):
|
71 |
+
try:
|
72 |
+
# Call the API
|
73 |
+
response = openai.ChatCompletion.create(
|
74 |
+
model="nvidia/llama-3.1-nemotron-70b-instruct",
|
75 |
+
messages=st.session_state["messages"],
|
76 |
+
temperature=0.5,
|
77 |
+
top_p=0.7,
|
78 |
+
max_tokens=1024,
|
79 |
+
stream=True
|
80 |
+
)
|
81 |
+
|
82 |
+
# Stream response in real-time
|
83 |
+
response_container = st.empty()
|
84 |
+
full_response = ""
|
85 |
+
for chunk in response:
|
86 |
+
if chunk.choices[0].delta.content is not None:
|
87 |
+
delta_content = chunk.choices[0].delta.content
|
88 |
+
full_response += delta_content
|
89 |
+
response_container.markdown(f"**π€ Bot:** {full_response}")
|
90 |
+
|
91 |
+
# Add the assistant's response to chat history
|
92 |
+
st.session_state["messages"].append({"role": "assistant", "content": full_response})
|
93 |
+
except Exception as e:
|
94 |
+
st.error(f"An error occurred while fetching the response: {e}")
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
st.error(f"Failed to configure OpenAI API: {e}")
|