avimittal30 commited on
Commit
a0dc409
·
verified ·
1 Parent(s): 1b7e795

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -16
app.py CHANGED
@@ -76,21 +76,48 @@ vector_store = None
76
  chain = None
77
  chat_history = []
78
 
79
- # Function to handle file uploads
80
  def upload_file(files):
81
- for file in files:
82
- file_path = os.path.join("documents", os.path.basename(file.name))
83
- with open(file_path, "wb") as f:
84
- f.write(file.read())
85
-
86
- global vector_store, chain
87
- vector_store = process_documents()
88
- chain = create_chain(vector_store)
89
-
90
- if chain is None:
91
- return "Files uploaded and processed, but HuggingFace API token is missing. Set the environment variable to enable the chatbot."
92
-
93
- return "Files uploaded and processed successfully!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  # Function to handle user queries
96
  def chat(message, history):
@@ -127,7 +154,7 @@ def chat(message, history):
127
  error_message = f"Error processing your request: {str(e)}"
128
  return history + [[message, error_message]]
129
 
130
- # Create Gradio interface
131
  with gr.Blocks(title="RAG Chatbot") as demo:
132
  gr.Markdown("# RAG-based Conversational Chatbot")
133
  gr.Markdown("Upload text documents and chat with an AI that can answer questions based on their content.")
@@ -137,7 +164,8 @@ with gr.Blocks(title="RAG Chatbot") as demo:
137
  file_output = gr.Textbox(label="Upload Status")
138
  file_input = gr.File(
139
  file_count="multiple",
140
- label="Upload Documents (.txt files)"
 
141
  )
142
  upload_button = gr.Button("Process Documents")
143
  upload_button.click(upload_file, inputs=[file_input], outputs=[file_output])
 
76
  chain = None
77
  chat_history = []
78
 
79
+ # Function to handle file uploads - FIXED to handle Gradio's file objects properly
80
  def upload_file(files):
81
+ try:
82
+ # Clear existing documents if we're uploading new ones
83
+ for f in os.listdir("documents"):
84
+ file_path = os.path.join("documents", f)
85
+ if os.path.isfile(file_path):
86
+ os.remove(file_path)
87
+
88
+ # Handle the uploaded files
89
+ for file in files:
90
+ # Get file content and name
91
+ if hasattr(file, "name"): # For standard file objects
92
+ file_name = os.path.basename(file.name)
93
+ if hasattr(file, "read"):
94
+ content = file.read()
95
+ else: # For NamedString objects
96
+ content = file.decode('utf-8') if isinstance(file, bytes) else str(file)
97
+ else: # For tuple format (file_name, file_data)
98
+ if isinstance(file, tuple) and len(file) >= 2:
99
+ file_name = os.path.basename(file[0])
100
+ content = file[1]
101
+ else:
102
+ # If none of the above, try to handle as string with a default name
103
+ file_name = f"document_{len(os.listdir('documents'))}.txt"
104
+ content = str(file)
105
+
106
+ # Write content to file
107
+ file_path = os.path.join("documents", file_name)
108
+ with open(file_path, "w", encoding='utf-8') as f:
109
+ f.write(content if isinstance(content, str) else content.decode('utf-8'))
110
+
111
+ global vector_store, chain
112
+ vector_store = process_documents()
113
+ chain = create_chain(vector_store)
114
+
115
+ if chain is None:
116
+ return "Files uploaded and processed, but HuggingFace API token is missing. Set the environment variable to enable the chatbot."
117
+
118
+ return "Files uploaded and processed successfully!"
119
+ except Exception as e:
120
+ return f"Error processing files: {str(e)}"
121
 
122
  # Function to handle user queries
123
  def chat(message, history):
 
154
  error_message = f"Error processing your request: {str(e)}"
155
  return history + [[message, error_message]]
156
 
157
+ # Create Gradio interface - UPDATED to use a simpler file upload approach
158
  with gr.Blocks(title="RAG Chatbot") as demo:
159
  gr.Markdown("# RAG-based Conversational Chatbot")
160
  gr.Markdown("Upload text documents and chat with an AI that can answer questions based on their content.")
 
164
  file_output = gr.Textbox(label="Upload Status")
165
  file_input = gr.File(
166
  file_count="multiple",
167
+ label="Upload Documents (.txt files)",
168
+ type="text" # Specifying text type for proper handling
169
  )
170
  upload_button = gr.Button("Process Documents")
171
  upload_button.click(upload_file, inputs=[file_input], outputs=[file_output])