gauravsahu1990 commited on
Commit
3c2ac6b
Β·
verified Β·
1 Parent(s): 5a89f5c

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +68 -18
app.py CHANGED
@@ -1,37 +1,87 @@
1
-
2
  import streamlit as st
3
  import requests
4
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Backend URL (if deployed together on Hugging Face, use relative URL)
7
- BACKEND_URL = "https://gauravsahu1990-Chabot_Backend.hf.space/api/ask" # Change to your Space/EC2 endpoint if remote
8
 
9
- st.set_page_config(page_title="ChatSQL Assistant", page_icon="🧠", layout="wide")
 
 
 
10
 
11
- st.title("🧠 ChatSQL Assistant")
12
- st.write("Ask your question about the database β€” I’ll show the result and chart!")
 
 
 
 
13
 
14
- # User input
 
 
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
- response = requests.post(BACKEND_URL, json={"question": question})
24
- data = response.json()
 
 
25
 
26
- st.markdown("### πŸ“Š Result")
27
- st.markdown(data.get("answer", ""), unsafe_allow_html=True)
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
- st.info("No chart available for this query.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  except Exception as e:
37
- st.error(f"⚠️ Error: {e}")
 
 
 
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}")