JBHF commited on
Commit
f3467c1
1 Parent(s): 206c4b5

Create app_BACKUP.py

Browse files
Files changed (1) hide show
  1. app_BACKUP.py +72 -0
app_BACKUP.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app_BACKUP.py - STREAMLIT APP "Literature Based Research v0.0 (A. Unzicker and J. Bours)"
2
+ # app.py (ChatPDF)
3
+ # https://github.com/vndee/local-rag-example/blob/main/app.py
4
+
5
+ import os
6
+ import tempfile
7
+ import streamlit as st
8
+ from streamlit_chat import message
9
+ # https://pypi.org/project/streamlit-chat/
10
+ # from streamlit-chat import message
11
+ from rag import ChatPDF
12
+
13
+ st.set_page_config(page_title="ChatPDF")
14
+
15
+
16
+ def display_messages():
17
+ st.subheader("Chat")
18
+ for i, (msg, is_user) in enumerate(st.session_state["messages"]):
19
+ message(msg, is_user=is_user, key=str(i))
20
+ st.session_state["thinking_spinner"] = st.empty()
21
+
22
+
23
+ def process_input():
24
+ if st.session_state["user_input"] and len(st.session_state["user_input"].strip()) > 0:
25
+ user_text = st.session_state["user_input"].strip()
26
+ with st.session_state["thinking_spinner"], st.spinner(f"Thinking"):
27
+ agent_text = st.session_state["assistant"].ask(user_text)
28
+
29
+ st.session_state["messages"].append((user_text, True))
30
+ st.session_state["messages"].append((agent_text, False))
31
+
32
+
33
+ def read_and_save_file():
34
+ st.session_state["assistant"].clear()
35
+ st.session_state["messages"] = []
36
+ st.session_state["user_input"] = ""
37
+
38
+ for file in st.session_state["file_uploader"]:
39
+ with tempfile.NamedTemporaryFile(delete=False) as tf:
40
+ tf.write(file.getbuffer())
41
+ file_path = tf.name
42
+
43
+ with st.session_state["ingestion_spinner"], st.spinner(f"Ingesting {file.name}"):
44
+ st.session_state["assistant"].ingest(file_path)
45
+ os.remove(file_path)
46
+
47
+
48
+ def page():
49
+ if len(st.session_state) == 0:
50
+ st.session_state["messages"] = []
51
+ st.session_state["assistant"] = ChatPDF()
52
+
53
+ st.header("ChatPDF")
54
+
55
+ st.subheader("Upload a document")
56
+ st.file_uploader(
57
+ "Upload document",
58
+ type=["pdf"],
59
+ key="file_uploader",
60
+ on_change=read_and_save_file,
61
+ label_visibility="collapsed",
62
+ accept_multiple_files=True,
63
+ )
64
+
65
+ st.session_state["ingestion_spinner"] = st.empty()
66
+
67
+ display_messages()
68
+ st.text_input("Message", key="user_input", on_change=process_input)
69
+
70
+
71
+ if __name__ == "__main__":
72
+ page()