elshehawy commited on
Commit
9d35f9f
·
verified ·
1 Parent(s): 517602c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -39
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import os
2
  import json
3
- import time
4
  import requests
5
  import pandas as pd
6
  import streamlit as st
@@ -18,7 +17,7 @@ CHAT_URL = get_env_variable("CHAT_URL")
18
  # API interaction
19
  def fetch_results(query: str) -> list[dict]:
20
  """
21
- Send a query to the chat API and return the parsed JSON 'results' list.
22
  """
23
  payload = {"query": query}
24
  response = requests.post(
@@ -26,27 +25,24 @@ def fetch_results(query: str) -> list[dict]:
26
  headers={"Content-Type": "application/json"},
27
  data=json.dumps(payload),
28
  )
29
- # response.raise_for_status()
30
- response = response.json()
31
- return response
32
 
33
  # UI components
34
- def display_messages(history: list[dict]) -> None:
35
- """Render chat messages from history."""
36
- for msg in history:
37
- with st.chat_message(msg["role"]):
38
- st.markdown(msg["content"])
39
-
40
-
41
- def display_results_table(results: list[dict]) -> None:
42
  """Normalize and display API results as a Streamlit table."""
43
  if "results" not in results:
44
- st.info(results['detail'])
45
  return
46
  df = pd.json_normalize(results['results'])
47
- st.subheader("Results")
48
  st.dataframe(df)
49
 
 
 
 
 
 
 
 
50
 
51
  def main() -> None:
52
  st.title("BookDB 📚 🤖")
@@ -55,30 +51,26 @@ def main() -> None:
55
  if "messages" not in st.session_state:
56
  st.session_state.messages = []
57
 
58
- display_messages(st.session_state.messages)
59
-
60
- # User input
61
  query = st.chat_input("Ask about books...")
62
- if not query:
63
- return
64
-
65
- # Add and render user message
66
- user_msg = query.strip()
67
- st.session_state.messages.append({"role": "user", "content": user_msg})
68
- with st.chat_message("user"):
69
- st.markdown(user_msg)
70
-
71
- # Fetch and display results
72
- with st.chat_message("assistant"):
73
- results = fetch_results(user_msg)
74
- display_results_table(results)
75
-
76
- # Store assistant placeholder in history
77
- st.session_state.messages.append({
78
- "role": "assistant",
79
- "content": "Displayed results as a table."
80
- })
81
-
82
 
83
  if __name__ == "__main__":
84
- main()
 
1
  import os
2
  import json
 
3
  import requests
4
  import pandas as pd
5
  import streamlit as st
 
17
  # API interaction
18
  def fetch_results(query: str) -> list[dict]:
19
  """
20
+ Send a query to the chat API and return the parsed JSON response.
21
  """
22
  payload = {"query": query}
23
  response = requests.post(
 
25
  headers={"Content-Type": "application/json"},
26
  data=json.dumps(payload),
27
  )
28
+ return response.json()
 
 
29
 
30
  # UI components
31
+ def display_results_table(results: dict) -> None:
 
 
 
 
 
 
 
32
  """Normalize and display API results as a Streamlit table."""
33
  if "results" not in results:
34
+ st.info(results.get('detail', 'No results found.'))
35
  return
36
  df = pd.json_normalize(results['results'])
 
37
  st.dataframe(df)
38
 
39
+ def display_messages(history: list[dict]) -> None:
40
+ """Render chat messages from history including tables."""
41
+ for msg in history:
42
+ with st.chat_message(msg["role"]):
43
+ st.markdown(msg["content"])
44
+ if "data" in msg:
45
+ display_results_table(msg["data"])
46
 
47
  def main() -> None:
48
  st.title("BookDB 📚 🤖")
 
51
  if "messages" not in st.session_state:
52
  st.session_state.messages = []
53
 
54
+ # Get user input
 
 
55
  query = st.chat_input("Ask about books...")
56
+
57
+ if query:
58
+ # Add user message to history
59
+ st.session_state.messages.append({
60
+ "role": "user",
61
+ "content": query.strip()
62
+ })
63
+
64
+ # Fetch results and add assistant response
65
+ results = fetch_results(query)
66
+ st.session_state.messages.append({
67
+ "role": "assistant",
68
+ "content": "Here are the results:",
69
+ "data": results
70
+ })
71
+
72
+ # Display all messages
73
+ display_messages(st.session_state.messages)
 
 
74
 
75
  if __name__ == "__main__":
76
+ main()