Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,57 @@
|
|
1 |
-
import PyPDF2
|
2 |
-
from transformers import pipeline
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Function to extract text from PDF
|
6 |
-
def
|
7 |
-
reader =
|
8 |
text = ""
|
9 |
for page in reader.pages:
|
10 |
if page and page.extract_text():
|
11 |
text += page.extract_text()
|
12 |
return text
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
-
def
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
with open(audio_path, "wb") as f:
|
26 |
-
f.write(audio["audio"]) # Hugging Face TTS returns audio data
|
27 |
|
28 |
-
|
|
|
|
|
29 |
|
30 |
# Gradio interface
|
31 |
-
|
32 |
-
fn=
|
33 |
-
inputs=
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
interface.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from ebooklib import epub
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
|
7 |
+
# Load the VITS model from Hugging Face
|
8 |
+
text_to_speech = pipeline("text-to-speech", model="efficient-speech/lite-whisper-large-v3-turbo")
|
9 |
|
10 |
# Function to extract text from PDF
|
11 |
+
def extract_pdf_text(file):
|
12 |
+
reader = PdfReader(file)
|
13 |
text = ""
|
14 |
for page in reader.pages:
|
15 |
if page and page.extract_text():
|
16 |
text += page.extract_text()
|
17 |
return text
|
18 |
|
19 |
+
# Function to extract text from EPUB
|
20 |
+
def extract_epub_text(file):
|
21 |
+
book = epub.read_epub(file)
|
22 |
+
text = ""
|
23 |
+
for item in book.get_items():
|
24 |
+
if item.get_type() == epub.ITEM_DOCUMENT:
|
25 |
+
soup = BeautifulSoup(item.content, 'html.parser')
|
26 |
+
text += soup.get_text()
|
27 |
+
return text
|
28 |
|
29 |
+
# Unified function to convert text to speech
|
30 |
+
def convert_to_audio(file, file_type):
|
31 |
+
if file_type == 'PDF':
|
32 |
+
text = extract_pdf_text(file)
|
33 |
+
elif file_type == 'EPUB':
|
34 |
+
text = extract_epub_text(file)
|
35 |
+
else:
|
36 |
+
text = file.read().decode('utf-8')
|
37 |
|
38 |
+
if not text.strip():
|
39 |
+
return "No text found in the file."
|
|
|
|
|
40 |
|
41 |
+
# Convert text to speech
|
42 |
+
audio = text_to_speech(text[:5000]) # Limiting input to avoid model constraints
|
43 |
+
return (audio["audio"],)
|
44 |
|
45 |
# Gradio interface
|
46 |
+
demo = gr.Interface(
|
47 |
+
fn=convert_to_audio,
|
48 |
+
inputs=[
|
49 |
+
gr.File(label="Upload PDF, EPUB, or Text File"),
|
50 |
+
gr.Radio(["PDF", "EPUB", "TXT"], label="File Type")
|
51 |
+
],
|
52 |
+
outputs="audio",
|
53 |
+
title="Unlimited Text-to-Speech Converter",
|
54 |
+
description="Upload PDF, EPUB, or text files β convert them into audio with no limits!"
|
55 |
)
|
56 |
|
57 |
+
demo.launch()
|
|