anasmkh commited on
Commit
5f94cf5
·
verified ·
1 Parent(s): 008970e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -77,13 +77,37 @@ def clear_history():
77
  return [], ""
78
 
79
  def upload_file(file):
80
- # Save the uploaded file to the "new_file" directory
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  if not os.path.exists("new_file"):
82
  os.makedirs("new_file")
83
- file_path = os.path.join("new_file", file)
 
 
 
 
 
 
 
 
 
84
  with open(file_path, "wb") as f:
85
- f.write(file.read())
86
- return f"File {file} uploaded successfully!"
 
 
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: