Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PyPDF2
|
2 |
+
from docx import Document
|
3 |
+
import os
|
4 |
+
from unittest.mock import patch
|
5 |
+
from TTS.api import TTS
|
6 |
+
import torch
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
|
10 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
11 |
+
|
12 |
+
# Function to always return 'y'
|
13 |
+
def always_yes(*args, **kwargs):
|
14 |
+
return 'y'
|
15 |
+
|
16 |
+
#Get device
|
17 |
+
device="cuda:0" if torch.cuda.is_available() else "cpu"
|
18 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False).to(device)
|
19 |
+
|
20 |
+
#################################
|
21 |
+
# Path to the audio file
|
22 |
+
SAMPLE_AUDIOS = ["sampleaudio.wav"]
|
23 |
+
|
24 |
+
################################
|
25 |
+
|
26 |
+
|
27 |
+
def text_to_speech(text):
|
28 |
+
save_path = "outputz.wav"
|
29 |
+
try:
|
30 |
+
tts.tts_to_file(
|
31 |
+
text,
|
32 |
+
speaker_wav=SAMPLE_AUDIOS,
|
33 |
+
language="en",
|
34 |
+
file_path=save_path,
|
35 |
+
split_sentences=True,
|
36 |
+
)
|
37 |
+
return save_path
|
38 |
+
except Exception as e:
|
39 |
+
return str(e)
|
40 |
+
|
41 |
+
def extract_text_from_pdf(file_path):
|
42 |
+
with open(file_path, 'rb') as file:
|
43 |
+
reader = PyPDF2.PdfReader(file)
|
44 |
+
text = "".join([page.extract_text() for page in reader.pages])
|
45 |
+
return text
|
46 |
+
|
47 |
+
def extract_text_from_doc(file_path):
|
48 |
+
doc = Document(file_path)
|
49 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
50 |
+
|
51 |
+
def file_to_audio(file_path):
|
52 |
+
file_type = file_path.split('.')[-1]
|
53 |
+
if file_type == 'pdf':
|
54 |
+
text = extract_text_from_pdf(file_path)
|
55 |
+
elif file_type in ['doc', 'docx']:
|
56 |
+
text = extract_text_from_doc(file_path)
|
57 |
+
else:
|
58 |
+
return "Unsupported file format"
|
59 |
+
|
60 |
+
return text_to_speech(text)
|
61 |
+
def handle_inputs(text, file):
|
62 |
+
if text and file:
|
63 |
+
return None, "Please provide only one input at a time: text or file."
|
64 |
+
elif text:
|
65 |
+
audio_path = text_to_speech(text)
|
66 |
+
return audio_path, "Thanks for your text input"
|
67 |
+
elif file:
|
68 |
+
audio_path = file_to_audio(file)
|
69 |
+
return audio_path, 'Thanks for your File'
|
70 |
+
else:
|
71 |
+
return None, "No input provided."
|