File size: 7,810 Bytes
47a983d
67df1ec
 
 
ccf63fb
4e0ecdf
67df1ec
a2709b1
67df1ec
 
ec0c527
a2709b1
9016ad6
ea05862
322280b
ea05862
ed0b82e
 
9a9f732
 
ed0b82e
 
8571cd0
67df1ec
 
 
 
223aa24
67df1ec
9213359
67df1ec
 
 
 
 
6d5b659
67df1ec
 
 
 
 
6d5b659
08c3cd4
37836de
ed0b82e
4589b4c
ed0b82e
 
 
 
 
 
 
 
4589b4c
0da0e15
 
f9f19a4
797c734
61101c1
88ac129
8659d91
d89a211
e8f7a69
ad6b280
9a9f732
 
 
6f59945
0da0e15
4929289
 
e6ee474
ed0b82e
e6ee474
0da0e15
b8f8abd
ed0b82e
9a9f732
ed0b82e
edab6bd
61101c1
0da0e15
edab6bd
 
 
 
52eeaba
6f59945
67df1ec
c0f4dec
67df1ec
f1c79b1
4e408fd
 
 
 
d4b148d
 
4e408fd
2c0bf37
4e408fd
 
 
07da7b1
f1c79b1
8571cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a937218
 
f1c79b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a983d
7362783
09e540a
162a4cb
 
 
 
 
 
0a1ff49
 
affba00
0a1ff49
affba00
6028dff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8571cd0
695cef6
162a4cb
8083ec5
fb759c5
8083ec5
2c0bf37
 
 
 
 
84865a8
a5cafe7
 
84865a8
2c0bf37
6e3ac3a
84865a8
a9c2af4
37836de
2c0bf37
1c7b260
6e3ac3a
67df1ec
f87aee3
 
 
 
 
 
 
 
 
 
ed0b82e
f027b25
fb759c5
297adbb
a50eb64
6d5b659
f1c79b1
37836de
f87aee3
3f88419
f87aee3
f027b25
9976c65
a50eb64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import gradio as gr
import io 
import os
import uuid
import json
import random
import requests
import subprocess
from pypdf import PdfReader
from pypipertts import PyPiper
from urllib.parse import urlparse, parse_qs
from huggingface_hub import HfApi
import datetime
from utils import make_im
mi=make_im()

token=os.environ.get("HF_TOKEN")
user_name="broadfield"
repo_name="a"
#save_data=f'https://huggingface.co/datasets/{user_name}/{repo_name}/raw/main/'

api = HfApi(token=token)

pp=PyPiper()
pp.load_mod()

def read_pdf(pdf_url):
    print(pdf_url)
    file_name=f"{uuid.uuid4()}.pdf"
    response = requests.get(pdf_url.replace('http:','https:'), stream=True)
    if response.status_code == 200:
        with open(file_name, "wb") as f:
            f.write(response.content)
    else:
        print(response.status_code)
    txt_out=""
    reader = PdfReader(file_name)
    number_of_pages = len(reader.pages)
    #file_name=str(pdf_path).split("\\")[-1]
    for i in range(number_of_pages):
        page = reader.pages[i]
        txt_out+=page.extract_text()
        #return txt_out
    return txt_out.replace("\n","")

def load_data(file):
    try:
        r = requests.get(f'{save_data}{file}.json') 
        lod = json.loads(r.text)
    except:
        lod=[]
        pass
    return(lod)
        
def save_data(t,m,l,n,w,p):
    rand = random.randint(1000,9999)
    
    title = t.replace('https://',"").replace('/',"_").replace('.',"_")
    im = mi.run_1(name=title,img_size=[315,600])

    im1 = f"{os.getcwd()}/images/{title}_prob.png"
    #im2=f"https://{user_name}-{repo_name}.static.hf.space/images/{title}_prob.png"
    im2=f"https://huggingface.co/spaces/{user_name}/{repo_name}/resolve/main/images/{title}_{rand}.png"
    #im3=f"{make_im.root}/images/{name}_prob.png"
    #uid=uuid.uuid4()
    with open('template.html','r') as file:
        file_cont=file.read()
    file.close()
    file_out=file_cont.replace('IMAGE',im2).replace('TITLE',f'{title}_{rand}').replace('PDFURL',t).replace('VOICE',m).replace('LENGTH',l).replace('NOISE',n).replace('WIDTH',w).replace('PAUSE',p)
    out_name=f"{title}_{rand}"
    buffer = io.BytesIO()
    buffer.write(file_out.encode())
    buffer.seek(0)
    api.upload_file(
        path_or_fileobj=buffer,
        path_in_repo=f"{out_name}.html",
        repo_id=f'{user_name}/{repo_name}',
        token=token,
        repo_type="space",
    )
    api.upload_file(
        path_or_fileobj=im1,
        path_in_repo=f"/images/{out_name}.png",
        repo_id=f'{user_name}/{repo_name}',
        token=token,
        repo_type="space",
    )
    #return f"https://broadfield-a.hf.space/?pdfurl={t}&mod={m}&len={l}&nos={n}&wid={w}&pau={p}"
    return f"<div><a href='https://{user_name}-{repo_name}.static.hf.space/{title}_{rand}.html' target='_blank'>https://broadfield-a.static.hf.space/{title}_{rand}.html</a></div>"
def load_html(url):
    html=f"""<iframe src="https://docs.google.com/viewer?url={url})&embedded=true" frameborder="0" height="1200px" width="100%"></iframe></div>"""
    return html
head = """
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="twitter:card" content="player"/>
<meta name="twitter:site" content=""/>
<meta name="twitter:player" content="https://broadfield-fast-voice-full.hf.space/"/>
<meta name="twitter:player:stream" content="https://broadfield-fast-voice-full.hf.space/"/>
<meta name="twitter:player:width" content="100%"/>
<meta name="twitter:player:height" content="100%"/>    
<meta property="og:title" content="PDF to Voice"/>
<meta property="og:description" content="Add PDF link to /?pdfurl= parameter"/>
<!---meta property="og:image" content=""/--->
<!---meta http-equiv="refresh" content="0; url=https://broadfield-fast-voice-full.hf.space/?pdfurl="--->
<script>


function changeMetaTag(name, content) {
  // Find the existing meta tag with the given name
  let metaTag = document.querySelector(`meta[name="${name}"]`);

  // If it doesn't exist, create a new meta tag
  if (!metaTag) {
    metaTag = document.createElement('meta');
    metaTag.name = name;
    document.head.appendChild(metaTag);
  }

  // Set the content of the meta tag
  metaTag.content = content;
}
const urlParams1 = new URLSearchParams(window.location.search);
var p_pdf = urlParams1.get('pdfurl')
console.log("params")
console.log(p_pdf)
/*changeMetaTag('twitter:player', 'https://broadfield-fast-voice-full.hf.space/?pdfurl=ZZZ'.replace('ZZZ',p_pdf))
changeMetaTag('twitter:player:stream', 'https://broadfield-fast-voice-full.hf.space/?pdfurl=ZZZ'.replace('ZZZ',p_pdf))*/
function run(url_in) {
    let url = String(url_in)
    console.log(url);
    
    var framediv = document.getElementById('cap');
    let child = framediv.lastElementChild;
    while (child) {
        framediv.removeChild(child);
        child=framediv.lastElementChild;
    }   
    const iframediv = document.createElement('iframe');
      iframediv.src = 'https://docs.google.com/viewer?url=' + url +'&embedded=true';
      iframediv.width = '100%'; 
      iframediv.height = '1000px';
      iframediv.frameborder="0";
      framediv.appendChild(iframediv);
}
</script>
"""
js="""
function () {
    const urlParams = new URLSearchParams(window.location.search);
    var p_pdf = urlParams.get('pdfurl')
    var p_mod = urlParams.get('mod')
    var p_len = urlParams.get('len')
    var p_nos = urlParams.get('nos')
    var p_wid = urlParams.get('wid')
    var p_pau = urlParams.get('pau')
    if (p_mod) {
        console.log(p_mod)
    } else {
        p_mod = "en_US-joe-medium";
    };
    if (p_len) {
        console.log(p_len)
    } else {
        p_len = "1";
    };
    if (p_nos) {
        console.log(p_nos)
    } else {
        p_nos = "0.5";
    };
    if (p_wid) {
        console.log(p_wid)
    } else {
        p_wid = "0.5";
    };
    if (p_pau) {
        console.log(p_pau)
    } else {
        p_pau = "1";
    };    

    run(p_pdf)
    return [p_pdf, p_mod, p_len, p_nos, p_wid, p_pau];
}

"""
css="""
#id {
height:500px;
width:100%;
}
.mes_div {
height:30px;
font-weight:500;
}
"""
def upd_mes(t):
    return(f"<div class='mes_div'>Reading PDF: {t}</div>")
def upd_mes2():
    return(f"<div class='mes_div'>PDF Loaded, creating audio</div>")
with gr.Blocks(head=head,css=css) as app:
    html=gr.HTML("""<div style='font-size:xx-large;font-weight:900'>PDF Reader to Voice</div>""")
    html2=gr.HTML()
    a=gr.Audio(streaming=True,autoplay=True)
    with gr.Accordion("Voice Controls",open=False):
        with gr.Row():
            stp=gr.Button("Stop")
            upd=gr.Button("Update Voice")
        t=gr.Textbox(label="PDF URL",interactive=True,visible=True)
        m=gr.Dropdown(label="Voice", choices=pp.key_list,value="en_US-lessac-high",interactive=True)
        l=gr.Slider(label="Length", minimum=0.01, maximum=10.0, value=1,interactive=True)
        n=gr.Slider(label="Noise", minimum=0.01, maximum=3.0, value=0.5,interactive=True)
        w=gr.Slider(label="Noise Width", minimum=0.01, maximum=3.0, value=0.5,interactive=True)
        p=gr.Slider(label="Sentence Pause", minimum=0.1, maximum=10.0, value=1,interactive=True)
        save=gr.Button("Save Data")
        save_html=gr.HTML()
    h=gr.HTML("""<div id='cap'></div>""")
    with gr.Column(visible=False):    
        bulk=gr.Textbox(label="bulk",interactive=False,visible=True)
    
    app.load(None,None,[t,m,l,n,w,p],js=js)
    tc=t.change(upd_mes,t,html2).then(read_pdf,t,[bulk])
    mc=m.change(pp.load_mod,m,None)
    bc=bulk.change(upd_mes2,None,html2).then(pp.stream_tts,[bulk,m,l,n,w,p],a)
    uc=upd.click(pp.stream_tts,[bulk,m,l,n,w,p],a)
    sb=save.click(save_data,[t,m,l,n,w,p],save_html)
    sc=stp.click(None,None,None, cancels=[tc,mc,bc,uc])
app.queue(default_concurrency_limit=20).launch(max_threads=40)