Update app.py
Browse files
app.py
CHANGED
|
@@ -77,13 +77,37 @@ def clear_history():
|
|
| 77 |
return [], ""
|
| 78 |
|
| 79 |
def upload_file(file):
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if not os.path.exists("new_file"):
|
| 82 |
os.makedirs("new_file")
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with open(file_path, "wb") as f:
|
| 85 |
-
f.write(
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def gradio_chatbot():
|
| 89 |
with gr.Blocks() as demo:
|
|
|
|
| 77 |
return [], ""
|
| 78 |
|
| 79 |
def upload_file(file):
|
| 80 |
+
if file is None:
|
| 81 |
+
return "No file uploaded!"
|
| 82 |
+
|
| 83 |
+
if isinstance(file, list):
|
| 84 |
+
file = file[0]
|
| 85 |
+
|
| 86 |
+
if hasattr(file, 'name'):
|
| 87 |
+
file_name = file.name
|
| 88 |
+
elif isinstance(file, dict):
|
| 89 |
+
file_name = file.get("name", "uploaded_file")
|
| 90 |
+
else:
|
| 91 |
+
file_name = "uploaded_file"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
if not os.path.exists("new_file"):
|
| 95 |
os.makedirs("new_file")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
file_path = os.path.join("new_file", file_name)
|
| 99 |
+
if hasattr(file, "read"):
|
| 100 |
+
content = file.read()
|
| 101 |
+
elif isinstance(file, dict) and "data" in file:
|
| 102 |
+
content = file["data"]
|
| 103 |
+
else:
|
| 104 |
+
return "Uploaded file format not recognized."
|
| 105 |
+
|
| 106 |
with open(file_path, "wb") as f:
|
| 107 |
+
f.write(content)
|
| 108 |
+
|
| 109 |
+
return f"File {file_name} uploaded successfully!"
|
| 110 |
+
|
| 111 |
|
| 112 |
def gradio_chatbot():
|
| 113 |
with gr.Blocks() as demo:
|