Spaces:
Sleeping
Sleeping
Download app.py from flexingdatai/ai-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/spaces/flexingdatai/ai-chatbot/resolve/refs%2Fpr%2F1/app.py
- Command line
-
hf download hf://spaces/flexingdatai/ai-chatbot@refs/pr/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/flexingdatai/ai-chatbot/resolve/refs%2Fpr%2F1/app.py
3.87 kB
| import streamlit as st | |
| import google.generativeai as genai | |
| import requests | |
| from datetime import datetime | |
| from inst import system_instruction # Contains your system role string | |
| # === SETUP === | |
| api_key = st.secrets["GEMINI_API_KEY"] | |
| genai.configure(api_key=api_key) | |
| model = genai.GenerativeModel( | |
| model_name="models/gemini-2.0-flash", | |
| system_instruction=system_instruction | |
| ) | |
| # === CONFIGURE PAGE === | |
| 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%; | |
| line-height: 1.5; | |
| font-size: 14px; | |
| word-wrap: break-word; | |
| box-shadow: 0 1px 3px rgba(0,0,0,0.1); | |
| } | |
| .user-bubble { | |
| background-color: #075E54; | |
| color: white; | |
| margin-left: auto; | |
| } | |
| .bot-bubble { | |
| background-color: #ffffff; | |
| color: #333; | |
| margin-right: auto; | |
| border: 1px solid #ccc; | |
| } | |
| .chat-container { | |
| max-height: 420px; | |
| overflow-y: auto; | |
| padding: 10px; | |
| background-color: #eaf1f8; | |
| border-radius: 12px; | |
| margin-bottom: 10px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown("<h3 style='text-align: center; color: #FF5722;'>🤖 Flexing Data AI Chatbot</h3>", unsafe_allow_html=True) | |
| st.markdown("<p style='text-align: center;'>Ask anything about our Data Analytics Internship Program</p>", unsafe_allow_html=True) | |
| # === USER INFO FORM === | |
| if "user_info" not in st.session_state: | |
| with st.form("user_info_form"): | |
| name = st.text_input("Your Name") | |
| email = st.text_input("Email") | |
| whatsapp = st.text_input("WhatsApp Number") | |
| submit = st.form_submit_button("Start Chat") | |
| if submit: | |
| if name and email and whatsapp: | |
| st.session_state.user_info = { | |
| "name": name.strip(), | |
| "email": email.strip(), | |
| "whatsapp": whatsapp.strip() | |
| } | |
| else: | |
| st.warning("Please fill out all fields to start chatting.") | |
| st.stop() | |
| else: | |
| st.stop() | |
| # === CHAT SESSION INIT === | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # === POST TO GOOGLE SHEET VIA APPS SCRIPT === | |
| def send_to_google_apps_script(name, email, whatsapp, question, answer): | |
| url = st.secrets["google_url"] # Replace with your deployed Apps Script URL | |
| payload = { | |
| "name": name, | |
| "email": email, | |
| "whatsapp": whatsapp, | |
| "question": question, | |
| "answer": answer | |
| } | |
| try: | |
| requests.post(url, json=payload, timeout=5) | |
| except Exception as e: | |
| st.error(f"⚠️ Failed to send data to Google Sheet: {e}") | |
| # === CHAT INPUT === | |
| user_input = st.chat_input("Type your message...") | |
| if user_input: | |
| user = st.session_state.user_info | |
| st.session_state.messages.append(("You", user_input)) | |
| with st.spinner("Thinking..."): | |
| try: | |
| response = model.generate_content(user_input) | |
| bot_reply = response.text.strip() | |
| except Exception as e: | |
| bot_reply = "⚠️ Sorry, something went wrong." | |
| st.error(str(e)) | |
| st.session_state.messages.append(("Bot", bot_reply)) | |
| # Send to Google Sheets | |
| send_to_google_apps_script(user["name"], user["email"], user["whatsapp"], user_input, bot_reply) | |
| # === DISPLAY MESSAGES === | |
| st.markdown('<div class="chat-container">', unsafe_allow_html=True) | |
| for sender, message in st.session_state.messages: | |
| bubble_class = "user-bubble" if sender == "You" else "bot-bubble" | |
| role_icon = "🧑💼" if sender == "You" else "🤖" | |
| st.markdown( | |
| f'<div class="chat-bubble {bubble_class}">{role_icon} <strong>{sender}:</strong><br>{message}</div>', | |
| unsafe_allow_html=True | |
| ) | |
| st.markdown('</div>', unsafe_allow_html=True) | |