Spaces:
Running
Running
fix app for latest gradio release
Browse files
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
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
else:
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
if ext == ".pdf":
|
| 93 |
-
cleaned_text = extract_and_clean_pdf(
|
|
|
|
| 94 |
elif ext == ".docx":
|
| 95 |
-
cleaned_text = extract_and_clean_docx(
|
|
|
|
| 96 |
else:
|
| 97 |
-
cleaned_text = extract_and_clean_txt(
|
| 98 |
|
| 99 |
if not output_name.lower().endswith(".md"):
|
| 100 |
output_name += ".md"
|
| 101 |
|
| 102 |
-
|
| 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)
|