Spaces:
Sleeping
Sleeping
Update token from authorization issue.
Browse files
app.py
CHANGED
|
@@ -1,62 +1,65 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from openai import OpenAI
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
st.
|
| 16 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|