Anne31415 commited on
Commit
f6250a9
·
1 Parent(s): 8d2f0ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -73
app.py CHANGED
@@ -1,77 +1,177 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Define the HTML and CSS for the cloud-shaped button and text field
4
- cloud_button_html = """
5
- <style>
6
- .cloud-button {
7
- background-color: #FFCC99;
8
- border: none;
9
- color: white;
10
- padding: 10px 20px;
11
- text-align: center;
12
- text-decoration: none;
13
- display: inline-block;
14
- font-size: 16px;
15
- margin: 4px 2px;
16
- transition-duration: 0.4s;
17
- cursor: pointer;
18
- border-radius: 16px;
19
- box-shadow: 0 6px 6px 0 rgba(0, 0, 0, 0.24), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
20
- position: relative;
21
- }
22
-
23
- .cloud-button:after {
24
- content: "";
25
- position: absolute;
26
- top: 100%;
27
- left: 50%;
28
- border-width: 0 15px 10px 15px;
29
- border-style: solid;
30
- border-color: #FFCC99 transparent transparent transparent;
31
- margin-left: -15px;
32
- margin-top: -2px;
33
- }
34
-
35
- .text-field {
36
- border: none;
37
- background: transparent;
38
- color: #333;
39
- width: 100%;
40
- text-align: center;
41
- }
42
- </style>
43
- <div style="text-align: center; margin-top: 20px;">
44
- <button class="cloud-button" onclick="handleClick()">
45
- <input type="text" value="this is a text field" class="text-field" readonly />
46
- </button>
47
- </div>
48
- <script>
49
- function handleClick() {
50
- const outputDiv = document.getElementById('output');
51
- if(outputDiv) {
52
- outputDiv.innerText = 'success';
53
- }
54
- }
55
- </script>
56
- """
57
-
58
- # Render the cloud button with Streamlit
59
- st.markdown(cloud_button_html, unsafe_allow_html=True)
60
-
61
- # Placeholder for the output text
62
- output = st.empty()
63
-
64
- # Update the output text when the button is clicked
65
- if "button_clicked" not in st.session_state:
66
- st.session_state["button_clicked"] = False
67
-
68
- if st.session_state["button_clicked"]:
69
- output.text("success")
70
-
71
- # Button click handler
72
- with st.form(key="my_form"):
73
- submit_button = st.button("Click me!")
74
- if submit_button:
75
- st.session_state["button_clicked"] = True
76
 
 
 
 
 
 
 
 
 
 
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import streamlit_analytics
3
+ from dotenv import load_dotenv
4
+ import pickle
5
+ from huggingface_hub import Repository
6
+ from PyPDF2 import PdfReader
7
+ from streamlit_extras.add_vertical_space import add_vertical_space
8
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
9
+ from langchain.embeddings.openai import OpenAIEmbeddings
10
+ from langchain.vectorstores import FAISS
11
+ from langchain.llms import OpenAI
12
+ from langchain.chains.question_answering import load_qa_chain
13
+ from langchain.callbacks import get_openai_callback
14
+ import
15
 
16
+ streamlit_analytics.start_tracking()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Step 1: Clone the Dataset Repository
19
+ repo = Repository(
20
+ local_dir="Private_Book", # Local directory to clone the repository
21
+ repo_type="dataset", # Specify that this is a dataset repository
22
+
23
+ clone_from="Anne31415/Private_Book", # Replace with your repository URL
24
+
25
+ token=os.environ["HUB_TOKEN"] # Use the secret token to authenticate
26
+ )
27
+ repo.git_pull() # Pull the latest changes (if any)
28
 
29
+ # Step 2: Load the PDF File
30
+ pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
31
+
32
+ with st.sidebar:
33
+ st.title('BinDoc GmbH')
34
+ st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
35
+
36
+ add_vertical_space(1) # Adjust as per the desired spacing
37
+
38
+ st.markdown("""
39
+ Hello! I’m here to assist you with:<br><br>
40
+ 📘 **Glossary Inquiries:**<br>
41
+ I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
42
+ 🆘 **Help Page Navigation:**<br>
43
+ Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
44
+ 📰 **Latest Whitepapers Insights:**<br>
45
+ Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
46
+ """, unsafe_allow_html=True)
47
+
48
+ add_vertical_space(1) # Adjust as per the desired spacing
49
+
50
+ st.write('Made with ❤️ by BinDoc GmbH')
51
+
52
+ api_key = os.getenv("OPENAI_API_KEY")
53
+ # Retrieve the API key from st.secrets
54
+
55
+
56
+ def load_pdf(file_path):
57
+ pdf_reader = PdfReader(file_path)
58
+ text = ""
59
+ for page in pdf_reader.pages:
60
+ text += page.extract_text()
61
+
62
+ text_splitter = RecursiveCharacterTextSplitter(
63
+ chunk_size=1000,
64
+ chunk_overlap=200,
65
+ length_function=len
66
+ )
67
+ chunks = text_splitter.split_text(text=text)
68
+
69
+ store_name, _ = os.path.splitext(os.path.basename(file_path))
70
+
71
+ if os.path.exists(f"{store_name}.pkl"):
72
+ with open(f"{store_name}.pkl", "rb") as f:
73
+ VectorStore = pickle.load(f)
74
+ else:
75
+ embeddings = OpenAIEmbeddings()
76
+ VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
77
+ with open(f"{store_name}.pkl", "wb") as f:
78
+ pickle.dump(VectorStore, f)
79
+
80
+ return VectorStore
81
+
82
+
83
+
84
+ def load_chatbot():
85
+ return load_qa_chain(llm=OpenAI(), chain_type="stuff")
86
+
87
+ def main():
88
+
89
+ hide_streamlit_style = """
90
+ <style>
91
+ #MainMenu {visibility: hidden;}
92
+ footer {visibility: hidden;}
93
+ </style>
94
+ """
95
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
96
+
97
+
98
+ # Main content
99
+ st.title("Welcome to BinDocs ChatBot! 🤖")
100
+
101
+ # Directly specifying the path to the PDF file
102
+ pdf_path = pdf_file_path
103
+ if not os.path.exists(pdf_path):
104
+ st.error("File not found. Please check the file path.")
105
+ return
106
+
107
+ if "chat_history" not in st.session_state:
108
+ st.session_state['chat_history'] = []
109
+
110
+ display_chat_history(st.session_state['chat_history'])
111
+
112
+ st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
113
+ st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
114
+ st.write("<!-- End Spacer -->", unsafe_allow_html=True)
115
+
116
+ new_messages_placeholder = st.empty()
117
+
118
+ if pdf_path is not None:
119
+ query = st.text_input("Ask questions about your PDF file (in any preferred language):")
120
+
121
+ if st.button("Was genau ist ein Belegarzt?"):
122
+ query = "Was genau ist ein Belegarzt?"
123
+ if st.button("Wofür wird die Alpha-ID verwendet?"):
124
+ query = "Wofür wird die Alpha-ID verwendet?"
125
+ if st.button("Was sind die Vorteile des ambulanten operierens?"):
126
+ query = "Was sind die Vorteile des ambulanten operierens?"
127
+ if st.button("Was kann ich mit dem Prognose-Analyse Toll machen?"):
128
+ query = "Was kann ich mit dem Prognose-Analyse Toll machen?"
129
+ if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
130
+ query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
131
+ if st.button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?"):
132
+ query = ("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?")
133
+
134
+
135
+ if st.button("Ask") or (not st.session_state['chat_history'] and query) or (st.session_state['chat_history'] and query != st.session_state['chat_history'][-1][1]):
136
+ st.session_state['chat_history'].append(("User", query, "new"))
137
+
138
+ loading_message = st.empty()
139
+ loading_message.text('Bot is thinking...')
140
+
141
+ VectorStore = load_pdf(pdf_path)
142
+ chain = load_chatbot()
143
+ docs = VectorStore.similarity_search(query=query, k=3)
144
+ with get_openai_callback() as cb:
145
+ response = chain.run(input_documents=docs, question=query)
146
+
147
+ st.session_state['chat_history'].append(("Bot", response, "new"))
148
+
149
+ # Display new messages at the bottom
150
+ new_messages = st.session_state['chat_history'][-2:]
151
+ for chat in new_messages:
152
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
153
+ new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
154
+
155
+ # Scroll to the latest response using JavaScript
156
+ st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
157
+
158
+ loading_message.empty()
159
+
160
+ # Clear the input field by setting the query variable to an empty string
161
+ query = ""
162
+
163
+ # Mark all messages as old after displaying
164
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
165
+
166
+
167
+
168
+ def display_chat_history(chat_history):
169
+ for chat in chat_history:
170
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
171
+ st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
172
+
173
+ if __name__ == "__main__":
174
+ main()
175
+
176
+ streamlit_analytics.stop_tracking()
177
+ streamlit_analytics.track(unsafe_password="Anne31415")