Spaces:
Sleeping
Sleeping
File size: 1,714 Bytes
f5e0882 b09bae2 f5e0882 4c6fdbe f5e0882 4c6fdbe b09bae2 f5e0882 4c6fdbe f5e0882 4c6fdbe f5e0882 4c6fdbe f5e0882 b09bae2 4c6fdbe b09bae2 f5e0882 4c6fdbe f5e0882 4c6fdbe 69cc487 4c6fdbe efa9479 4e76f2d 4c6fdbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import os
from PyPDF2 import PdfReader
from gtts import gTTS
from pydub import AudioSegment
import gradio as gr
def pdf_to_text(pdf_file):
try:
reader = PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
except Exception as e:
return f"Error: {str(e)}"
def text_to_speech(text, lang='en', file_format='mp3'):
tts = gTTS(text=text, lang=lang)
output_path = f"output.{file_format}"
tts.save("temp.mp3")
if file_format == 'mp3':
os.rename("temp.mp3", output_path)
elif file_format == 'wav':
sound = AudioSegment.from_mp3("temp.mp3")
sound.export(output_path, format="wav")
os.remove("temp.mp3")
return output_path
def convert_pdf_to_speech(pdf_file, lang='en', file_format='mp3'):
text = pdf_to_text(pdf_file)
if text and not text.startswith("Error:"):
audio_file = text_to_speech(text, lang=lang, file_format=file_format)
return audio_file
else:
return text
# Gradio arayüzü
def interface(pdf_file, lang, file_format):
audio_file = convert_pdf_to_speech(pdf_file.name, lang=lang, file_format=file_format)
return audio_file
iface = gr.Interface(
fn=interface,
inputs=[
gr.File(label="Pdf i yükleyiniz"),
gr.Dropdown(choices=["en", "es", "fr", "de", "it", "tr", "zh", "jp"], label="Dil Seçiniz"),
gr.Radio(choices=["mp3", "wav"], label="Çıktı formatını seçiniz")
],
outputs="file",
title="Pdf Seslendirme",
description="Bir PDF dosyası yükleyin, dili seçin ve çıktı formatını (MP3 veya WAV) seçiniz."
)
iface.launch(share=True)
|