Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,37 +1,87 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
BACKEND_URL = "https://gauravsahu1990-Chabot_Backend.hf.space/api/ask" # Change to your Space/EC2 endpoint if remote
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
question = st.text_input("π¬ Ask your question:", placeholder="e.g., Show total order amount by customer")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
if st.button("Ask"):
|
| 18 |
if not question.strip():
|
| 19 |
st.warning("Please enter a question.")
|
| 20 |
else:
|
| 21 |
with st.spinner("Thinking..."):
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if data.get("chart"):
|
| 30 |
-
st.markdown("### π Visualization")
|
| 31 |
-
img_bytes = base64.b64decode(data["chart"])
|
| 32 |
-
st.image(img_bytes, use_container_width=True)
|
| 33 |
else:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
st.error(f"β οΈ
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
+
import base64
|
| 5 |
+
import logging
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# -------------------------------------------------------
|
| 9 |
+
# π§Ύ Logging Configuration
|
| 10 |
+
# -------------------------------------------------------
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
format="[%(asctime)s] [%(levelname)s] %(message)s",
|
| 14 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 15 |
+
)
|
| 16 |
+
logger = logging.getLogger("ChatBot-Frontend")
|
| 17 |
|
| 18 |
+
logger.info("π Starting ChatBot frontend...")
|
|
|
|
| 19 |
|
| 20 |
+
# -------------------------------------------------------
|
| 21 |
+
# βοΈ Backend URL (update for your Hugging Face Space)
|
| 22 |
+
# -------------------------------------------------------
|
| 23 |
+
BACKEND_URL = "https://gauravsahu1990-Chabot_Backend.hf.space/api/ask" # Adjust if hosted elsewhere
|
| 24 |
|
| 25 |
+
# -------------------------------------------------------
|
| 26 |
+
# π§ Streamlit App Setup
|
| 27 |
+
# -------------------------------------------------------
|
| 28 |
+
st.set_page_config(page_title="Chat Assistant", page_icon="π§ ", layout="wide")
|
| 29 |
+
st.title("π§ Chat Assistant")
|
| 30 |
+
st.write("Ask your question β Iβll show the result and chart!")
|
| 31 |
|
| 32 |
+
# -------------------------------------------------------
|
| 33 |
+
# π¬ User Input
|
| 34 |
+
# -------------------------------------------------------
|
| 35 |
question = st.text_input("π¬ Ask your question:", placeholder="e.g., Show total order amount by customer")
|
| 36 |
|
| 37 |
+
# -------------------------------------------------------
|
| 38 |
+
# ποΈ Query Submission
|
| 39 |
+
# -------------------------------------------------------
|
| 40 |
if st.button("Ask"):
|
| 41 |
if not question.strip():
|
| 42 |
st.warning("Please enter a question.")
|
| 43 |
else:
|
| 44 |
with st.spinner("Thinking..."):
|
| 45 |
+
start_time = time.time()
|
| 46 |
try:
|
| 47 |
+
logger.info(f"π¨οΈ Sending question to backend: {question}")
|
| 48 |
+
response = requests.post(BACKEND_URL, json={"question": question}, timeout=60)
|
| 49 |
+
elapsed = round(time.time() - start_time, 2)
|
| 50 |
+
logger.info(f"β±οΈ Backend responded in {elapsed}s, status {response.status_code}")
|
| 51 |
|
| 52 |
+
if response.status_code != 200:
|
| 53 |
+
st.error(f"β Backend returned HTTP {response.status_code}")
|
| 54 |
+
logger.error(f"β Backend response: {response.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
+
try:
|
| 57 |
+
data = response.json()
|
| 58 |
+
logger.info(f"β
JSON parsed successfully: keys={list(data.keys())}")
|
| 59 |
+
|
| 60 |
+
st.markdown("### π Result")
|
| 61 |
+
st.markdown(data.get("answer", ""), unsafe_allow_html=True)
|
| 62 |
+
|
| 63 |
+
if data.get("chart"):
|
| 64 |
+
st.markdown("### π Visualization")
|
| 65 |
+
img_bytes = base64.b64decode(data["chart"])
|
| 66 |
+
st.image(img_bytes, use_container_width=True)
|
| 67 |
+
logger.info("π Chart displayed successfully.")
|
| 68 |
+
else:
|
| 69 |
+
st.info("No chart available for this query.")
|
| 70 |
+
logger.info("βΉοΈ No chart returned from backend.")
|
| 71 |
+
|
| 72 |
+
except Exception as json_err:
|
| 73 |
+
st.error("β οΈ Failed to parse backend response. Check logs for details.")
|
| 74 |
+
logger.exception(f"β JSON parsing error: {json_err}")
|
| 75 |
+
logger.error(f"Raw response: {response.text}")
|
| 76 |
+
|
| 77 |
+
except requests.exceptions.ConnectionError as conn_err:
|
| 78 |
+
st.error("β οΈ Could not connect to backend. Is it running?")
|
| 79 |
+
logger.exception(f"β Connection error: {conn_err}")
|
| 80 |
+
|
| 81 |
+
except requests.exceptions.Timeout:
|
| 82 |
+
st.error("β οΈ Backend took too long to respond.")
|
| 83 |
+
logger.error("β° Request timeout after 60s.")
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
+
st.error(f"β οΈ Unexpected error: {e}")
|
| 87 |
+
logger.exception(f"β Unexpected frontend error: {e}")
|