Spaces:
Sleeping
Sleeping
| 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}") | |