sakthivinash commited on
Commit
b548ae4
·
verified ·
1 Parent(s): bfc10fc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:", type="password")
11
+
12
+ # Initialize session state for chat history
13
+ if "messages" not in st.session_state:
14
+ st.session_state["messages"] = [{"role": "system", "content": "You are a helpful assistant."}]
15
+
16
+ # Main Title
17
+ st.title("NVIDIA OpenAI ChatGPT Interface")
18
+
19
+ if not api_key:
20
+ st.warning("Please enter your API key in the sidebar to start.")
21
+ else:
22
+ try:
23
+ # Configure the OpenAI client
24
+ openai.api_base = "https://integrate.api.nvidia.com/v1"
25
+ openai.api_key = api_key
26
+
27
+ # Create containers for chat history and input field
28
+ chat_container = st.container()
29
+ input_container = st.empty() # Ensures the input stays fixed at the bottom
30
+
31
+ # Display the conversation
32
+ with chat_container:
33
+ # Display the conversation
34
+ for message in st.session_state["messages"]:
35
+ if message["role"] == "user":
36
+ st.markdown(f"**🧑 You:** {message['content']}")
37
+ elif message["role"] == "assistant":
38
+ st.markdown(f"**🤖 Bot:** {message['content']}")
39
+
40
+ with input_container:
41
+ # Input form for user messages
42
+ with st.form("chat_form", clear_on_submit=True):
43
+ user_input = st.text_input("Your message:", placeholder="Type your message here...")
44
+ submitted = st.form_submit_button("Send")
45
+
46
+ if submitted and user_input:
47
+ # Add the user's message to the chat history
48
+ st.session_state["messages"].append({"role": "user", "content": user_input})
49
+
50
+ # Display the user's message immediately
51
+ st.markdown(f"**🧑 You:** {user_input}")
52
+
53
+ # Fetch the assistant's response
54
+ with st.spinner("🤖 Bot is typing..."):
55
+ try:
56
+ # Call the API
57
+ response = openai.ChatCompletion.create(
58
+ model="nvidia/llama-3.1-nemotron-70b-instruct",
59
+ messages=st.session_state["messages"],
60
+ temperature=0.5,
61
+ top_p=0.7,
62
+ max_tokens=1024,
63
+ stream=True
64
+ )
65
+
66
+ # Stream response in real-time
67
+ response_container = st.empty()
68
+ full_response = ""
69
+ for chunk in response:
70
+ if chunk.choices[0].delta.content is not None:
71
+ delta_content = chunk.choices[0].delta.content
72
+ full_response += delta_content
73
+ response_container.markdown(f"**🤖 Bot:** {full_response}")
74
+
75
+ # Add the assistant's response to chat history
76
+ st.session_state["messages"].append({"role": "assistant", "content": full_response})
77
+ except Exception as e:
78
+ st.error(f"An error occurred while fetching the response: {e}")
79
+
80
+ except Exception as e:
81
+ st.error(f"Failed to configure OpenAI API: {e}")