shivam701171 commited on
Commit
4ace40e
Β·
verified Β·
1 Parent(s): a719a46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -29
app.py CHANGED
@@ -1008,6 +1008,10 @@ class HuggingFaceChatBot:
1008
  # STREAMLIT APPLICATION FOR HUGGING FACE
1009
  # ===============================================================================
1010
 
 
 
 
 
1011
  def create_huggingface_app():
1012
  """Main Streamlit application optimized for Hugging Face Spaces"""
1013
 
@@ -1032,6 +1036,14 @@ def create_huggingface_app():
1032
  .status-ok { color: #28a745; font-weight: bold; }
1033
  .status-warning { color: #ffc107; font-weight: bold; }
1034
  .status-error { color: #dc3545; font-weight: bold; }
 
 
 
 
 
 
 
 
1035
  </style>
1036
  """, unsafe_allow_html=True)
1037
 
@@ -1202,45 +1214,29 @@ def create_huggingface_app():
1202
  st.balloons()
1203
 
1204
  # -------------------------------------------------------------------------
1205
- # TAB 2: AI CHAT
1206
  # -------------------------------------------------------------------------
1207
 
1208
  with tab2:
1209
  st.header("πŸ’¬ AI Chat Interface")
1210
 
1211
- # Chat interface
1212
- user_query = st.chat_input("Ask about your invoices... (e.g., 'show me total spending')")
1213
-
1214
- if user_query:
1215
- # Add user message
1216
- st.session_state.chat_history.append({
1217
- "role": "user",
1218
- "content": user_query,
1219
- "timestamp": datetime.now()
1220
- })
1221
-
1222
- # Get AI response
1223
- with st.spinner("πŸ€– AI is analyzing..."):
1224
- response = st.session_state.hf_chatbot.query_database(user_query)
1225
-
1226
- st.session_state.chat_history.append({
1227
- "role": "assistant",
1228
- "content": response,
1229
- "timestamp": datetime.now()
1230
- })
1231
-
1232
- # Display chat history
1233
- for message in st.session_state.chat_history:
1234
- with st.chat_message(message["role"]):
1235
- st.markdown(message["content"])
1236
 
1237
- # Suggested queries
1238
  if not st.session_state.chat_history:
1239
  st.markdown("### πŸ’‘ Try These Queries")
1240
 
1241
  col1, col2 = st.columns(2)
1242
 
1243
  with col1:
 
1244
  queries = [
1245
  "Show me a summary of all invoices",
1246
  "How much have we spent in total?",
@@ -1248,13 +1244,14 @@ def create_huggingface_app():
1248
  "Find invoices with high amounts"
1249
  ]
1250
  for i, query in enumerate(queries):
1251
- if st.button(query, key=f"query_{i}"):
1252
  st.session_state.chat_history.append({"role": "user", "content": query, "timestamp": datetime.now()})
1253
  response = st.session_state.hf_chatbot.query_database(query)
1254
  st.session_state.chat_history.append({"role": "assistant", "content": response, "timestamp": datetime.now()})
1255
  st.rerun()
1256
 
1257
  with col2:
 
1258
  if st.session_state.hf_processor.vector_store:
1259
  semantic_queries = [
1260
  "Find technology equipment purchases",
@@ -1263,11 +1260,56 @@ def create_huggingface_app():
1263
  "Find maintenance contracts"
1264
  ]
1265
  for i, query in enumerate(semantic_queries):
1266
- if st.button(query, key=f"semantic_{i}"):
1267
  st.session_state.chat_history.append({"role": "user", "content": query, "timestamp": datetime.now()})
1268
  response = st.session_state.hf_chatbot.query_database(query)
1269
  st.session_state.chat_history.append({"role": "assistant", "content": response, "timestamp": datetime.now()})
1270
  st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1271
 
1272
  # -------------------------------------------------------------------------
1273
  # TAB 3: ANALYTICS
@@ -1444,6 +1486,44 @@ def create_huggingface_app():
1444
  except Exception as e:
1445
  st.error(f"Data explorer error: {e}")
1446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1447
  # Footer
1448
  st.markdown("---")
1449
  st.markdown("""
@@ -1453,6 +1533,27 @@ def create_huggingface_app():
1453
  </div>
1454
  """, unsafe_allow_html=True)
1455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1456
  # ===============================================================================
1457
  # HUGGING FACE REQUIREMENTS AND CONFIGURATION
1458
  # ===============================================================================
 
1008
  # STREAMLIT APPLICATION FOR HUGGING FACE
1009
  # ===============================================================================
1010
 
1011
+ # ===============================================================================
1012
+ # FIXED MAIN APPLICATION WITH PROPER CHAT INPUT PLACEMENT
1013
+ # ===============================================================================
1014
+
1015
  def create_huggingface_app():
1016
  """Main Streamlit application optimized for Hugging Face Spaces"""
1017
 
 
1036
  .status-ok { color: #28a745; font-weight: bold; }
1037
  .status-warning { color: #ffc107; font-weight: bold; }
1038
  .status-error { color: #dc3545; font-weight: bold; }
1039
+ .chat-container {
1040
+ border: 1px solid #ddd;
1041
+ border-radius: 10px;
1042
+ padding: 1rem;
1043
+ margin: 1rem 0;
1044
+ max-height: 400px;
1045
+ overflow-y: auto;
1046
+ }
1047
  </style>
1048
  """, unsafe_allow_html=True)
1049
 
 
1214
  st.balloons()
1215
 
1216
  # -------------------------------------------------------------------------
1217
+ # TAB 2: AI CHAT - FIXED LAYOUT
1218
  # -------------------------------------------------------------------------
1219
 
1220
  with tab2:
1221
  st.header("πŸ’¬ AI Chat Interface")
1222
 
1223
+ # Display chat history first
1224
+ if st.session_state.chat_history:
1225
+ st.markdown("### πŸ’¬ Chat History")
1226
+ chat_container = st.container()
1227
+ with chat_container:
1228
+ for message in st.session_state.chat_history:
1229
+ with st.chat_message(message["role"]):
1230
+ st.markdown(message["content"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1231
 
1232
+ # Suggested queries when no chat history
1233
  if not st.session_state.chat_history:
1234
  st.markdown("### πŸ’‘ Try These Queries")
1235
 
1236
  col1, col2 = st.columns(2)
1237
 
1238
  with col1:
1239
+ st.markdown("**πŸ“Š Basic Queries:**")
1240
  queries = [
1241
  "Show me a summary of all invoices",
1242
  "How much have we spent in total?",
 
1244
  "Find invoices with high amounts"
1245
  ]
1246
  for i, query in enumerate(queries):
1247
+ if st.button(query, key=f"query_{i}", use_container_width=True):
1248
  st.session_state.chat_history.append({"role": "user", "content": query, "timestamp": datetime.now()})
1249
  response = st.session_state.hf_chatbot.query_database(query)
1250
  st.session_state.chat_history.append({"role": "assistant", "content": response, "timestamp": datetime.now()})
1251
  st.rerun()
1252
 
1253
  with col2:
1254
+ st.markdown("**πŸ” Semantic Queries:**")
1255
  if st.session_state.hf_processor.vector_store:
1256
  semantic_queries = [
1257
  "Find technology equipment purchases",
 
1260
  "Find maintenance contracts"
1261
  ]
1262
  for i, query in enumerate(semantic_queries):
1263
+ if st.button(query, key=f"semantic_{i}", use_container_width=True):
1264
  st.session_state.chat_history.append({"role": "user", "content": query, "timestamp": datetime.now()})
1265
  response = st.session_state.hf_chatbot.query_database(query)
1266
  st.session_state.chat_history.append({"role": "assistant", "content": response, "timestamp": datetime.now()})
1267
  st.rerun()
1268
+ else:
1269
+ st.info("Semantic search not available. Upload some invoices first!")
1270
+
1271
+ # Alternative input method using text input and button
1272
+ st.markdown("### ✍️ Ask a Question")
1273
+
1274
+ col1, col2 = st.columns([4, 1])
1275
+
1276
+ with col1:
1277
+ user_input = st.text_input(
1278
+ "Type your question here:",
1279
+ placeholder="e.g., 'show me total spending' or 'find technology purchases'",
1280
+ key="chat_text_input"
1281
+ )
1282
+
1283
+ with col2:
1284
+ ask_button = st.button("πŸš€ Ask", type="primary", use_container_width=True)
1285
+
1286
+ # Process the input
1287
+ if ask_button and user_input:
1288
+ # Add user message
1289
+ st.session_state.chat_history.append({
1290
+ "role": "user",
1291
+ "content": user_input,
1292
+ "timestamp": datetime.now()
1293
+ })
1294
+
1295
+ # Get AI response
1296
+ with st.spinner("πŸ€– AI is analyzing..."):
1297
+ response = st.session_state.hf_chatbot.query_database(user_input)
1298
+
1299
+ st.session_state.chat_history.append({
1300
+ "role": "assistant",
1301
+ "content": response,
1302
+ "timestamp": datetime.now()
1303
+ })
1304
+
1305
+ # Clear the input and rerun to show new messages
1306
+ st.rerun()
1307
+
1308
+ # Clear chat button
1309
+ if st.session_state.chat_history:
1310
+ if st.button("πŸ—‘οΈ Clear Chat History", use_container_width=True):
1311
+ st.session_state.chat_history = []
1312
+ st.rerun()
1313
 
1314
  # -------------------------------------------------------------------------
1315
  # TAB 3: ANALYTICS
 
1486
  except Exception as e:
1487
  st.error(f"Data explorer error: {e}")
1488
 
1489
+ # -------------------------------------------------------------------------
1490
+ # GLOBAL CHAT INPUT - OUTSIDE TABS
1491
+ # -------------------------------------------------------------------------
1492
+
1493
+ # Add some spacing
1494
+ st.markdown("---")
1495
+
1496
+ # Global chat input that works from any tab
1497
+ st.markdown("### πŸ’¬ Quick Chat (Available from any tab)")
1498
+
1499
+ # Use chat_input outside of tabs - this will work
1500
+ global_user_query = st.chat_input("Ask about your invoices from anywhere...")
1501
+
1502
+ if global_user_query:
1503
+ # Add user message
1504
+ st.session_state.chat_history.append({
1505
+ "role": "user",
1506
+ "content": global_user_query,
1507
+ "timestamp": datetime.now()
1508
+ })
1509
+
1510
+ # Get AI response
1511
+ with st.spinner("πŸ€– AI is analyzing..."):
1512
+ response = st.session_state.hf_chatbot.query_database(global_user_query)
1513
+
1514
+ st.session_state.chat_history.append({
1515
+ "role": "assistant",
1516
+ "content": response,
1517
+ "timestamp": datetime.now()
1518
+ })
1519
+
1520
+ # Show the response immediately
1521
+ with st.chat_message("assistant"):
1522
+ st.markdown(response)
1523
+
1524
+ # Suggest switching to chat tab if not already there
1525
+ st.info("πŸ’‘ Switch to the 'AI Chat' tab to see full conversation history!")
1526
+
1527
  # Footer
1528
  st.markdown("---")
1529
  st.markdown("""
 
1533
  </div>
1534
  """, unsafe_allow_html=True)
1535
 
1536
+ # ===============================================================================
1537
+ # MAIN APPLICATION ENTRY POINT
1538
+ # ===============================================================================
1539
+
1540
+ def main():
1541
+ """Main entry point for Hugging Face Spaces"""
1542
+ try:
1543
+ # Display Hugging Face info if running on HF Spaces
1544
+ if IS_HF_SPACE:
1545
+ st.sidebar.info("πŸ€— Running on Hugging Face Spaces")
1546
+
1547
+ # Create and run the app
1548
+ create_huggingface_app()
1549
+
1550
+ except Exception as e:
1551
+ st.error(f"Application error: {e}")
1552
+ st.info("Please refresh the page or contact support if the error persists.")
1553
+
1554
+ if __name__ == "__main__":
1555
+ main()
1556
+
1557
  # ===============================================================================
1558
  # HUGGING FACE REQUIREMENTS AND CONFIGURATION
1559
  # ===============================================================================