lsottani commited on
Commit
d973914
·
verified ·
1 Parent(s): fb9658c

fix app for latest gradio release

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -76,31 +76,40 @@ def process_file(input_file, output_name):
76
 
77
  if input_file is None:
78
  return None
79
-
80
- if isinstance(input_file, list):
81
- input_path = input_file[0]
 
 
 
 
 
82
  else:
83
- input_path = input_file
 
84
 
85
- if hasattr(input_path, "name"):
86
- input_path = input_path.name
 
87
 
88
- input_path = str(input_path)
 
89
 
90
- _, ext = os.path.splitext(input_path.lower())
91
 
92
  if ext == ".pdf":
93
- cleaned_text = extract_and_clean_pdf(input_path)
 
94
  elif ext == ".docx":
95
- cleaned_text = extract_and_clean_docx(input_path)
 
96
  else:
97
- cleaned_text = extract_and_clean_txt(input_path)
98
 
99
  if not output_name.lower().endswith(".md"):
100
  output_name += ".md"
101
 
102
- temp_dir = tempfile.mkdtemp()
103
- out_path = os.path.join(temp_dir, output_name)
104
 
105
  with open(out_path, "w", encoding="utf-8") as f:
106
  f.write(cleaned_text)
 
76
 
77
  if input_file is None:
78
  return None
79
+
80
+ if hasattr(input_file, "read"):
81
+ data = input_file.read()
82
+ filename = input_file.name
83
+ elif isinstance(input_file, str):
84
+ filename = input_file
85
+ with open(input_file, "rb") as f:
86
+ data = f.read()
87
  else:
88
+ filename = input_file[0].name
89
+ data = input_file[0].read()
90
 
91
+ # écrire dans /tmp (important sur HF Spaces)
92
+ suffix = os.path.splitext(filename)[1]
93
+ tmp_path = os.path.join(tempfile.gettempdir(), "upload" + suffix)
94
 
95
+ with open(tmp_path, "wb") as f:
96
+ f.write(data)
97
 
98
+ ext = suffix.lower()
99
 
100
  if ext == ".pdf":
101
+ cleaned_text = extract_and_clean_pdf(tmp_path)
102
+
103
  elif ext == ".docx":
104
+ cleaned_text = extract_and_clean_docx(tmp_path)
105
+
106
  else:
107
+ cleaned_text = extract_and_clean_txt(tmp_path)
108
 
109
  if not output_name.lower().endswith(".md"):
110
  output_name += ".md"
111
 
112
+ out_path = os.path.join(tempfile.gettempdir(), output_name)
 
113
 
114
  with open(out_path, "w", encoding="utf-8") as f:
115
  f.write(cleaned_text)