Spaces:
Runtime error
Runtime error
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,30 +1,36 @@
|
|
| 1 |
# api.py
|
| 2 |
-
from flask import Flask, request, jsonify
|
| 3 |
import os
|
| 4 |
import zipfile
|
| 5 |
import rarfile
|
| 6 |
from PyPDF2 import PdfReader, PdfWriter
|
| 7 |
-
from moviepy.editor import VideoFileClip
|
| 8 |
from pydub import AudioSegment
|
| 9 |
from PIL import Image
|
| 10 |
import pdf2image
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
def convert_file(input_path, output_path):
|
| 15 |
if input_path.endswith('.pdf'):
|
|
|
|
| 16 |
images = pdf2image.convert_from_path(input_path)
|
| 17 |
for i, image in enumerate(images):
|
| 18 |
image.save(f"{os.path.splitext(output_path)[0]}_page_{i+1}.webp", 'WEBP')
|
|
|
|
|
|
|
| 19 |
webp_images = [Image.open(f"{os.path.splitext(output_path)[0]}_page_{i+1}.webp") for i in range(len(images))]
|
| 20 |
webp_images[0].save(output_path, 'PDF', resolution=100.0, save_all=True, append_images=webp_images[1:])
|
|
|
|
| 21 |
elif input_path.endswith('.mp4'):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
elif input_path.endswith('.wav'):
|
|
|
|
| 25 |
audio = AudioSegment.from_wav(input_path)
|
| 26 |
audio.export(output_path, format='mp3')
|
|
|
|
| 27 |
elif input_path.endswith(('.png', '.jpg')):
|
|
|
|
| 28 |
image = Image.open(input_path)
|
| 29 |
image.save(output_path, 'WEBP')
|
| 30 |
|
|
@@ -57,13 +63,58 @@ def get_output_extension(file):
|
|
| 57 |
return '.webp'
|
| 58 |
return ''
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# api.py
|
|
|
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
import rarfile
|
| 5 |
from PyPDF2 import PdfReader, PdfWriter
|
|
|
|
| 6 |
from pydub import AudioSegment
|
| 7 |
from PIL import Image
|
| 8 |
import pdf2image
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import ffmpeg
|
| 11 |
|
| 12 |
def convert_file(input_path, output_path):
|
| 13 |
if input_path.endswith('.pdf'):
|
| 14 |
+
# Convert PDF to WEBP
|
| 15 |
images = pdf2image.convert_from_path(input_path)
|
| 16 |
for i, image in enumerate(images):
|
| 17 |
image.save(f"{os.path.splitext(output_path)[0]}_page_{i+1}.webp", 'WEBP')
|
| 18 |
+
|
| 19 |
+
# Convert WEBP back to PDF
|
| 20 |
webp_images = [Image.open(f"{os.path.splitext(output_path)[0]}_page_{i+1}.webp") for i in range(len(images))]
|
| 21 |
webp_images[0].save(output_path, 'PDF', resolution=100.0, save_all=True, append_images=webp_images[1:])
|
| 22 |
+
|
| 23 |
elif input_path.endswith('.mp4'):
|
| 24 |
+
# Convert MP4 to MKV using ffmpeg
|
| 25 |
+
ffmpeg.input(input_path).output(output_path, codec='libx264').run()
|
| 26 |
+
|
| 27 |
elif input_path.endswith('.wav'):
|
| 28 |
+
# Convert WAV to MP3
|
| 29 |
audio = AudioSegment.from_wav(input_path)
|
| 30 |
audio.export(output_path, format='mp3')
|
| 31 |
+
|
| 32 |
elif input_path.endswith(('.png', '.jpg')):
|
| 33 |
+
# Convert PNG/JPG to WEBP
|
| 34 |
image = Image.open(input_path)
|
| 35 |
image.save(output_path, 'WEBP')
|
| 36 |
|
|
|
|
| 63 |
return '.webp'
|
| 64 |
return ''
|
| 65 |
|
| 66 |
+
# Streamlit UI
|
| 67 |
+
st.title("File Converter Space")
|
| 68 |
+
|
| 69 |
+
# Language selection
|
| 70 |
+
language = st.selectbox("Select Language / انتخاب زبان", ["English", "فارسی"])
|
| 71 |
+
|
| 72 |
+
if language == "English":
|
| 73 |
+
description = """
|
| 74 |
+
This space allows you to convert files between various formats.
|
| 75 |
+
Supported conversions:
|
| 76 |
+
- PDF to WEBP and back to PDF
|
| 77 |
+
- MP4 to MKV
|
| 78 |
+
- WAV to MP3
|
| 79 |
+
- PNG/JPG to WEBP
|
| 80 |
+
"""
|
| 81 |
+
upload_label = "Choose a file"
|
| 82 |
+
convert_button = "Convert Files"
|
| 83 |
+
success_message = "Files converted successfully!"
|
| 84 |
+
converted_files_label = "Converted Files:"
|
| 85 |
+
else:
|
| 86 |
+
description = """
|
| 87 |
+
این اسپیس به شما امکان میدهد فایلها را بین فرمتهای مختلف تبدیل کنید.
|
| 88 |
+
تبدیلهای پشتیبانی شده:
|
| 89 |
+
- PDF به WEBP و بازگشت به PDF
|
| 90 |
+
- MP4 به MKV
|
| 91 |
+
- WAV به MP3
|
| 92 |
+
- PNG/JPG به WEBP
|
| 93 |
+
"""
|
| 94 |
+
upload_label = "یک فایل انتخاب کنید"
|
| 95 |
+
convert_button = "تبدیل فایلها"
|
| 96 |
+
success_message = "فایلها با موفقیت تبدیل شدند!"
|
| 97 |
+
converted_files_label = "فایلهای تبدیل شده:"
|
| 98 |
+
|
| 99 |
+
st.write(description)
|
| 100 |
+
|
| 101 |
+
uploaded_file = st.file_uploader(upload_label, type=['zip', 'rar'])
|
| 102 |
+
|
| 103 |
+
if uploaded_file is not None:
|
| 104 |
+
file_path = f"temp_{uploaded_file.name}"
|
| 105 |
+
with open(file_path, "wb") as f:
|
| 106 |
+
f.write(uploaded_file.getbuffer())
|
| 107 |
+
|
| 108 |
+
st.success("File uploaded successfully!")
|
| 109 |
+
|
| 110 |
+
if st.button(convert_button):
|
| 111 |
+
extract_and_convert(file_path)
|
| 112 |
+
st.success(success_message)
|
| 113 |
|
| 114 |
+
# Display converted files
|
| 115 |
+
st.write(converted_files_label)
|
| 116 |
+
extracted_dir = os.path.splitext(file_path)[0]
|
| 117 |
+
for root, _, files in os.walk(extracted_dir):
|
| 118 |
+
for file in files:
|
| 119 |
+
if file.endswith(('.pdf', '.mkv', '.mp3', '.webp')):
|
| 120 |
+
st.write(f"- {file}")
|