gnumanth commited on
Commit
7f5bd14
·
verified ·
1 Parent(s): 5b6b75b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -19,33 +19,27 @@ def process_with_markitdown(input_path):
19
  return result.text_content
20
  except Exception as e:
21
  return f"Error processing input: {str(e)}"
22
-
23
  def save_uploaded_file(file_data):
24
- """Saves a file uploaded with gardio to a temporary location."""
25
 
26
  if file_data is None:
27
  return "No file uploaded."
28
 
29
  try:
30
  temp_dir = tempfile.gettempdir()
31
-
32
- # Critical: Handle the different types from gardio
33
- if hasattr(file_data, 'filename') and hasattr(file_data, 'content'):
34
- temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file_data.filename)[1])[1]
35
- with open(temp_filename, 'wb') as f:
36
- f.write(file_data.content)
37
- return temp_filename
38
- elif hasattr(file_data, 'read'):
39
- temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file_data.filename)[1])[1] if hasattr(file_data, 'filename') else tempfile.mkstemp(dir=temp_dir)
40
- with open(temp_filename, 'wb') as f:
41
- while True:
42
- chunk = file_data.read(8192) # Crucial: Chunk
43
- if not chunk:
44
- break
45
- f.write(chunk)
46
- return temp_filename
47
  else:
48
- return "Error: Invalid file data object."
 
 
 
 
 
 
 
49
  except Exception as e:
50
  return f"An error occurred during file saving: {str(e)}"
51
 
 
19
  return result.text_content
20
  except Exception as e:
21
  return f"Error processing input: {str(e)}"
22
+
23
  def save_uploaded_file(file_data):
24
+ """Saves a file to a temporary location."""
25
 
26
  if file_data is None:
27
  return "No file uploaded."
28
 
29
  try:
30
  temp_dir = tempfile.gettempdir()
31
+ # Important: Extract the filename.
32
+ if file_data.name:
33
+ temp_filename = os.path.join(temp_dir, file_data.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
+ return "Error: File has no name."
36
+
37
+
38
+ with open(temp_filename, 'wb') as f:
39
+ f.write(file_data.file.read())
40
+
41
+
42
+ return temp_filename
43
  except Exception as e:
44
  return f"An error occurred during file saving: {str(e)}"
45