Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- .gitignore +1 -0
- LICENSE.txt +21 -0
- README.md +64 -13
- VirtualBrainGPT.py +51 -0
- diary/diary_journal.txt +29 -0
- pages/Brain_Entry.py +116 -0
- pages/Brain_Search.py +102 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Bilal Kaludi
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,13 +1,64 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# VirtualBrainGPT: Digital Journal π
|
3 |
+
|
4 |
+
VirtualBrainGPT is a digital journal application that leverages the power of OpenAI's Embeddings API and Langchain to create a seamless and efficient journaling experience. With the capability to search and extract information from journal entries in seconds, VirtualBrainGPT becomes an indispensable tool for users who want to recall specific details from their past entries.
|
5 |
+
|
6 |
+
# Web App
|
7 |
+
Click [Here](https://huggingface.co/spaces/Kaludi/ChatGPT-BingChat-GPT3-Prompt-Generator_App "Here") To View This Application Online!
|
8 |
+
|
9 |
+
![VirtualBrainGPT](https://github.com/add/your-image-url-here.jpg)
|
10 |
+
|
11 |
+
## Features
|
12 |
+
|
13 |
+
- Create and manage journal entries using a clean interface.
|
14 |
+
- Search and extract information from journal entries quickly and accurately.
|
15 |
+
- Support for both TXT and PDF file formats.
|
16 |
+
- Powered by OpenAI's Embeddings API and Langchain for efficient information retrieval.
|
17 |
+
|
18 |
+
## Usage
|
19 |
+
|
20 |
+
### Brain Entry
|
21 |
+
|
22 |
+
In the 'Brain Entry' section, users can create a new journal entry or edit an existing one by choosing a date using the date picker. Once you have completed your entry, click 'Submit' and it will be saved or updated to the diary_journal.txt file.
|
23 |
+
|
24 |
+
### Brain Search
|
25 |
+
|
26 |
+
The 'Brain Search' section of the application unlocks the full potential of your digital journal. Here, you can ask any questions related to your journal entries, and the combination of OpenAI's Embeddings API and Langchain will provide accurate responses in seconds, no matter how long the entry or document may be. The virtual brain can help in all aspects of a user's life, enabling users to easily recall specific information, even if they can't remember it themselves. Users also have an option to select other file types if they wish to upload their own file, the current file types include TXT and PDF files, which can be selected in the file type dropdown option.
|
27 |
+
|
28 |
+
## Examples of Use-Cases
|
29 |
+
|
30 |
+
This application can be used in many ways. Imagine five years from now, you're trying to recollect a specific event that you documented in your journal, or you may have kept a journal from your childhood. VirtualBrainGPT will locate the exact date and provide you with a detailed account of that exact situation in a matter of seconds, as well as any other memories that you may have forgotten about. This type of application also could offer invaluable support for individuals with Alzheimer's, helping them retrieve memories from the past that may have been lost and share them with their loved ones.
|
31 |
+
|
32 |
+
## Tools & Libraries Used
|
33 |
+
|
34 |
+
- OpenAI [Embeddings](https://platform.openai.com/docs/guides/embeddings)
|
35 |
+
- [LangChain](https://python.langchain.com/en/latest/use_cases/question_answering.html)
|
36 |
+
- [Streamlit](https://streamlit.io/)
|
37 |
+
|
38 |
+
## Installation
|
39 |
+
|
40 |
+
To install VirtualBrainGPT, you need to have Python 3.7+ installed. Follow these steps to install the necessary dependencies:
|
41 |
+
|
42 |
+
1. Clone this repository:
|
43 |
+
|
44 |
+
`git clone https://github.com/Kaludii/VirtualBrainGPT.git`
|
45 |
+
|
46 |
+
2. Change directory to the cloned repository:
|
47 |
+
|
48 |
+
`cd VirtualBrainGPT`
|
49 |
+
|
50 |
+
3. Install the required packages:
|
51 |
+
|
52 |
+
`pip install -r requirements.txt`
|
53 |
+
|
54 |
+
4. Run the Streamlit application:
|
55 |
+
|
56 |
+
`streamlit run VirtualBrainGPT.py`
|
57 |
+
|
58 |
+
## About the Developer
|
59 |
+
|
60 |
+
This application was developed by [Kaludii](https://github.com/Kaludii) using the the different tools and libraries linked above. Kaludii is an AI enthusiast who is passionate about developing and applying large learning models to solve real-world problems quickly and stress-free.
|
61 |
+
|
62 |
+
## Contributions
|
63 |
+
|
64 |
+
If you have any suggestions or improvements for this project, feel free to open an issue or submit a pull request. Your contributions are always welcome!
|
VirtualBrainGPT.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import datetime
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
st.set_page_config(
|
6 |
+
page_title="VirtualBrainGPT",
|
7 |
+
page_icon="π",
|
8 |
+
)
|
9 |
+
hide_streamlit_style = """
|
10 |
+
<style>
|
11 |
+
footer {visibility: hidden;}
|
12 |
+
</style>
|
13 |
+
"""
|
14 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
15 |
+
st.write("# VirtualBrainGPT: Digital Journal π")
|
16 |
+
|
17 |
+
st.sidebar.success("Select either the Brain Entry or Brain Search from above.")
|
18 |
+
|
19 |
+
st.markdown(
|
20 |
+
"""
|
21 |
+
This application uses the Streamlit interface to take journaling to a whole new level.
|
22 |
+
Welcome to VirtualBrainGPT, an application that streamlines the process of creating and managing personal journal entries. By leveraging the power of OpenAI's Embeddings API and Langchain, this application not only simplifies journaling but also offers a new way to effortly and immediately search between multiple different journal entries, no matter how many or how long they may be.
|
23 |
+
|
24 |
+
To begin, simply select either **'Brain Entry'** or **'Brain Search'** from the **sidebar menu**! π
|
25 |
+
|
26 |
+
### Brain Entry
|
27 |
+
In the 'Brain Entry' section, users can create a new journal entry or edit an existing one by choosing a date using the date picker. Once you have completed your entry, click 'Submit' and it will be saved or updated to the diary_journal.txt file.
|
28 |
+
|
29 |
+
### Brain Search
|
30 |
+
The 'Brain Search' section of the application unlocks the full potential of your digital journal. Here, you can ask any questions related to your journal entries, and the combination of OpenAI's Embeddings API and Langchain will provide accurate responses in seconds, no matter how long the entry or document may be. The virtual brain can help in all aspects of a user's life, enabling users to easily recall specific information, even if they can't remember it themselves. Users also have an option to select other file types if they wish to upload their own file, the current file types include TXT and PDF files, which can be selected in the file type dropdown option.
|
31 |
+
|
32 |
+
### Tools Used
|
33 |
+
- OpenAI [Embeddings](https://platform.openai.com/docs/guides/embeddings)
|
34 |
+
- [LangChain](https://python.langchain.com/en/latest/use_cases/question_answering.html)
|
35 |
+
- [Streamlit](https://streamlit.io/)
|
36 |
+
|
37 |
+
### Application Info
|
38 |
+
|
39 |
+
- **GitHub Application & Documentation:** [https://github.com/Kaludii/VirtualBrainGPT](https://github.com/Kaludii/VirtualBrainGPT)
|
40 |
+
|
41 |
+
- **Web Application:** [huggingface.co/add](https://huggingface.co/add)
|
42 |
+
|
43 |
+
## About the Developer
|
44 |
+
This application was developed by [Kaludii](https://github.com/Kaludii) using the the different tools and libraries linked above. Kaludii is an AI enthusiast who is passionate about developing and applying large learning models to solve real-world problems quickly and stress-free.
|
45 |
+
|
46 |
+
"""
|
47 |
+
)
|
48 |
+
|
49 |
+
st.markdown("---")
|
50 |
+
st.markdown("")
|
51 |
+
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
diary/diary_journal.txt
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Date: 04-24-2023 Monday
|
2 |
+
Today was a rollercoaster of emotions. As we enter the last week of senior year, it's finally hitting me that college is coming to an end. I spent most of the day working on my final project for the Capstone course. I can't believe I'm just a few days away from presenting it. In the evening, I met up with my friends for a study session, but we mostly spent the time reminiscing and laughing about our college adventures.
|
3 |
+
|
4 |
+
Date: 04-25-2023 Tuesday
|
5 |
+
Another hectic day filled with finalizing projects and preparing for exams. I received an email from the Career Services office informing me of a potential job opportunity after graduation. The thought of starting my career is both exciting and nerve-wracking. I spent the evening with my roommate, talking about our post-graduation plans and the challenges we'll face in the "real world."
|
6 |
+
|
7 |
+
Date: 04-26-2023 Wednesday
|
8 |
+
Today was the last day of classes! It's such a surreal feeling. My professors shared some words of wisdom and encouragement, which was very touching. After class, I attended a senior send-off event on campus, where we were given our caps and gowns. As we tried them on, the reality of graduating really set in. We took lots of photos to commemorate the moment.
|
9 |
+
|
10 |
+
Date: 04-27-2023 Thursday
|
11 |
+
It's officially finals week. I spent most of my day in the library, studying and finishing up my final papers. My friends and I took a break in the afternoon to grab some ice cream and enjoy the sunshine. We discussed our plans for the upcoming summer and our future careers. The conversation was bittersweet, as we know that we'll soon be parting ways and entering new chapters in our lives.
|
12 |
+
|
13 |
+
Date: 04-28-2023 Friday
|
14 |
+
I had my first final exam this morning. I think it went well, but I'm just relieved it's over. I spent the rest of the day preparing for my Capstone presentation. I'm feeling pretty confident about it, and I can't wait to show my professor and classmates what I've been working on all semester. In the evening, I attended a pre-graduation party with my friends. We danced and sang, embracing every moment together.
|
15 |
+
|
16 |
+
Date: 04-29-2023 Saturday
|
17 |
+
Today was all about relaxation and self-care. I spent the morning catching up on sleep, then went for a run around campus to clear my head. In the afternoon, I attended a yoga class with my friends to destress before our final exams next week. It's important to find balance during these stressful times.
|
18 |
+
|
19 |
+
Date: 04-30-2023 Sunday
|
20 |
+
I'm in full-on study mode for my last two exams. The library was packed, but I managed to find a quiet spot to focus. I took a break in the evening to watch a movie with my roommates. It's our tradition to have a movie night every Sunday, and tonight's choice was a nostalgic throwback to our freshman year.
|
21 |
+
|
22 |
+
Date: 05-01-2023 Monday
|
23 |
+
One more exam down, just one more to go! After finishing today's exam, I met up with my Capstone group to finalize our presentation for tomorrow. We practiced our parts and made last-minute adjustments to our slides. We're all feeling confident and ready to present.
|
24 |
+
|
25 |
+
Date: 05-02-2023 Tuesday
|
26 |
+
We finally presented our Capstone project! It was a huge success, and our professor praised our hard work and dedication. It was such a rewarding feeling to see our project come together after months of effort. In the evening, I attended the Senior Awards Ceremony, where several of my friends received well-deserved awards for their achievements.
|
27 |
+
|
28 |
+
Date: 05-03-2023 Wednesday
|
29 |
+
I completed my final exam today! It's hard to believe that my college journey is coming to an end. To celebrate, my friends and I went out for dinner at our favorite restaurant, reminiscing about our college experiences and sharing our hopes for the future. After dinner, we took a long walk around campus, stopping at all the memorable spots and taking pictures to capture these precious moments. As we walked, we couldn't help but feel a mix of happiness, sadness, and anticipation for what lies ahead.
|
pages/Brain_Entry.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import datetime
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
hide_streamlit_style = """
|
6 |
+
<style>
|
7 |
+
footer {visibility: hidden;}
|
8 |
+
</style>
|
9 |
+
"""
|
10 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
11 |
+
|
12 |
+
# Create a 'diary' folder in the current directory if it doesn't exist
|
13 |
+
if not os.path.exists("diary"):
|
14 |
+
os.makedirs("diary")
|
15 |
+
|
16 |
+
# Name of the single journal file
|
17 |
+
journal_file = "diary/diary_journal.txt"
|
18 |
+
|
19 |
+
def parse_date(date_string):
|
20 |
+
try:
|
21 |
+
return datetime.datetime.strptime(date_string, "%m-%d-%Y %A")
|
22 |
+
except ValueError:
|
23 |
+
return datetime.datetime.strptime(date_string, "%m-%d-%Y")
|
24 |
+
|
25 |
+
def get_journal_entries():
|
26 |
+
entries = []
|
27 |
+
if not os.path.exists(journal_file):
|
28 |
+
return entries
|
29 |
+
|
30 |
+
with open(journal_file, "r", encoding="utf-8") as f:
|
31 |
+
for line in f:
|
32 |
+
if line.startswith("Date: "):
|
33 |
+
entry_date = parse_date(line[6:].strip())
|
34 |
+
entries.append(entry_date)
|
35 |
+
entries.sort(reverse=True)
|
36 |
+
return entries
|
37 |
+
|
38 |
+
def read_entry(date):
|
39 |
+
content = ""
|
40 |
+
with open(journal_file, "r", encoding="utf-8") as f:
|
41 |
+
lines = f.readlines()
|
42 |
+
|
43 |
+
start_reading = False
|
44 |
+
for line in lines:
|
45 |
+
if line.startswith("Date: ") and start_reading:
|
46 |
+
break
|
47 |
+
|
48 |
+
if start_reading:
|
49 |
+
content += line
|
50 |
+
|
51 |
+
if line.startswith("Date: ") and date == parse_date(line[6:].strip()):
|
52 |
+
start_reading = True
|
53 |
+
|
54 |
+
return content
|
55 |
+
|
56 |
+
def write_entry(date, content):
|
57 |
+
new_entry = f"\nDate: {date}\n{content}\n"
|
58 |
+
|
59 |
+
# Check if the entry already exists
|
60 |
+
entry_exists = False
|
61 |
+
if os.path.exists(journal_file):
|
62 |
+
with open(journal_file, "r", encoding="utf-8") as f:
|
63 |
+
lines = f.readlines()
|
64 |
+
entry_exists = any(line.strip() == f"Date: {date}" for line in lines)
|
65 |
+
|
66 |
+
# If the entry does not exist, append the new entry to the end of the file
|
67 |
+
if not entry_exists:
|
68 |
+
with open(journal_file, "a", encoding="utf-8") as f:
|
69 |
+
f.write(new_entry)
|
70 |
+
else:
|
71 |
+
# If the entry exists, update the existing entry
|
72 |
+
with open(journal_file, "r", encoding="utf-8") as f:
|
73 |
+
lines = f.readlines()
|
74 |
+
|
75 |
+
# Remove existing entry if present
|
76 |
+
lines = [line for line in lines if line.strip() != f"Date: {date}"]
|
77 |
+
|
78 |
+
with open(journal_file, "w", encoding="utf-8") as f:
|
79 |
+
f.writelines(lines)
|
80 |
+
f.write(new_entry)
|
81 |
+
|
82 |
+
st.title("Digital Brain Journal Entry βοΈ")
|
83 |
+
st.write("Write a diary journal entry or edit an existing one by selecting on the date picker.")
|
84 |
+
|
85 |
+
selected_date = st.date_input("Select the date for the journal entry:", value=datetime.date.today())
|
86 |
+
formatted_date = selected_date.strftime("%m-%d-%Y %A")
|
87 |
+
st.write(f"Selected date: {formatted_date}")
|
88 |
+
|
89 |
+
entry = ""
|
90 |
+
|
91 |
+
if selected_date in get_journal_entries():
|
92 |
+
entry = read_entry(selected_date)
|
93 |
+
|
94 |
+
new_entry = st.text_area("Write your journal entry:", entry)
|
95 |
+
|
96 |
+
if st.button("Submit"):
|
97 |
+
write_entry(formatted_date, new_entry)
|
98 |
+
st.success("Journal entry saved successfully!")
|
99 |
+
|
100 |
+
st.header("Previous Journal Entries")
|
101 |
+
entries = get_journal_entries()
|
102 |
+
|
103 |
+
if entries:
|
104 |
+
selected_entry_date = st.selectbox("Select an entry to view or edit:", entries, format_func=lambda x: x.strftime("%m-%d-%Y %A"))
|
105 |
+
|
106 |
+
if st.button("Load Entry"):
|
107 |
+
entry_text = read_entry(selected_entry_date)
|
108 |
+
st.write(f"**{selected_entry_date.strftime('%m-%d-%Y %A')}**")
|
109 |
+
st.markdown(entry_text.replace("\n", "<br>"), unsafe_allow_html=True)
|
110 |
+
|
111 |
+
else:
|
112 |
+
st.write("No previous entries found.")
|
113 |
+
|
114 |
+
st.markdown("---")
|
115 |
+
st.markdown("")
|
116 |
+
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
pages/Brain_Search.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.chains.question_answering import load_qa_chain
|
9 |
+
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
10 |
+
from langchain.llms import OpenAI
|
11 |
+
from langchain.callbacks import get_openai_callback
|
12 |
+
|
13 |
+
def extract_text_from_pdf(pdf):
|
14 |
+
pdf_reader = PdfReader(pdf)
|
15 |
+
text = ""
|
16 |
+
for page in pdf_reader.pages:
|
17 |
+
text += page.extract_text()
|
18 |
+
return text
|
19 |
+
|
20 |
+
|
21 |
+
def extract_text_from_txt(txt):
|
22 |
+
text = txt.read().decode("utf-8")
|
23 |
+
return text
|
24 |
+
|
25 |
+
|
26 |
+
def extract_text_from_diary():
|
27 |
+
with open('diary/diary_journal.txt', 'r', encoding='utf-8') as file:
|
28 |
+
text = file.read()
|
29 |
+
return text
|
30 |
+
|
31 |
+
|
32 |
+
def main():
|
33 |
+
load_dotenv()
|
34 |
+
hide_streamlit_style = """
|
35 |
+
<style>
|
36 |
+
footer {visibility: hidden;}
|
37 |
+
</style>
|
38 |
+
"""
|
39 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
40 |
+
st.title("Digital Brain Journal Search π")
|
41 |
+
st.write("Ask any questions about all the journal entries with OpenAI's Embeddings API and Langchain. The virtual brain keeps track of everything in a user's life. If you have another TXT or PDF file you'd like to search for answers, click on the dropdown and select eithter TXT or PDF option in file type.")
|
42 |
+
|
43 |
+
# Add API key input
|
44 |
+
api_key = st.text_input("Enter your API key:", type="password")
|
45 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
46 |
+
|
47 |
+
if not api_key:
|
48 |
+
st.warning("Please enter your OpenAI API key to continue.")
|
49 |
+
else:
|
50 |
+
file_type = st.selectbox("Choose the file type", options=["Diary", "PDF", "TXT"])
|
51 |
+
|
52 |
+
file = None
|
53 |
+
text = None
|
54 |
+
|
55 |
+
if file_type == "PDF":
|
56 |
+
file = st.file_uploader("Upload your PDF", type="pdf")
|
57 |
+
if file is not None:
|
58 |
+
text = extract_text_from_pdf(file)
|
59 |
+
elif file_type == "TXT":
|
60 |
+
file = st.file_uploader("Upload your TXT", type="txt")
|
61 |
+
if file is not None:
|
62 |
+
text = extract_text_from_txt(file)
|
63 |
+
elif file_type == "Diary":
|
64 |
+
text = extract_text_from_diary()
|
65 |
+
|
66 |
+
if file is not None or file_type == "Diary":
|
67 |
+
# split into chunks
|
68 |
+
text_splitter = CharacterTextSplitter(
|
69 |
+
separator="\n",
|
70 |
+
chunk_size=1000,
|
71 |
+
chunk_overlap=200,
|
72 |
+
length_function=len
|
73 |
+
)
|
74 |
+
chunks = text_splitter.split_text(text)
|
75 |
+
|
76 |
+
# create embeddings
|
77 |
+
embeddings = OpenAIEmbeddings()
|
78 |
+
knowledge_base = FAISS.from_texts(chunks, embeddings)
|
79 |
+
|
80 |
+
# show user input
|
81 |
+
user_question = st.text_input("Ask a question about your document:")
|
82 |
+
|
83 |
+
if st.button("Submit"):
|
84 |
+
if user_question:
|
85 |
+
docs = knowledge_base.similarity_search(user_question)
|
86 |
+
|
87 |
+
llm = OpenAI()
|
88 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
89 |
+
with get_openai_callback() as cb:
|
90 |
+
response = chain.run(input_documents=docs, question=user_question)
|
91 |
+
print(cb)
|
92 |
+
|
93 |
+
st.markdown("### Response:")
|
94 |
+
st.write(response)
|
95 |
+
st.write(cb)
|
96 |
+
st.markdown("---")
|
97 |
+
st.markdown("")
|
98 |
+
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
99 |
+
|
100 |
+
|
101 |
+
if __name__ == '__main__':
|
102 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.154
|
2 |
+
PyPDF2==3.0.1
|
3 |
+
streamlit==1.18.1
|