import streamlit as st import requests import pandas as pd import base64 import logging import time # ------------------------------------------------------- # ๐Ÿงพ Logging Configuration # ------------------------------------------------------- logging.basicConfig( level=logging.INFO, format="[%(asctime)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) logger = logging.getLogger("ChatBot-Frontend") logger.info("๐Ÿš€ Starting ChatBot frontend...") # ------------------------------------------------------- # โš™๏ธ Backend URL (update for your Hugging Face Space) # ------------------------------------------------------- BACKEND_URL = "https://gauravsahu1990-Chat-Bot-Backend.hf.space/api/ask" # Adjust if hosted elsewhere # ------------------------------------------------------- # ๐Ÿง  Streamlit App Setup # ------------------------------------------------------- st.set_page_config(page_title="Chat Assistant", page_icon="๐Ÿง ", layout="wide") st.title("๐Ÿง  Chat Assistant") st.write("Ask your question โ€” Iโ€™ll show the result and chart!") # ------------------------------------------------------- # ๐Ÿ’ฌ User Input # ------------------------------------------------------- question = st.text_input("๐Ÿ’ฌ Ask your question:", placeholder="e.g., Show total order amount by customer") # ------------------------------------------------------- # ๐ŸŽ›๏ธ Query Submission # ------------------------------------------------------- if st.button("Ask"): if not question.strip(): st.warning("Please enter a question.") else: with st.spinner("Thinking..."): start_time = time.time() try: logger.info(f"๐Ÿ—จ๏ธ Sending question to backend: {question}") response = requests.post(BACKEND_URL, json={"question": question}, timeout=60) elapsed = round(time.time() - start_time, 2) logger.info(f"โฑ๏ธ Backend responded in {elapsed}s, status {response.status_code}") if response.status_code != 200: st.error(f"โŒ Backend returned HTTP {response.status_code}") logger.error(f"โŒ Backend response: {response.text}") else: try: data = response.json() logger.info(f"โœ… JSON parsed successfully: keys={list(data.keys())}") st.markdown("### ๐Ÿ“Š Result") st.markdown(data.get("answer", ""), unsafe_allow_html=True) if data.get("chart"): st.markdown("### ๐Ÿ“ˆ Visualization") img_bytes = base64.b64decode(data["chart"]) st.image(img_bytes, use_container_width=True) logger.info("๐Ÿ“ˆ Chart displayed successfully.") else: st.info("No chart available for this query.") logger.info("โ„น๏ธ No chart returned from backend.") except Exception as json_err: st.error("โš ๏ธ Failed to parse backend response. Check logs for details.") logger.exception(f"โŒ JSON parsing error: {json_err}") logger.error(f"Raw response: {response.text}") except requests.exceptions.ConnectionError as conn_err: st.error("โš ๏ธ Could not connect to backend. Is it running?") logger.exception(f"โŒ Connection error: {conn_err}") except requests.exceptions.Timeout: st.error("โš ๏ธ Backend took too long to respond.") logger.error("โฐ Request timeout after 60s.") except Exception as e: st.error(f"โš ๏ธ Unexpected error: {e}") logger.exception(f"โŒ Unexpected frontend error: {e}")