kheejay88 commited on
Commit
4d73b93
·
verified ·
1 Parent(s): 59393cb

Update token from authorization issue.

Browse files
Files changed (1) hide show
  1. app.py +65 -62
app.py CHANGED
@@ -1,62 +1,65 @@
1
- import streamlit as st
2
- from openai import OpenAI
3
-
4
- # Initialize the NVIDIA model
5
- client = OpenAI(
6
- base_url="https://integrate.api.nvidia.com/v1",
7
- api_key="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" # Replace with your actual API key
8
- )
9
-
10
- # Streamlit App Layout
11
- st.title("Personalized Email Drafting Tool")
12
- st.subheader("Generate professional emails effortlessly!")
13
-
14
- # Input fields for user
15
- st.sidebar.header("Email Preferences")
16
- email_tone = st.sidebar.selectbox("Select the tone of your email:",
17
- ["Professional", "Formal", "Casual", "Persuasive", "Friendly", "Angry"])
18
- email_topic = st.text_input("Topic of the email", placeholder="e.g., Meeting reschedule")
19
- email_points = st.text_area("Key points to include",
20
- placeholder="e.g., Highlight urgency, request a response by Friday")
21
-
22
- # Slider for temperature control (creativity level)
23
- temperature = st.sidebar.slider("Set creativity level (temperature):", 0.0, 1.0, 0.5)
24
-
25
- # Generate email button
26
- if st.button("Generate Email"):
27
- if email_topic and email_points:
28
- # Display loading spinner
29
- with st.spinner("Generating your email..."):
30
- # Call NVIDIA's model
31
- messages = [
32
- {"role": "system", "content": f"Write an email with a {email_tone.lower()} tone."},
33
- {"role": "user", "content": f"Topic: {email_topic}\nKey Points: {email_points}"}
34
- ]
35
- completion = client.chat.completions.create(
36
- model="meta/llama-3.1-405b-instruct",
37
- messages=messages,
38
- temperature=temperature,
39
- top_p=0.7,
40
- max_tokens=512,
41
- stream=True
42
- )
43
-
44
- # Placeholder for real-time streaming output
45
- email_placeholder = st.empty()
46
- email_output = "" # To accumulate content
47
-
48
- for chunk in completion:
49
- if chunk.choices[0].delta.content is not None:
50
- email_output += chunk.choices[0].delta.content
51
- email_placeholder.text(email_output) # Update the same placeholder
52
-
53
- # Store the generated email in session state to persist across button clicks
54
- if email_output:
55
- st.session_state.generated_email = email_output # Store in session state
56
-
57
- else:
58
- st.warning("Please fill in both the topic and key points to generate an email.")
59
-
60
- # If generated email exists in session state, display it
61
- if 'generated_email' in st.session_state:
62
- st.text_area("Generated Email", st.session_state.generated_email, height=300)
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ API_KEY = os.getenv("MY_API_KEY")
6
+
7
+ # Initialize the NVIDIA model
8
+ client = OpenAI(
9
+ base_url="https://integrate.api.nvidia.com/v1",
10
+ api_key=API_KEY # Replace with your actual API key
11
+ )
12
+
13
+ # Streamlit App Layout
14
+ st.title("Personalized Email Drafting Tool")
15
+ st.subheader("Generate professional emails effortlessly!")
16
+
17
+ # Input fields for user
18
+ st.sidebar.header("Email Preferences")
19
+ email_tone = st.sidebar.selectbox("Select the tone of your email:",
20
+ ["Professional", "Formal", "Casual", "Persuasive", "Friendly", "Angry"])
21
+ email_topic = st.text_input("Topic of the email", placeholder="e.g., Meeting reschedule")
22
+ email_points = st.text_area("Key points to include",
23
+ placeholder="e.g., Highlight urgency, request a response by Friday")
24
+
25
+ # Slider for temperature control (creativity level)
26
+ temperature = st.sidebar.slider("Set creativity level (temperature):", 0.0, 1.0, 0.5)
27
+
28
+ # Generate email button
29
+ if st.button("Generate Email"):
30
+ if email_topic and email_points:
31
+ # Display loading spinner
32
+ with st.spinner("Generating your email..."):
33
+ # Call NVIDIA's model
34
+ messages = [
35
+ {"role": "system", "content": f"Write an email with a {email_tone.lower()} tone."},
36
+ {"role": "user", "content": f"Topic: {email_topic}\nKey Points: {email_points}"}
37
+ ]
38
+ completion = client.chat.completions.create(
39
+ model="meta/llama-3.1-405b-instruct",
40
+ messages=messages,
41
+ temperature=temperature,
42
+ top_p=0.7,
43
+ max_tokens=512,
44
+ stream=True
45
+ )
46
+
47
+ # Placeholder for real-time streaming output
48
+ email_placeholder = st.empty()
49
+ email_output = "" # To accumulate content
50
+
51
+ for chunk in completion:
52
+ if chunk.choices[0].delta.content is not None:
53
+ email_output += chunk.choices[0].delta.content
54
+ email_placeholder.text(email_output) # Update the same placeholder
55
+
56
+ # Store the generated email in session state to persist across button clicks
57
+ if email_output:
58
+ st.session_state.generated_email = email_output # Store in session state
59
+
60
+ else:
61
+ st.warning("Please fill in both the topic and key points to generate an email.")
62
+
63
+ # If generated email exists in session state, display it
64
+ if 'generated_email' in st.session_state:
65
+ st.text_area("Generated Email", st.session_state.generated_email, height=300)