Pavan178 commited on
Commit
a6d253a
·
verified ·
1 Parent(s): b04938d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import gradio as gr
 
3
  from langchain.document_loaders import PyPDFLoader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain.embeddings import OpenAIEmbeddings
@@ -7,6 +8,8 @@ from langchain.vectorstores import FAISS
7
  from langchain.chains import ConversationalRetrievalChain
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain.memory import ConversationBufferMemory
 
 
10
 
11
  from langchain.prompts import PromptTemplate
12
 
@@ -103,10 +106,25 @@ NOTE : DESCRIBE/SUMMARY should always return the overall summary of the document
103
  # Initialize the chatbot
104
  pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
105
 
 
 
 
 
 
 
 
 
 
 
106
  def upload_pdf(pdf_file):
107
  if pdf_file is None:
108
  return "Please upload a PDF file."
109
- file_path = pdf_file.name
 
 
 
 
 
110
  pdf_chatbot.load_and_process_pdf(file_path)
111
  return file_path
112
 
@@ -120,13 +138,16 @@ def clear_chatbot():
120
  return []
121
 
122
  def get_pdf_path():
123
- # Call the method to return the current PDF path
124
  return pdf_chatbot.get_pdf_path()
125
 
126
  # Create the Gradio interface
127
  with gr.Blocks() as demo:
128
  gr.Markdown("# PDF Chatbot")
129
 
 
 
 
 
130
  with gr.Row():
131
  pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
132
  upload_button = gr.Button("Process PDF")
@@ -143,5 +164,7 @@ with gr.Blocks() as demo:
143
  clear.click(clear_chatbot, outputs=[chatbot_interface])
144
  path_button.click(get_pdf_path, outputs=[pdf_path_display])
145
 
 
 
146
  if __name__ == "__main__":
147
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ from huggingface_hub import HfApi, whoami
4
  from langchain.document_loaders import PyPDFLoader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.embeddings import OpenAIEmbeddings
 
8
  from langchain.chains import ConversationalRetrievalChain
9
  from langchain.chat_models import ChatOpenAI
10
  from langchain.memory import ConversationBufferMemory
11
+ from langchain.prompts import PromptTemplate
12
+
13
 
14
  from langchain.prompts import PromptTemplate
15
 
 
106
  # Initialize the chatbot
107
  pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
108
 
109
+ def get_user_folder():
110
+ try:
111
+ user_info = whoami()
112
+ username = user_info['name']
113
+ user_folder = f"user_data/{username}"
114
+ os.makedirs(user_folder, exist_ok=True)
115
+ return user_folder
116
+ except Exception:
117
+ return None
118
+
119
  def upload_pdf(pdf_file):
120
  if pdf_file is None:
121
  return "Please upload a PDF file."
122
+ user_folder = get_user_folder()
123
+ if user_folder is None:
124
+ return "Please log in to upload a PDF."
125
+ file_path = os.path.join(user_folder, pdf_file.name)
126
+ with open(file_path, "wb") as f:
127
+ f.write(pdf_file.read())
128
  pdf_chatbot.load_and_process_pdf(file_path)
129
  return file_path
130
 
 
138
  return []
139
 
140
  def get_pdf_path():
 
141
  return pdf_chatbot.get_pdf_path()
142
 
143
  # Create the Gradio interface
144
  with gr.Blocks() as demo:
145
  gr.Markdown("# PDF Chatbot")
146
 
147
+ with gr.Row():
148
+ login_button = gr.LoginButton()
149
+ user_info = gr.Markdown()
150
+
151
  with gr.Row():
152
  pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
153
  upload_button = gr.Button("Process PDF")
 
164
  clear.click(clear_chatbot, outputs=[chatbot_interface])
165
  path_button.click(get_pdf_path, outputs=[pdf_path_display])
166
 
167
+ demo.load(lambda: gr.update(visible=True), outputs=[user_info], inputs=None)
168
+
169
  if __name__ == "__main__":
170
  demo.launch()