Spaces:
Build error
Build error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from TTS.api import TTS
|
| 3 |
-
import fitz # PyMuPDF for PDF text extraction
|
| 4 |
-
from pydub import AudioSegment
|
| 5 |
-
import tempfile
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Initialize TTS models
|
| 9 |
-
TTS_MODELS = {
|
| 10 |
-
"American - Male": "tts_models/en/ljspeech/tacotron2-DDC",
|
| 11 |
-
"American - Female": "tts_models/en/ljspeech/tacotron2-DDC",
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
def extract_text_from_pdf(pdf_path, page_selection=None):
|
| 15 |
-
"""Extract specific pages from a PDF."""
|
| 16 |
-
pdf_document = fitz.open(pdf_path)
|
| 17 |
-
text = ""
|
| 18 |
-
total_pages = len(pdf_document)
|
| 19 |
-
|
| 20 |
-
# Parse the page_selection input (e.g., "1,3-5")
|
| 21 |
-
pages_to_read = []
|
| 22 |
-
if page_selection:
|
| 23 |
-
for part in page_selection.split(","):
|
| 24 |
-
if "-" in part:
|
| 25 |
-
start, end = map(int, part.split("-"))
|
| 26 |
-
pages_to_read.extend(range(start - 1, end)) # Convert to 0-indexed
|
| 27 |
-
else:
|
| 28 |
-
pages_to_read.append(int(part) - 1) # Convert to 0-indexed
|
| 29 |
-
else:
|
| 30 |
-
pages_to_read = list(range(total_pages)) # Default: all pages
|
| 31 |
-
|
| 32 |
-
# Ensure valid page selection
|
| 33 |
-
pages_to_read = [p for p in pages_to_read if 0 <= p < total_pages]
|
| 34 |
-
|
| 35 |
-
# Extract text from selected pages
|
| 36 |
-
for page_num in pages_to_read:
|
| 37 |
-
page = pdf_document[page_num]
|
| 38 |
-
page_text = page.get_text()
|
| 39 |
-
text += f"\n--- Page {page_num + 1} ---\n{page_text}" # Add page info
|
| 40 |
-
|
| 41 |
-
if not text.strip():
|
| 42 |
-
return "Error: No text found on the selected pages. They might contain images only."
|
| 43 |
-
return text
|
| 44 |
-
|
| 45 |
-
def generate_audio(text, accent_gender, speed):
|
| 46 |
-
"""Generate audio from text using selected accent, gender, and speed."""
|
| 47 |
-
model_name = TTS_MODELS[accent_gender]
|
| 48 |
-
tts = TTS(model_name=model_name)
|
| 49 |
-
temp_audio_path = os.path.join(tempfile.gettempdir(), "output.wav")
|
| 50 |
-
tts.tts_to_file(text=text, file_path=temp_audio_path)
|
| 51 |
-
|
| 52 |
-
# Adjust speed
|
| 53 |
-
audio = AudioSegment.from_file(temp_audio_path)
|
| 54 |
-
audio = audio.speedup(playback_speed=speed)
|
| 55 |
-
|
| 56 |
-
# Save as MP3
|
| 57 |
-
output_mp3_path = os.path.join(tempfile.gettempdir(), "output.mp3")
|
| 58 |
-
audio.export(output_mp3_path, format="mp3")
|
| 59 |
-
return output_mp3_path
|
| 60 |
-
|
| 61 |
-
def process_input(input_text, uploaded_file, page_selection, accent_gender, speed):
|
| 62 |
-
"""Process input (text or file) and generate audio."""
|
| 63 |
-
try:
|
| 64 |
-
if not input_text and not uploaded_file:
|
| 65 |
-
return "Please provide input text or upload a file.", None
|
| 66 |
-
|
| 67 |
-
# Extract text from uploaded file
|
| 68 |
-
if uploaded_file:
|
| 69 |
-
file_extension = uploaded_file.name.split('.')[-1].lower()
|
| 70 |
-
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 71 |
-
temp_file.write(uploaded_file.read())
|
| 72 |
-
temp_file_path = temp_file.name
|
| 73 |
-
|
| 74 |
-
if file_extension == "pdf":
|
| 75 |
-
text = extract_text_from_pdf(temp_file_path, page_selection)
|
| 76 |
-
else:
|
| 77 |
-
return "Error: Only PDF file support for page selection.", None
|
| 78 |
-
|
| 79 |
-
os.remove(temp_file_path)
|
| 80 |
-
else:
|
| 81 |
-
text = input_text
|
| 82 |
-
|
| 83 |
-
# Generate audio
|
| 84 |
-
mp3_path = generate_audio(text, accent_gender, float(speed))
|
| 85 |
-
return "Audio generated successfully!", mp3_path
|
| 86 |
-
|
| 87 |
-
except Exception as e:
|
| 88 |
-
import traceback
|
| 89 |
-
error_message = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 90 |
-
print(error_message)
|
| 91 |
-
return error_message, None
|
| 92 |
-
|
| 93 |
-
# Gradio interface
|
| 94 |
-
interface = gr.Interface(
|
| 95 |
-
fn=process_input,
|
| 96 |
-
inputs=[
|
| 97 |
-
gr.Textbox(label="Enter Text", placeholder="Type or paste text here...", lines=5),
|
| 98 |
-
gr.File(label="Upload File (.pdf only for page selection)", file_types=[".pdf"]),
|
| 99 |
-
gr.Textbox(label="Page Selection (e.g., '1,3-5' or leave blank for all pages)", placeholder="Pages to read"),
|
| 100 |
-
gr.Dropdown(label="Accent & Gender", choices=list(TTS_MODELS.keys()), value="American - Male"),
|
| 101 |
-
gr.Slider(label="Speed (e.g., 1.0 = Normal, 0.75 = Slower, 1.25 = Faster)", minimum=0.5, maximum=2.0, value=1.0, step=0.1),
|
| 102 |
-
],
|
| 103 |
-
outputs=[
|
| 104 |
-
gr.Textbox(label="Result"),
|
| 105 |
-
gr.Audio(label="Generated Audio"),
|
| 106 |
-
],
|
| 107 |
-
title="Text-to-Speech (TTS) Application",
|
| 108 |
-
description="Upload a PDF file or enter text directly. Specify pages to extract text from, customize accent, gender, and speed. Download the generated audio as MP3."
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
# Launch the app
|
| 112 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|