devchavda11 commited on
Commit
5d6e27a
·
verified ·
1 Parent(s): 6298ad3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +30 -31
src/streamlit_app.py CHANGED
@@ -1,7 +1,6 @@
1
  import streamlit as st
2
  from chat_langraph import system, workflow, HumanMessage, AIMessage, get_all_chat_ids, ToolMessage
3
  import uuid
4
- import re
5
  import os
6
  import base64
7
 
@@ -9,8 +8,8 @@ import base64
9
  TEMP_DIR = "/tmp"
10
  os.makedirs(TEMP_DIR, exist_ok=True)
11
 
 
12
  st.title("MY CHATBOT")
13
- st.set_page_config(layout='wide')
14
 
15
 
16
  def set_title(messages):
@@ -20,7 +19,7 @@ def set_title(messages):
20
 
21
 
22
  def set_config():
23
- return {'configurable': {'thread_id': st.session_state.current_chat_id}}
24
 
25
 
26
  def load_session_state():
@@ -44,7 +43,7 @@ def render_sidebar():
44
  new_id = str(uuid.uuid4())
45
  st.session_state.chats.append(new_id)
46
  st.session_state.current_chat_id = new_id
47
- config = {'configurable': {'thread_id': new_id}}
48
  workflow.update_state(config, {"messages": [system]})
49
  st.session_state.chat_dict[new_id] = "New Chat"
50
  for chat_id in st.session_state.chats:
@@ -55,7 +54,7 @@ def render_sidebar():
55
  def loadchats():
56
  if "current_chat_id" not in st.session_state:
57
  return []
58
- config = {'configurable': {'thread_id': st.session_state.current_chat_id}}
59
  state = workflow.get_state(config)
60
  messages = state.values.get("messages", [])
61
  for message in messages:
@@ -68,8 +67,7 @@ def loadchats():
68
  with st.chat_message("assistant"):
69
  st.write(message.content)
70
  elif isinstance(message, ToolMessage):
71
- with st.chat_message("assistant"):
72
- st.info(f"Used a tool")
73
  return messages
74
 
75
 
@@ -84,13 +82,25 @@ def create_download_link(file_path: str, label: str = "Download file") -> str:
84
  return f"Error creating download link: {e}"
85
 
86
 
87
- def show_image_with_download(file_path: str):
88
- """Display image in chat and show a download button."""
89
- if os.path.exists(file_path) and file_path.endswith(".png"):
90
- st.image(file_path, caption=os.path.basename(file_path))
91
- st.markdown(create_download_link(file_path, "📥 Download Image"), unsafe_allow_html=True)
92
 
 
 
 
 
 
 
 
 
 
 
 
93
 
 
 
94
  load_session_state()
95
  render_sidebar()
96
 
@@ -104,26 +114,15 @@ if "current_chat_id" in st.session_state:
104
  with st.chat_message("assistant"):
105
  response_placeholder = st.empty()
106
  full_response = ""
107
- for messages, metadata in workflow.stream(
108
  {"messages": [system, HumanMessage(user_input)]},
109
- config={'configurable': {'thread_id': st.session_state.current_chat_id}},
110
- stream_mode="messages"
111
  ):
112
- if isinstance(messages, AIMessage) and messages.content.strip():
113
- full_response += messages.content
114
-
115
- if isinstance(messages, ToolMessage) and messages.content.strip():
116
- st.info(f"Using appropriate tool")
117
-
118
- # Detect generated PNG files and show + download them
119
- file_matches = re.findall(r"/tmp/[\w\.\-_]+\.png", messages.content)
120
- for file_path in file_matches:
121
- show_image_with_download(file_path)
122
-
123
- # Existing logic for "Content saved to"
124
- if "Content saved to" in messages.content:
125
- file_name = messages.content.replace("Content saved to ", "").strip()
126
- if os.path.exists(file_name):
127
- show_image_with_download(file_name)
128
 
129
  response_placeholder.markdown(full_response)
 
1
  import streamlit as st
2
  from chat_langraph import system, workflow, HumanMessage, AIMessage, get_all_chat_ids, ToolMessage
3
  import uuid
 
4
  import os
5
  import base64
6
 
 
8
  TEMP_DIR = "/tmp"
9
  os.makedirs(TEMP_DIR, exist_ok=True)
10
 
11
+ st.set_page_config(layout="wide")
12
  st.title("MY CHATBOT")
 
13
 
14
 
15
  def set_title(messages):
 
19
 
20
 
21
  def set_config():
22
+ return {"configurable": {"thread_id": st.session_state.current_chat_id}}
23
 
24
 
25
  def load_session_state():
 
43
  new_id = str(uuid.uuid4())
44
  st.session_state.chats.append(new_id)
45
  st.session_state.current_chat_id = new_id
46
+ config = {"configurable": {"thread_id": new_id}}
47
  workflow.update_state(config, {"messages": [system]})
48
  st.session_state.chat_dict[new_id] = "New Chat"
49
  for chat_id in st.session_state.chats:
 
54
  def loadchats():
55
  if "current_chat_id" not in st.session_state:
56
  return []
57
+ config = {"configurable": {"thread_id": st.session_state.current_chat_id}}
58
  state = workflow.get_state(config)
59
  messages = state.values.get("messages", [])
60
  for message in messages:
 
67
  with st.chat_message("assistant"):
68
  st.write(message.content)
69
  elif isinstance(message, ToolMessage):
70
+ render_tool_message(message)
 
71
  return messages
72
 
73
 
 
82
  return f"Error creating download link: {e}"
83
 
84
 
85
+ def render_tool_message(tool_message: ToolMessage):
86
+ """Render tool output including download links for any created files."""
87
+ with st.chat_message("assistant"):
88
+ st.info("Tool executed")
 
89
 
90
+ # If tool_message has an explicit "file" field in content/metadata
91
+ if hasattr(tool_message, "content") and isinstance(tool_message.content, dict):
92
+ file_paths = tool_message.content.get("files", [])
93
+ for file_path in file_paths:
94
+ if os.path.exists(file_path):
95
+ if file_path.lower().endswith((".png", ".jpg", ".jpeg")):
96
+ st.image(file_path, caption=os.path.basename(file_path))
97
+ st.markdown(
98
+ create_download_link(file_path, f"📥 Download {os.path.basename(file_path)}"),
99
+ unsafe_allow_html=True,
100
+ )
101
 
102
+
103
+ # --- Main App Logic ---
104
  load_session_state()
105
  render_sidebar()
106
 
 
114
  with st.chat_message("assistant"):
115
  response_placeholder = st.empty()
116
  full_response = ""
117
+ for message, metadata in workflow.stream(
118
  {"messages": [system, HumanMessage(user_input)]},
119
+ config={"configurable": {"thread_id": st.session_state.current_chat_id}},
120
+ stream_mode="messages",
121
  ):
122
+ if isinstance(message, AIMessage) and message.content.strip():
123
+ full_response += message.content
124
+
125
+ if isinstance(message, ToolMessage):
126
+ render_tool_message(message)
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  response_placeholder.markdown(full_response)