Generative-Subodh commited on
Commit
a0432be
·
verified ·
1 Parent(s): 38477da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -47
app.py CHANGED
@@ -1,17 +1,20 @@
1
  import streamlit as st
2
  from llama_index.core import VectorStoreIndex, Document
3
- from llama_index.llms import OpenAI
4
  import os
5
  import pdfplumber
6
  from docx import Document as DocxDocument
7
  from dotenv import load_dotenv
8
  import json
 
 
9
  load_dotenv()
10
 
11
- st.header("Chat")
12
 
 
13
  if 'openai_api_key' not in st.session_state:
14
- st.session_state.openai_api_key = ""
15
 
16
  # Input for OpenAI API Key
17
  st.session_state.openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password",
@@ -21,32 +24,29 @@ st.session_state.openai_api_key = st.sidebar.text_input("Enter your OpenAI API K
21
  if 'phone_number' not in st.session_state:
22
  st.session_state.phone_number = ""
23
 
24
- st.session_state.phone_number = st.sidebar.text_input("Enter your phone number: ")
25
-
26
- # Instructions
27
- if 'messages' not in st.session_state:
28
- st.session_state.messages = [
29
- {"role": "assistant", "content": "Ask me a question about the docs you uploaded!"},
30
- ]
31
 
 
 
 
 
 
32
 
 
33
  def read_pdf(file):
34
- with pdfplumber.open(file) as pdf:
35
- text = ''
36
- for page in pdf.pages:
37
- text += page.extract_text + '\n'
38
-
39
- return text
40
-
41
 
 
42
  def read_docx(file):
43
- doc = DocxDocument(file)
44
- text = ''
45
-
46
- for paragraph in doc.paragraphs:
47
- text += paragraph.text + '\n'
48
-
49
- return text
50
 
51
  @st.cache_resource(show_spinner=False)
52
  def load_data(uploaded_files):
@@ -66,21 +66,18 @@ def load_data(uploaded_files):
66
  index = VectorStoreIndex.from_documents(docs, settings=llm)
67
  return index
68
 
 
69
  def save_conversation():
70
- if st.session_state.phone_number:
71
-
72
- conversation_entry = {
73
- "phone_number": st.session_state.phone_number,
74
- "messages": st.session_state.messages,
75
- }
76
-
77
- with open("conversation.json", "a") as f:
78
- json.dump(conversation_entry, f) # dump means adding
79
- f.write("\n")
80
-
81
- else:
82
- st.warning("Please enter phone number before saving conversation")
83
-
84
 
85
  # Function to load previous conversations based on phone number
86
  def load_conversations(phone_number):
@@ -101,15 +98,14 @@ def load_conversations(phone_number):
101
  return conversations
102
  return []
103
 
104
-
105
  def delete_selected_conversations(selected_indices, phone_number):
106
- conversations = load_conversations(phone_number)
107
- remaining_conversation = [conv for i, conv in enumerate(conversations) if i not in selected_indices]
108
-
109
- with open("conversatinons.json", "w") as f:
110
- for conv in remaining_conversations:
111
- json.dump(conv, f)
112
- f.write("\n")
113
 
114
  # File uploader for multiple PDF and DOCX files
115
  uploaded_files = st.file_uploader("Upload PDF or DOCX files", type=["pdf", "docx"], accept_multiple_files=True)
@@ -188,4 +184,4 @@ if st.session_state.show_conversations and st.session_state.phone_number:
188
  else:
189
  st.sidebar.write("No previous conversations found for this phone number.")
190
  else:
191
- st.sidebar.write("Previous conversations are hidden. Enter your phone number to view them.")
 
1
  import streamlit as st
2
  from llama_index.core import VectorStoreIndex, Document
3
+ from llama_index.llms.openai import OpenAI
4
  import os
5
  import pdfplumber
6
  from docx import Document as DocxDocument
7
  from dotenv import load_dotenv
8
  import json
9
+
10
+ # Load environment variables from .env file
11
  load_dotenv()
12
 
13
+ st.header("Chat with the uploaded docs 💬 📚")
14
 
15
+ # Sidebar for OpenAI API Key
16
  if 'openai_api_key' not in st.session_state:
17
+ st.session_state.openai_api_key = ""
18
 
19
  # Input for OpenAI API Key
20
  st.session_state.openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password",
 
24
  if 'phone_number' not in st.session_state:
25
  st.session_state.phone_number = ""
26
 
27
+ st.session_state.phone_number = st.sidebar.text_input("Enter your phone number:")
 
 
 
 
 
 
28
 
29
+ # Initialize session state for messages
30
+ if "messages" not in st.session_state:
31
+ st.session_state.messages = [
32
+ {"role": "assistant", "content": "Ask me a question about the documents you uploaded!"}
33
+ ]
34
 
35
+ # Function to read PDF files
36
  def read_pdf(file):
37
+ with pdfplumber.open(file) as pdf:
38
+ text = ''
39
+ for page in pdf.pages:
40
+ text += page.extract_text() + '\n'
41
+ return text
 
 
42
 
43
+ # Function to read DOCX files
44
  def read_docx(file):
45
+ doc = DocxDocument(file)
46
+ text = ''
47
+ for paragraph in doc.paragraphs:
48
+ text += paragraph.text + '\n'
49
+ return text
 
 
50
 
51
  @st.cache_resource(show_spinner=False)
52
  def load_data(uploaded_files):
 
66
  index = VectorStoreIndex.from_documents(docs, settings=llm)
67
  return index
68
 
69
+ # Function to save the conversation
70
  def save_conversation():
71
+ if st.session_state.phone_number:
72
+ conversation_entry = {
73
+ "phone_number": st.session_state.phone_number,
74
+ "messages": st.session_state.messages
75
+ }
76
+ with open("conversations.json", "a") as f:
77
+ json.dump(conversation_entry, f)
78
+ f.write("\n")
79
+ else:
80
+ st.warning("Please enter a phone number before saving the conversation.")
 
 
 
 
81
 
82
  # Function to load previous conversations based on phone number
83
  def load_conversations(phone_number):
 
98
  return conversations
99
  return []
100
 
101
+ # Function to delete selected conversations
102
  def delete_selected_conversations(selected_indices, phone_number):
103
+ conversations = load_conversations(phone_number)
104
+ remaining_conversations = [conv for i, conv in enumerate(conversations) if i not in selected_indices]
105
+ with open("conversations.json", "w") as f:
106
+ for conv in remaining_conversations:
107
+ json.dump(conv, f)
108
+ f.write("\n")
 
109
 
110
  # File uploader for multiple PDF and DOCX files
111
  uploaded_files = st.file_uploader("Upload PDF or DOCX files", type=["pdf", "docx"], accept_multiple_files=True)
 
184
  else:
185
  st.sidebar.write("No previous conversations found for this phone number.")
186
  else:
187
+ st.sidebar.write("Previous conversations are hidden. Enter your phone number to view them.")