PARTHA181098 commited on
Commit
f748655
·
verified ·
1 Parent(s): f7fc579

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +110 -26
src/streamlit_app.py CHANGED
@@ -1,16 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import uuid
3
  import json
4
  import asyncio
5
  import websockets
6
 
 
7
  MIDDLEWARE_WS = "wss://partha181098-middleware.hf.space/chat"
8
 
9
- st.set_page_config(page_title="Secure AI Chat")
10
 
11
  ss = st.session_state
12
 
13
- # ------------------ SESSION ------------------
14
  def new_session():
15
  ss.session_id = str(uuid.uuid4())
16
  ss.messages = []
@@ -21,59 +103,61 @@ if "session_id" not in ss:
21
  if "messages" not in ss:
22
  ss.messages = []
23
 
24
- # ------------------ UI ------------------
25
- st.title("🔐 Secure Database Chatbot")
26
 
27
  if st.button("New Chat"):
28
  new_session()
29
  st.rerun()
30
 
 
31
  for msg in ss.messages:
32
  with st.chat_message(msg["role"]):
33
- st.write(msg["content"])
34
 
35
  user_input = st.chat_input("Ask something...")
36
 
37
- # ------------------ WS CLIENT ------------------
38
- async def send_and_receive(user_text):
39
- async with websockets.connect(
40
- f"{MIDDLEWARE_WS}?session_id={ss.session_id}"
41
- ) as ws:
42
 
 
43
  await ws.send(user_text)
44
 
45
- assistant_msg = ""
46
 
47
  while True:
48
- data = await ws.recv()
49
- payload = json.loads(data)
50
 
51
  if payload["type"] == "security_warning":
52
  st.warning(payload["message"])
53
- return
54
 
55
  if payload["type"] == "response":
56
- assistant_msg += payload["content"]
57
- yield assistant_msg
58
 
59
  if payload["type"] == "complete":
60
- break
61
 
62
- # ------------------ CHAT FLOW ------------------
63
  if user_input:
 
64
  ss.messages.append({"role": "user", "content": user_input})
65
  with st.chat_message("user"):
66
- st.write(user_input)
67
 
68
  with st.chat_message("assistant"):
69
  placeholder = st.empty()
70
- full_response = {"text": ""}
71
 
72
- async def runner():
73
- async for chunk in send_and_receive(user_input):
74
- full_response["text"] = chunk
75
- placeholder.markdown(full_response["text"])
76
 
77
- asyncio.run(runner())
 
 
 
78
 
79
- ss.messages.append({"role": "assistant", "content": full_response})
 
1
+ # import streamlit as st
2
+ # import uuid
3
+ # import json
4
+ # import asyncio
5
+ # import websockets
6
+
7
+ # MIDDLEWARE_WS = "wss://partha181098-middleware.hf.space/chat"
8
+
9
+ # st.set_page_config(page_title="Secure AI Chat")
10
+
11
+ # ss = st.session_state
12
+
13
+ # # ------------------ SESSION ------------------
14
+ # def new_session():
15
+ # ss.session_id = str(uuid.uuid4())
16
+ # ss.messages = []
17
+
18
+ # if "session_id" not in ss:
19
+ # new_session()
20
+
21
+ # if "messages" not in ss:
22
+ # ss.messages = []
23
+
24
+ # # ------------------ UI ------------------
25
+ # st.title("🔐 Secure Database Chatbot")
26
+
27
+ # if st.button("New Chat"):
28
+ # new_session()
29
+ # st.rerun()
30
+
31
+ # for msg in ss.messages:
32
+ # with st.chat_message(msg["role"]):
33
+ # st.write(msg["content"])
34
+
35
+ # user_input = st.chat_input("Ask something...")
36
+
37
+ # # ------------------ WS CLIENT ------------------
38
+ # async def send_and_receive(user_text):
39
+ # async with websockets.connect(
40
+ # f"{MIDDLEWARE_WS}?session_id={ss.session_id}"
41
+ # ) as ws:
42
+
43
+ # await ws.send(user_text)
44
+
45
+ # assistant_msg = ""
46
+
47
+ # while True:
48
+ # data = await ws.recv()
49
+ # payload = json.loads(data)
50
+
51
+ # if payload["type"] == "security_warning":
52
+ # st.warning(payload["message"])
53
+ # return
54
+
55
+ # if payload["type"] == "response":
56
+ # assistant_msg += payload["content"]
57
+ # yield assistant_msg
58
+
59
+ # if payload["type"] == "complete":
60
+ # break
61
+
62
+ # # ------------------ CHAT FLOW ------------------
63
+ # if user_input:
64
+ # ss.messages.append({"role": "user", "content": user_input})
65
+ # with st.chat_message("user"):
66
+ # st.write(user_input)
67
+
68
+ # with st.chat_message("assistant"):
69
+ # placeholder = st.empty()
70
+ # full_response = {"text": ""}
71
+
72
+ # async def runner():
73
+ # async for chunk in send_and_receive(user_input):
74
+ # full_response["text"] = chunk
75
+ # placeholder.markdown(full_response["text"])
76
+
77
+ # asyncio.run(runner())
78
+
79
+ # ss.messages.append({"role": "assistant", "content": full_response})
80
+
81
+
82
  import streamlit as st
83
  import uuid
84
  import json
85
  import asyncio
86
  import websockets
87
 
88
+ # ================= CONFIG =================
89
  MIDDLEWARE_WS = "wss://partha181098-middleware.hf.space/chat"
90
 
91
+ st.set_page_config(page_title="Secure AI Chat", layout="centered")
92
 
93
  ss = st.session_state
94
 
95
+ # ================= SESSION =================
96
  def new_session():
97
  ss.session_id = str(uuid.uuid4())
98
  ss.messages = []
 
103
  if "messages" not in ss:
104
  ss.messages = []
105
 
106
+ # ================= UI =================
107
+ st.title("🔐 Secure AI Chatbot")
108
 
109
  if st.button("New Chat"):
110
  new_session()
111
  st.rerun()
112
 
113
+ # Show chat history
114
  for msg in ss.messages:
115
  with st.chat_message(msg["role"]):
116
+ st.markdown(msg["content"])
117
 
118
  user_input = st.chat_input("Ask something...")
119
 
120
+ # ================= STREAM HANDLER =================
121
+ async def ws_stream(user_text, placeholder):
122
+ uri = f"{MIDDLEWARE_WS}?session_id={ss.session_id}"
 
 
123
 
124
+ async with websockets.connect(uri) as ws:
125
  await ws.send(user_text)
126
 
127
+ full_response = ""
128
 
129
  while True:
130
+ raw = await ws.recv()
131
+ payload = json.loads(raw)
132
 
133
  if payload["type"] == "security_warning":
134
  st.warning(payload["message"])
135
+ return None
136
 
137
  if payload["type"] == "response":
138
+ full_response += payload["content"]
139
+ placeholder.markdown(full_response)
140
 
141
  if payload["type"] == "complete":
142
+ return full_response
143
 
144
+ # ================= CHAT FLOW =================
145
  if user_input:
146
+ # Store user message
147
  ss.messages.append({"role": "user", "content": user_input})
148
  with st.chat_message("user"):
149
+ st.markdown(user_input)
150
 
151
  with st.chat_message("assistant"):
152
  placeholder = st.empty()
 
153
 
154
+ # IMPORTANT: Streamlit-safe asyncio execution
155
+ response = asyncio.new_event_loop().run_until_complete(
156
+ ws_stream(user_input, placeholder)
157
+ )
158
 
159
+ if response:
160
+ ss.messages.append(
161
+ {"role": "assistant", "content": response}
162
+ )
163