Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
3 |
import os
|
4 |
|
5 |
-
def
|
6 |
"""
|
7 |
-
|
8 |
|
9 |
Args:
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
lang (str): Language code (default is 'en' for English).
|
12 |
|
13 |
Returns:
|
14 |
-
str: Path to the generated audio file.
|
15 |
"""
|
16 |
try:
|
|
|
|
|
|
|
|
|
|
|
17 |
# Create gTTS object
|
18 |
tts = gTTS(text=text, lang=lang, slow=False)
|
19 |
|
@@ -30,12 +55,12 @@ def text_to_speech(text, lang="en"):
|
|
30 |
demo = gr.Interface(
|
31 |
fn=text_to_speech,
|
32 |
inputs=[
|
33 |
-
gr.
|
34 |
gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en")
|
35 |
],
|
36 |
outputs=gr.Audio(label="Generated Speech"),
|
37 |
-
title="
|
38 |
-
description="
|
39 |
)
|
40 |
|
41 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
+
import pdfplumber
|
4 |
import os
|
5 |
|
6 |
+
def extract_text_from_pdf(pdf_file):
|
7 |
"""
|
8 |
+
Extract text from a PDF file using pdfplumber.
|
9 |
|
10 |
Args:
|
11 |
+
pdf_file: Uploaded PDF file.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: Extracted text from the PDF.
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
with pdfplumber.open(pdf_file) as pdf:
|
18 |
+
text = ""
|
19 |
+
for page in pdf.pages:
|
20 |
+
text += page.extract_text() or ""
|
21 |
+
return text if text else "No text could be extracted from the PDF."
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error extracting text: {str(e)}"
|
24 |
+
|
25 |
+
def text_to_speech(pdf_file, lang="en"):
|
26 |
+
"""
|
27 |
+
Convert text from a PDF to speech using gTTS and return the audio file path.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
pdf_file: Uploaded PDF file.
|
31 |
lang (str): Language code (default is 'en' for English).
|
32 |
|
33 |
Returns:
|
34 |
+
str: Path to the generated audio file or error message.
|
35 |
"""
|
36 |
try:
|
37 |
+
# Extract text from PDF
|
38 |
+
text = extract_text_from_pdf(pdf_file)
|
39 |
+
if "Error" in text:
|
40 |
+
return text
|
41 |
+
|
42 |
# Create gTTS object
|
43 |
tts = gTTS(text=text, lang=lang, slow=False)
|
44 |
|
|
|
55 |
demo = gr.Interface(
|
56 |
fn=text_to_speech,
|
57 |
inputs=[
|
58 |
+
gr.File(label="Upload a PDF file", file_types=[".pdf"]),
|
59 |
gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en")
|
60 |
],
|
61 |
outputs=gr.Audio(label="Generated Speech"),
|
62 |
+
title="PDF to Speech with gTTS",
|
63 |
+
description="Upload a PDF file, select a language, and generate speech from the extracted text."
|
64 |
)
|
65 |
|
66 |
# Launch the app
|