Manojkumarpandi commited on
Commit
e54383e
Β·
verified Β·
1 Parent(s): 1323794

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -35
app.py CHANGED
@@ -4,25 +4,64 @@ from langchain.document_loaders import PyPDFDirectoryLoader
4
  from dotenv import load_dotenv
5
  import os
6
 
7
- # Load API keys from .env
8
  load_dotenv()
9
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
10
- # HF_API_KEY = os.getenv("HF_API_KEY") # Not used directly here, but loaded if needed
11
 
12
  # Page configuration
13
- st.set_page_config(page_title="Chat with PDFs", page_icon="πŸ“š")
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def initialize_session_state():
16
- session_vars = {
17
  "messages": [],
18
  "loaded_files": False,
19
  "pdf_content": None,
20
  "chat": None
21
  }
22
- for key, value in session_vars.items():
23
  if key not in st.session_state:
24
- st.session_state[key] = value
 
25
 
 
26
  def load_pdfs(folder_path):
27
  if not os.path.exists(folder_path):
28
  os.makedirs(folder_path)
@@ -30,64 +69,78 @@ def load_pdfs(folder_path):
30
  documents = loader.load()
31
  return "\n\n".join(doc.page_content for doc in documents)
32
 
 
 
33
  def initialize_chat(pdf_content):
34
  genai.configure(api_key=GOOGLE_API_KEY)
35
- generation_config = {
36
  "temperature": 0.7,
37
  "top_p": 0.95,
38
  "top_k": 40,
39
  "max_output_tokens": 8192,
40
  }
41
 
42
- model = genai.GenerativeModel(
43
- model_name="gemini-1.5-pro",
44
- generation_config=generation_config,
45
- )
46
 
47
- context_prompt = f"""You are a helpful assistant. Use the following PDF content to answer user questions:
48
  {pdf_content}
49
- If the answer isn't in the content, reply accordingly."""
50
 
51
  chat = model.start_chat(history=[])
52
- chat.send_message(context_prompt)
53
  return chat
54
 
 
 
55
  def main():
56
  initialize_session_state()
57
- st.title("πŸ’¬ Chat with PDFs")
 
 
58
 
59
  with st.sidebar:
60
- st.header("Upload PDFs")
61
- uploaded_files = st.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
 
 
 
 
 
62
 
63
  if uploaded_files and not st.session_state.loaded_files:
64
- if not os.path.exists("pdfs"):
65
- os.makedirs("pdfs")
66
- for file in os.listdir("pdfs"):
67
- os.remove(os.path.join("pdfs", file))
68
  for file in uploaded_files:
69
- with open(f"pdfs/{file.name}", "wb") as f:
70
- f.write(file.getvalue())
71
 
72
- with st.spinner("Processing PDFs..."):
73
  try:
74
  pdf_text = load_pdfs("pdfs")
75
  st.session_state.pdf_content = pdf_text
76
- st.session_state.loaded_files = True
77
  st.session_state.chat = initialize_chat(pdf_text)
 
 
78
  except Exception as e:
79
- st.error(f"Error loading PDFs: {str(e)}")
80
- return
 
 
 
 
81
 
 
82
  if st.session_state.loaded_files:
83
  for msg in st.session_state.messages:
 
84
  with st.chat_message(msg["role"]):
85
- st.markdown(msg["content"])
86
 
87
- if prompt := st.chat_input("Ask a question about your PDFs:"):
88
  st.session_state.messages.append({"role": "user", "content": prompt})
89
  with st.chat_message("user"):
90
- st.markdown(prompt)
91
 
92
  with st.chat_message("assistant"):
93
  placeholder = st.empty()
@@ -95,13 +148,13 @@ def main():
95
  if not st.session_state.chat:
96
  st.session_state.chat = initialize_chat(st.session_state.pdf_content)
97
  response = st.session_state.chat.send_message(prompt)
98
- response_text = response.text
99
- placeholder.markdown(response_text)
100
- st.session_state.messages.append({"role": "assistant", "content": response_text})
101
  except Exception as e:
102
- placeholder.error(f"Error generating response: {str(e)}")
103
  else:
104
- st.info("Please upload PDFs to begin.")
105
 
106
  if __name__ == "__main__":
107
  main()
 
4
  from dotenv import load_dotenv
5
  import os
6
 
7
+ # Load API keys
8
  load_dotenv()
9
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
 
10
 
11
  # Page configuration
12
+ st.set_page_config(
13
+ page_title="πŸ“š Chat with PDFs",
14
+ page_icon="πŸ“„",
15
+ layout="wide"
16
+ )
17
 
18
+ # Custom CSS styling
19
+ st.markdown("""
20
+ <style>
21
+ .main {
22
+ background-color: #f7f9fc;
23
+ padding: 20px;
24
+ }
25
+ .stChatMessage {
26
+ background-color: #ffffff;
27
+ border-radius: 1rem;
28
+ padding: 15px;
29
+ margin: 10px 0;
30
+ box-shadow: 0 2px 8px rgba(0,0,0,0.05);
31
+ }
32
+ .user-message {
33
+ background-color: #e0f7fa;
34
+ border-left: 5px solid #00796b;
35
+ }
36
+ .assistant-message {
37
+ background-color: #fff8e1;
38
+ border-left: 5px solid #fbc02d;
39
+ }
40
+ .sidebar .stButton button {
41
+ width: 100%;
42
+ }
43
+ .uploaded-files {
44
+ font-size: 0.9rem;
45
+ color: #666;
46
+ }
47
+ </style>
48
+ """, unsafe_allow_html=True)
49
+
50
+
51
+ # Initialize session variables
52
  def initialize_session_state():
53
+ defaults = {
54
  "messages": [],
55
  "loaded_files": False,
56
  "pdf_content": None,
57
  "chat": None
58
  }
59
+ for key, val in defaults.items():
60
  if key not in st.session_state:
61
+ st.session_state[key] = val
62
+
63
 
64
+ # Load PDFs
65
  def load_pdfs(folder_path):
66
  if not os.path.exists(folder_path):
67
  os.makedirs(folder_path)
 
69
  documents = loader.load()
70
  return "\n\n".join(doc.page_content for doc in documents)
71
 
72
+
73
+ # Initialize Gemini chat
74
  def initialize_chat(pdf_content):
75
  genai.configure(api_key=GOOGLE_API_KEY)
76
+ config = {
77
  "temperature": 0.7,
78
  "top_p": 0.95,
79
  "top_k": 40,
80
  "max_output_tokens": 8192,
81
  }
82
 
83
+ model = genai.GenerativeModel("gemini-1.5-pro", generation_config=config)
 
 
 
84
 
85
+ prompt = f"""You are a helpful assistant. Use the following PDF content to answer questions:
86
  {pdf_content}
87
+ If the answer is not in the content, kindly let the user know."""
88
 
89
  chat = model.start_chat(history=[])
90
+ chat.send_message(prompt)
91
  return chat
92
 
93
+
94
+ # Main application
95
  def main():
96
  initialize_session_state()
97
+
98
+ st.title("πŸ“˜ Chat with Your PDF Files")
99
+ st.caption("Built with πŸ’‘ Gemini AI & LangChain")
100
 
101
  with st.sidebar:
102
+ st.header("πŸ“ Upload PDFs")
103
+ uploaded_files = st.file_uploader(
104
+ "Drag and drop PDFs here",
105
+ type=["pdf"],
106
+ accept_multiple_files=True,
107
+ help="You can upload multiple PDFs"
108
+ )
109
 
110
  if uploaded_files and not st.session_state.loaded_files:
111
+ os.makedirs("pdfs", exist_ok=True)
112
+ for f in os.listdir("pdfs"):
113
+ os.remove(os.path.join("pdfs", f))
 
114
  for file in uploaded_files:
115
+ with open(f"pdfs/{file.name}", "wb") as f_out:
116
+ f_out.write(file.getvalue())
117
 
118
+ with st.spinner("πŸ” Processing PDFs..."):
119
  try:
120
  pdf_text = load_pdfs("pdfs")
121
  st.session_state.pdf_content = pdf_text
 
122
  st.session_state.chat = initialize_chat(pdf_text)
123
+ st.session_state.loaded_files = True
124
+ st.success("PDFs processed and ready to chat!")
125
  except Exception as e:
126
+ st.error(f"Failed to process PDFs: {str(e)}")
127
+
128
+ if st.session_state.loaded_files:
129
+ st.subheader("πŸ“„ Files Uploaded")
130
+ for file in uploaded_files:
131
+ st.markdown(f"- {file.name}", unsafe_allow_html=True)
132
 
133
+ # Chat section
134
  if st.session_state.loaded_files:
135
  for msg in st.session_state.messages:
136
+ css_class = "user-message" if msg["role"] == "user" else "assistant-message"
137
  with st.chat_message(msg["role"]):
138
+ st.markdown(f"<div class='stChatMessage {css_class}'>{msg['content']}</div>", unsafe_allow_html=True)
139
 
140
+ if prompt := st.chat_input("Ask something about the PDFs..."):
141
  st.session_state.messages.append({"role": "user", "content": prompt})
142
  with st.chat_message("user"):
143
+ st.markdown(f"<div class='stChatMessage user-message'>{prompt}</div>", unsafe_allow_html=True)
144
 
145
  with st.chat_message("assistant"):
146
  placeholder = st.empty()
 
148
  if not st.session_state.chat:
149
  st.session_state.chat = initialize_chat(st.session_state.pdf_content)
150
  response = st.session_state.chat.send_message(prompt)
151
+ reply = response.text
152
+ placeholder.markdown(f"<div class='stChatMessage assistant-message'>{reply}</div>", unsafe_allow_html=True)
153
+ st.session_state.messages.append({"role": "assistant", "content": reply})
154
  except Exception as e:
155
+ placeholder.error(f"⚠️ Error generating response: {str(e)}")
156
  else:
157
+ st.info("πŸ“€ Upload your PDF documents from the sidebar to begin chatting.")
158
 
159
  if __name__ == "__main__":
160
  main()