Spaces:
Sleeping
Sleeping
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) | |