Spaces:
Sleeping
Sleeping
File size: 3,820 Bytes
3d0020b edc19e6 3d0020b 4fc7de9 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 3d0020b edc19e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import streamlit as st
import google.generativeai as genai
import requests
from inst import system_instruction
# === SETUP ===
api_key = st.secrets["GEMINI_API_KEY"]
genai.configure(api_key=api_key)
model = genai.GenerativeModel(
model_name="models/gemini-flash-latest",
system_instruction=system_instruction
)
st.set_page_config(page_title="Flexing Data AI Chatbot", layout="centered")
# === STYLES ===
st.markdown("""<style>
.chat-bubble {padding:10px 16px;border-radius:16px;margin:6px 0;max-width:85%;}
.user-bubble {background:#075E54;color:white;margin-left:auto;}
.bot-bubble {background:#fff;color:#333;margin-right:auto;border:1px solid #ccc;}
.chat-container {max-height:420px;overflow-y:auto;padding:10px;background:#eaf1f8;border-radius:12px;}
</style>""", unsafe_allow_html=True)
st.markdown("<h3 style='text-align:center;color:#FF5722;'>🤖 Flexing Data AI Chatbot</h3>", unsafe_allow_html=True)
# === USER INFO ===
if "user_info" not in st.session_state:
with st.form("user_form"):
name = st.text_input("Name")
email = st.text_input("Email")
whatsapp = st.text_input("WhatsApp")
submit = st.form_submit_button("Start Chat")
if submit:
if name and email and whatsapp:
st.session_state.user_info = {"name": name, "email": email, "whatsapp": whatsapp}
else:
st.warning("Fill all fields")
st.stop()
else:
st.stop()
# === MEMORY ===
if "messages" not in st.session_state:
st.session_state.messages = []
# === LEAD SCORING FUNCTION ===
def classify_lead(text):
text = text.lower()
hot_keywords = ["price", "fees", "join", "enroll", "start", "payment"]
warm_keywords = ["duration", "curriculum", "learn", "projects"]
if any(k in text for k in hot_keywords):
return "🔥 Hot"
elif any(k in text for k in warm_keywords):
return "🤔 Warm"
else:
return "❄️ Cold"
# === GOOGLE SHEETS ===
def send_to_sheet(data):
try:
requests.post(st.secrets["google_url"], json=data, timeout=5)
except:
pass
# === BUILD CONTEXT ===
def build_prompt(user_input):
history = ""
for role, msg in st.session_state.messages[-6:]:
history += f"{role}: {msg}\n"
return f"""
Conversation so far:
{history}
User: {user_input}
Respond helpfully, guide toward enrolling if relevant.
Keep it short and friendly.
"""
# === CHAT ===
user_input = st.chat_input("Ask anything...")
if user_input:
user = st.session_state.user_info
st.session_state.messages.append(("User", user_input))
prompt = build_prompt(user_input)
with st.spinner("Thinking..."):
try:
response = model.generate_content(
prompt,
generation_config={"temperature": 0.3, "max_output_tokens": 400}
)
bot_reply = response.text.strip()
except Exception as e:
bot_reply = "Something went wrong"
st.error(str(e))
st.session_state.messages.append(("Bot", bot_reply))
# === LEAD SCORE ===
lead_score = classify_lead(user_input)
# === SEND DATA ===
send_to_sheet({
"name": user["name"],
"email": user["email"],
"whatsapp": user["whatsapp"],
"question": user_input,
"answer": bot_reply,
"lead_score": lead_score
})
# === DISPLAY ===
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
for role, msg in st.session_state.messages:
bubble = "user-bubble" if role == "User" else "bot-bubble"
icon = "🧑" if role == "User" else "🤖"
st.markdown(f'<div class="chat-bubble {bubble}">{icon} <b>{role}:</b><br>{msg}</div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True) |