Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- api.py +69 -0
- space.yaml +26 -0
api.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
app = Flask(__name__)
|
| 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 |
+
video = VideoFileClip(input_path)
|
| 23 |
+
video.write_videofile(output_path, codec='libx264')
|
| 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 |
+
|
| 31 |
+
def extract_and_convert(file_path):
|
| 32 |
+
if file_path.endswith('.zip'):
|
| 33 |
+
with zipfile.ZipFile(file_path, 'r') as zip_ref:
|
| 34 |
+
zip_ref.extractall(os.path.splitext(file_path)[0])
|
| 35 |
+
extracted_dir = os.path.splitext(file_path)[0]
|
| 36 |
+
elif file_path.endswith('.rar'):
|
| 37 |
+
with rarfile.RarFile(file_path, 'r') as rar_ref:
|
| 38 |
+
rar_ref.extractall(os.path.splitext(file_path)[0])
|
| 39 |
+
extracted_dir = os.path.splitext(file_path)[0]
|
| 40 |
+
else:
|
| 41 |
+
return
|
| 42 |
+
|
| 43 |
+
for root, _, files in os.walk(extracted_dir):
|
| 44 |
+
for file in files:
|
| 45 |
+
input_path = os.path.join(root, file)
|
| 46 |
+
output_path = os.path.join(root, os.path.splitext(file)[0] + get_output_extension(file))
|
| 47 |
+
convert_file(input_path, output_path)
|
| 48 |
+
|
| 49 |
+
def get_output_extension(file):
|
| 50 |
+
if file.endswith('.pdf'):
|
| 51 |
+
return '.pdf'
|
| 52 |
+
elif file.endswith('.mp4'):
|
| 53 |
+
return '.mkv'
|
| 54 |
+
elif file.endswith('.wav'):
|
| 55 |
+
return '.mp3'
|
| 56 |
+
elif file.endswith(('.png', '.jpg')):
|
| 57 |
+
return '.webp'
|
| 58 |
+
return ''
|
| 59 |
+
|
| 60 |
+
@app.route('/convert', methods=['POST'])
|
| 61 |
+
def convert():
|
| 62 |
+
file = request.files['file']
|
| 63 |
+
file_path = f"temp_{file.filename}"
|
| 64 |
+
file.save(file_path)
|
| 65 |
+
extract_and_convert(file_path)
|
| 66 |
+
return jsonify({"message": "Files converted successfully!"})
|
| 67 |
+
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
app.run(debug=True)
|
space.yaml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# space.yaml
|
| 2 |
+
name: file-converter
|
| 3 |
+
description: A space to convert files using various Python libraries.
|
| 4 |
+
|
| 5 |
+
# Define the base image
|
| 6 |
+
base: python:3.9-slim
|
| 7 |
+
|
| 8 |
+
# Install system dependencies
|
| 9 |
+
install:
|
| 10 |
+
- apt-get update
|
| 11 |
+
- apt-get install -y ffmpeg
|
| 12 |
+
|
| 13 |
+
# Install Python dependencies
|
| 14 |
+
pip:
|
| 15 |
+
- zipfile36
|
| 16 |
+
- rarfile
|
| 17 |
+
- PyPDF2
|
| 18 |
+
- moviepy
|
| 19 |
+
- pydub
|
| 20 |
+
- Pillow
|
| 21 |
+
- pdf2image
|
| 22 |
+
- streamlit
|
| 23 |
+
- flask
|
| 24 |
+
|
| 25 |
+
# Define the main script to run
|
| 26 |
+
entrypoint: streamlit run /path/to/your/app.py
|