Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,7 +99,7 @@ class MultiConverter:
|
|
| 99 |
def convert_file(file):
|
| 100 |
"""Process uploaded file and convert it to text"""
|
| 101 |
if file is None:
|
| 102 |
-
return "No file uploaded. Please select a file first.",
|
| 103 |
|
| 104 |
try:
|
| 105 |
logging.info(f"Starting conversion for file: {file.name if hasattr(file, 'name') else 'unknown'}")
|
|
@@ -136,9 +136,9 @@ def convert_file(file):
|
|
| 136 |
with open(str(file), 'rb') as src, open(temp_file_path, 'wb') as dst:
|
| 137 |
dst.write(src.read())
|
| 138 |
except:
|
| 139 |
-
return f"Could not read file. Type: {type(file)}",
|
| 140 |
except Exception as e:
|
| 141 |
-
return f"Error reading file: {str(e)}",
|
| 142 |
|
| 143 |
# Okre艣l rozszerzenie pliku
|
| 144 |
_, file_ext = os.path.splitext(file_name)
|
|
@@ -162,18 +162,23 @@ def convert_file(file):
|
|
| 162 |
output_filename = os.path.splitext(file_name)[0] + ".txt"
|
| 163 |
|
| 164 |
# Przygotuj plik do pobrania
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
f.write(result)
|
| 168 |
|
| 169 |
-
# Przygotuj
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
return result,
|
| 174 |
except Exception as e:
|
| 175 |
logging.exception(f"Error converting file: {str(e)}")
|
| 176 |
-
return f"Error converting file: {str(e)}",
|
| 177 |
finally:
|
| 178 |
# Usu艅 pliki tymczasowe
|
| 179 |
try:
|
|
@@ -187,7 +192,7 @@ def convert_file(file):
|
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
logging.exception(f"Unexpected error: {str(e)}")
|
| 190 |
-
return f"Unexpected error: {str(e)}",
|
| 191 |
|
| 192 |
|
| 193 |
# Utw贸rz interfejs Gradio
|
|
@@ -205,7 +210,7 @@ with gr.Blocks(title="Multi-Format to TXT Converter") as app:
|
|
| 205 |
text_output = gr.Textbox(label="Converted Text", lines=15)
|
| 206 |
|
| 207 |
with gr.Row():
|
| 208 |
-
|
| 209 |
|
| 210 |
# Info about supported formats
|
| 211 |
gr.Markdown("""
|
|
@@ -219,14 +224,14 @@ with gr.Blocks(title="Multi-Format to TXT Converter") as app:
|
|
| 219 |
1. Upload a file using the file upload button
|
| 220 |
2. Click "Convert to TXT"
|
| 221 |
3. View the converted text
|
| 222 |
-
4.
|
| 223 |
""")
|
| 224 |
|
| 225 |
# Obs艂uga konwersji
|
| 226 |
convert_button.click(
|
| 227 |
fn=convert_file,
|
| 228 |
inputs=[file_input],
|
| 229 |
-
outputs=[text_output,
|
| 230 |
)
|
| 231 |
|
| 232 |
# Uruchom aplikacj臋
|
|
|
|
| 99 |
def convert_file(file):
|
| 100 |
"""Process uploaded file and convert it to text"""
|
| 101 |
if file is None:
|
| 102 |
+
return "No file uploaded. Please select a file first.", ""
|
| 103 |
|
| 104 |
try:
|
| 105 |
logging.info(f"Starting conversion for file: {file.name if hasattr(file, 'name') else 'unknown'}")
|
|
|
|
| 136 |
with open(str(file), 'rb') as src, open(temp_file_path, 'wb') as dst:
|
| 137 |
dst.write(src.read())
|
| 138 |
except:
|
| 139 |
+
return f"Could not read file. Type: {type(file)}", ""
|
| 140 |
except Exception as e:
|
| 141 |
+
return f"Error reading file: {str(e)}", ""
|
| 142 |
|
| 143 |
# Okre艣l rozszerzenie pliku
|
| 144 |
_, file_ext = os.path.splitext(file_name)
|
|
|
|
| 162 |
output_filename = os.path.splitext(file_name)[0] + ".txt"
|
| 163 |
|
| 164 |
# Przygotuj plik do pobrania
|
| 165 |
+
content_bytes = result.encode('utf-8')
|
| 166 |
+
b64 = base64.b64encode(content_bytes).decode()
|
|
|
|
| 167 |
|
| 168 |
+
# Przygotuj przycisk do pobrania
|
| 169 |
+
download_link = f"""
|
| 170 |
+
<a href="data:text/plain;base64,{b64}" download="{output_filename}"
|
| 171 |
+
style="display: inline-block; padding: 0.6em 1.2em; margin: 0.5em 0;
|
| 172 |
+
background-color: #4CAF50; color: white; border: none; border-radius: 4px;
|
| 173 |
+
cursor: pointer; text-decoration: none; font-weight: bold;">
|
| 174 |
+
猬囷笍 Download {output_filename}
|
| 175 |
+
</a>
|
| 176 |
+
"""
|
| 177 |
|
| 178 |
+
return result, download_link
|
| 179 |
except Exception as e:
|
| 180 |
logging.exception(f"Error converting file: {str(e)}")
|
| 181 |
+
return f"Error converting file: {str(e)}", ""
|
| 182 |
finally:
|
| 183 |
# Usu艅 pliki tymczasowe
|
| 184 |
try:
|
|
|
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
logging.exception(f"Unexpected error: {str(e)}")
|
| 195 |
+
return f"Unexpected error: {str(e)}", ""
|
| 196 |
|
| 197 |
|
| 198 |
# Utw贸rz interfejs Gradio
|
|
|
|
| 210 |
text_output = gr.Textbox(label="Converted Text", lines=15)
|
| 211 |
|
| 212 |
with gr.Row():
|
| 213 |
+
download_html = gr.HTML(label="Download")
|
| 214 |
|
| 215 |
# Info about supported formats
|
| 216 |
gr.Markdown("""
|
|
|
|
| 224 |
1. Upload a file using the file upload button
|
| 225 |
2. Click "Convert to TXT"
|
| 226 |
3. View the converted text
|
| 227 |
+
4. Click the download button to save the converted text file
|
| 228 |
""")
|
| 229 |
|
| 230 |
# Obs艂uga konwersji
|
| 231 |
convert_button.click(
|
| 232 |
fn=convert_file,
|
| 233 |
inputs=[file_input],
|
| 234 |
+
outputs=[text_output, download_html]
|
| 235 |
)
|
| 236 |
|
| 237 |
# Uruchom aplikacj臋
|