louiecerv commited on
Commit
30b85a2
1 Parent(s): d1778e0

save changes

Browse files
Files changed (2) hide show
  1. app.py +55 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ # Get API Key from environment variable
6
+ api_key = os.getenv("NVIDIA_API_KEY")
7
+
8
+ # Check if the API key is found
9
+ if api_key is None:
10
+ st.error("NVIDIA_API_KEY environment variable not found.")
11
+ else:
12
+ # Initialize the OpenAI client
13
+ client = OpenAI(
14
+ base_url="https://integrate.api.nvidia.com/v1",
15
+ api_key=api_key
16
+ )
17
+ def get_response(user_prompt):
18
+ try:
19
+ # Create and send the chat completion request
20
+ completion = client.chat.completions.create(
21
+ model="nvidia/llama-3.1-nemotron-70b-instruct",
22
+ messages=[{"role": "user", "content": user_prompt}],
23
+ temperature=0.5, # Adjust temperature for creativity
24
+ top_p=1,
25
+ max_tokens=1024,
26
+ stream=True # Enable streaming response
27
+ )
28
+
29
+ # Display the streaming response
30
+ response_container = st.empty()
31
+ full_response = ""
32
+ for chunk in completion:
33
+ if chunk.choices[0].delta.content is not None:
34
+ full_response += chunk.choices[0].delta.content
35
+ response_container.write(full_response)
36
+ except Exception as e:
37
+ st.error(f"An error occurred: {e}")
38
+
39
+ def main():
40
+ # Streamlit app
41
+ st.title("NVIDIA Nemotron Chatbot")
42
+
43
+ # User Prompt Input
44
+ user_prompt = st.text_area("Enter your prompt:")
45
+
46
+ # When the user clicks "Submit"
47
+ if st.button("Submit"):
48
+ if user_prompt:
49
+ # Call the OpenAI API
50
+ get_response(user_prompt)
51
+ else:
52
+ st.warning("Please enter a prompt.")
53
+
54
+ if __name__ == "__main__":
55
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ openai