bori0824 commited on
Commit
43eaadf
·
verified ·
1 Parent(s): 99545a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from pydub import AudioSegment
4
+ from docx import Document
5
+ import fitz # PyMuPDF
6
+ import os
7
+ import tempfile
8
+
9
+ # Load Huggingface TTS pipeline
10
+ tts = pipeline("text-to-speech", model="tts_en_cmu_us_slt")
11
+
12
+ def extract_text_from_pdf(pdf_path):
13
+ """Extract text from a PDF file."""
14
+ pdf_document = fitz.open(pdf_path)
15
+ text = ""
16
+ for page_num in range(len(pdf_document)):
17
+ page = pdf_document[page_num]
18
+ text += page.get_text()
19
+ return text
20
+
21
+ def extract_text_from_docx(docx_path):
22
+ """Extract text from a Word (.docx) file."""
23
+ doc = Document(docx_path)
24
+ text = [paragraph.text for paragraph in doc.paragraphs]
25
+ return "\n".join(text)
26
+
27
+ def process_file(file):
28
+ """Process uploaded file and convert text to audio."""
29
+ file_extension = file.name.split('.')[-1].lower()
30
+ text = ""
31
+
32
+ try:
33
+ # Save uploaded file temporarily
34
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
35
+ temp_file.write(file.read())
36
+ temp_path = temp_file.name
37
+
38
+ # Extract text based on file type
39
+ if file_extension == 'txt':
40
+ with open(temp_path, 'r') as f:
41
+ text = f.read()
42
+ elif file_extension == 'pdf':
43
+ text = extract_text_from_pdf(temp_path)
44
+ elif file_extension == 'docx':
45
+ text = extract_text_from_docx(temp_path)
46
+ else:
47
+ return "Unsupported file type!", None
48
+
49
+ # Generate speech audio
50
+ audio_data = tts(text, return_tensors=True)
51
+
52
+ # Save the audio as an MP3 file
53
+ output_path = os.path.join(tempfile.gettempdir(), "output.mp3")
54
+ audio_segment = AudioSegment.from_file(audio_data["waveform"], format="wav")
55
+ audio_segment.export(output_path, format="mp3")
56
+
57
+ return "Text converted to audio successfully!", output_path
58
+ finally:
59
+ # Clean up temporary files
60
+ if os.path.exists(temp_path):
61
+ os.remove(temp_path)
62
+
63
+ # Define Gradio interface
64
+ def text_to_audio(file):
65
+ result_message, audio_file = process_file(file)
66
+ if audio_file:
67
+ return result_message, audio_file
68
+ else:
69
+ return result_message, None
70
+
71
+ # Create Gradio interface
72
+ interface = gr.Interface(
73
+ fn=text_to_audio,
74
+ inputs=gr.File(label="Upload your file (txt, pdf, or docx)"),
75
+ outputs=[
76
+ gr.Textbox(label="Result"),
77
+ gr.Audio(label="Generated Audio")
78
+ ],
79
+ title="Text-to-Audio Converter",
80
+ description="Upload a text, PDF, or Word file and convert its content to an audio file (American English). Download the audio after it's generated."
81
+ )
82
+
83
+ # Launch Gradio app
84
+ interface.launch()