Chatbot / app.py
aeblymt's picture
Update app.py
e7b447e verified
Raw
History Blame Contribute Delete
5.84 kB
import streamlit as st
import openai
import os
# ✅ Load OpenAI API Key securely from Hugging Face Spaces
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
st.error("Missing OpenAI API key. Set it in Hugging Face Spaces.")
st.stop()
client = openai.OpenAI(api_key=api_key)
# ✅ Supreme AI Consultant System Prompt
system_prompt = """
You are Eau Claire AI, the most advanced and insightful AI consultant for businesses worldwide. Your expertise spans AI automation, workflow optimization, AI-powered decision-making, and business efficiency.
Your goal is to **educate, advise, and guide** users toward leveraging AI solutions effectively while subtly encouraging them to contact us for a consultation with Eau Claire AI for deeper, tailored insights.
### **Rules of Engagement:**
1. **Provide Expert-Level AI Guidance**
- Use precise, authoritative language.
- Give clear, actionable AI recommendations.
- Adapt responses based on business size, industry, and AI readiness.
2. **Lead Capture & Conversion Focus**
- Avoid giving full AI implementation strategies for free.
- Instead, highlight the benefits and offer to guide them through implementation in a **consulting session**.
3. **Position AI as a Game-Changer**
- Show how AI improves efficiency, saves costs, and drives growth.
- Educate users about how AI solutions fit into **their** business model.
4. **Encourage Ongoing AI Adoption**
- If users express doubt, explain how AI **isn’t just for large corporations—it’s accessible to small businesses too**.
- Offer a free **AI Roadmap Guide** in exchange for their email.
### **Final Instruction:**
Every response must be clear, insightful, and guide the user toward either:
✔ Contact us for a **free AI consultation**
✔ Downloading a **lead magnet (AI Roadmap Guide)**
✔ Subscribing to **Eau Claire AI’s updates on the latest AI trends**
Do not make any attempt to tell a guest that you will email them. I do not have a way for you to email someone, so please direct them to the Contact Us page.
"""
# 🎨 Streamlit UI Customization
st.set_page_config(page_title="Eau Claire AI Chatbot", page_icon="🤖", layout="centered")
# ✅ Custom Styling
st.markdown(
"""
<style>
.block-container { padding-top: 0px; }
header { visibility: hidden; }
.stApp { padding-top: 0px; }
/* Center the logo */
.logo-container {
display: flex;
justify-content: center;
margin-bottom: 10px;
}
/* Chat styling */
.chat-container {
width: 100%;
max-width: 600px;
margin: auto;
}
.user-message {
background-color: #0077cc;
color: white;
padding: 10px;
border-radius: 10px;
text-align: right;
margin: 5px 0;
}
.ai-message {
background-color: #333;
color: white;
padding: 10px;
border-radius: 10px;
text-align: left;
margin: 5px 0;
}
.chat-title {
font-size: 24px;
font-weight: bold;
text-align: center;
color: #0077cc; /* Eau Claire AI brand color */
margin-bottom: 15px;
}
.typing-indicator {
font-style: italic;
color: #999;
text-align: left;
margin-top: 5px;
}
</style>
""",
unsafe_allow_html=True
)
# ✅ Display Eau Cl•AI•re Logo
logo_url = "https://huggingface.co/spaces/EauClaireAI/Chatbot/resolve/main/Eau%20Cl•AI•re.png"
st.markdown(f'<div class="logo-container"><img src="{logo_url}" width="200"></div>', unsafe_allow_html=True)
# ✅ Styled Chatbot Title
st.markdown('<div class="chat-title">Eau Claire AI - AI Consulting Chatbot</div>', unsafe_allow_html=True)
# ✅ Initialize chat history in session state
if "messages" not in st.session_state:
st.session_state.messages = []
# ✅ Display chat history
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
for message in st.session_state.messages:
if message["role"] == "user":
st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
elif message["role"] == "assistant":
st.markdown(f'<div class="ai-message">{message["content"]}</div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# ✅ AI Typing Indicator
typing_placeholder = st.empty()
# ✅ Input Form
with st.form(key="chat_form", clear_on_submit=True):
user_input = st.text_input("Type your message here:", key="input_text")
submit_button = st.form_submit_button("Send")
# ✅ Handle Chat Logic
if submit_button and user_input:
# ✅ Append user input to chat history
st.session_state.messages.append({"role": "user", "content": user_input})
# ✅ Show AI is typing indicator
typing_placeholder.markdown('<div class="typing-indicator">AI is typing...</div>', unsafe_allow_html=True)
try:
# ✅ Generate AI response
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": system_prompt}] + st.session_state.messages
)
bot_reply = response.choices[0].message.content.strip()
# ✅ Append AI response to chat history
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
except Exception as e:
st.session_state.messages.append({"role": "assistant", "content": f"Error: {str(e)}"})
# ✅ Clear typing indicator
typing_placeholder.empty()
# ✅ Refresh UI by re-executing script (Now using st.rerun())
st.rerun()