Update app.py
Browse files
app.py
CHANGED
|
@@ -129,29 +129,48 @@ def extract_text_from_file(file_path):
|
|
| 129 |
return text
|
| 130 |
|
| 131 |
def upload_file(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
if file is None:
|
| 133 |
return "No file uploaded!"
|
| 134 |
|
|
|
|
| 135 |
if isinstance(file, list):
|
| 136 |
file = file[0]
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
|
|
|
| 140 |
file_data = file.read()
|
|
|
|
| 141 |
elif isinstance(file, dict):
|
|
|
|
| 142 |
file_name = file.get("name", "uploaded_file")
|
| 143 |
file_data = file.get("data")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
else:
|
| 145 |
return "Uploaded file format not recognized."
|
| 146 |
|
|
|
|
| 147 |
if file_data is None:
|
| 148 |
return "Uploaded file data not found!"
|
| 149 |
|
| 150 |
-
|
| 151 |
if not os.path.exists("new_file"):
|
| 152 |
os.makedirs("new_file")
|
| 153 |
|
| 154 |
-
|
| 155 |
file_path = os.path.join("new_file", file_name)
|
| 156 |
try:
|
| 157 |
with open(file_path, "wb") as f:
|
|
@@ -159,27 +178,13 @@ def upload_file(file):
|
|
| 159 |
except Exception as e:
|
| 160 |
return f"Error saving file: {e}"
|
| 161 |
|
| 162 |
-
|
| 163 |
extracted_text = extract_text_from_file(file_path)
|
| 164 |
|
| 165 |
-
|
| 166 |
preview = extracted_text[:200] + "..." if len(extracted_text) > 200 else extracted_text
|
| 167 |
return f"File {file_name} uploaded and processed successfully!\nExtracted text preview:\n{preview}"
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
file_path = os.path.join("new_file", file_name)
|
| 172 |
-
if hasattr(file, "read"):
|
| 173 |
-
content = file.read()
|
| 174 |
-
elif isinstance(file, dict) and "data" in file:
|
| 175 |
-
content = file["data"]
|
| 176 |
-
else:
|
| 177 |
-
return "Uploaded file format not recognized."
|
| 178 |
-
|
| 179 |
-
with open(file_path, "wb") as f:
|
| 180 |
-
f.write(content)
|
| 181 |
-
|
| 182 |
-
return f"File {file_name} uploaded successfully!"
|
| 183 |
|
| 184 |
|
| 185 |
def gradio_chatbot():
|
|
|
|
| 129 |
return text
|
| 130 |
|
| 131 |
def upload_file(file):
|
| 132 |
+
"""
|
| 133 |
+
Handles file upload from Gradio.
|
| 134 |
+
Saves the file to the "new_file" directory and extracts text content based on file type.
|
| 135 |
+
Supports file-like objects, dictionaries, or file paths.
|
| 136 |
+
"""
|
| 137 |
+
# Check if a file was uploaded
|
| 138 |
if file is None:
|
| 139 |
return "No file uploaded!"
|
| 140 |
|
| 141 |
+
# If file is a list (multiple files), take the first one
|
| 142 |
if isinstance(file, list):
|
| 143 |
file = file[0]
|
| 144 |
|
| 145 |
+
# Initialize file_name and file_data based on the type of 'file'
|
| 146 |
+
if hasattr(file, 'read'):
|
| 147 |
+
# file is a file-like object
|
| 148 |
file_data = file.read()
|
| 149 |
+
file_name = getattr(file, 'name', "uploaded_file")
|
| 150 |
elif isinstance(file, dict):
|
| 151 |
+
# file is a dictionary with "name" and "data" keys
|
| 152 |
file_name = file.get("name", "uploaded_file")
|
| 153 |
file_data = file.get("data")
|
| 154 |
+
elif isinstance(file, str):
|
| 155 |
+
# file is a string (e.g., a NamedString representing a file path)
|
| 156 |
+
file_name = os.path.basename(file)
|
| 157 |
+
try:
|
| 158 |
+
with open(file, "rb") as f:
|
| 159 |
+
file_data = f.read()
|
| 160 |
+
except Exception as e:
|
| 161 |
+
return f"Error reading file from path: {e}"
|
| 162 |
else:
|
| 163 |
return "Uploaded file format not recognized."
|
| 164 |
|
| 165 |
+
# Validate that file_data is available
|
| 166 |
if file_data is None:
|
| 167 |
return "Uploaded file data not found!"
|
| 168 |
|
| 169 |
+
# Ensure the "new_file" directory exists
|
| 170 |
if not os.path.exists("new_file"):
|
| 171 |
os.makedirs("new_file")
|
| 172 |
|
| 173 |
+
# Save the file to the "new_file" directory
|
| 174 |
file_path = os.path.join("new_file", file_name)
|
| 175 |
try:
|
| 176 |
with open(file_path, "wb") as f:
|
|
|
|
| 178 |
except Exception as e:
|
| 179 |
return f"Error saving file: {e}"
|
| 180 |
|
| 181 |
+
# Extract text from the file for further processing
|
| 182 |
extracted_text = extract_text_from_file(file_path)
|
| 183 |
|
| 184 |
+
# Create a preview of the extracted text
|
| 185 |
preview = extracted_text[:200] + "..." if len(extracted_text) > 200 else extracted_text
|
| 186 |
return f"File {file_name} uploaded and processed successfully!\nExtracted text preview:\n{preview}"
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
def gradio_chatbot():
|