Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	| import gradio as gr | |
| import io | |
| import os | |
| import uuid | |
| import requests | |
| import gradio as gr | |
| from pypdf import PdfReader | |
| from pypipertts import PyPiper | |
| import subprocess | |
| 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_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/?pdfurl=https://arxiv.org/pdf/2412.01821"/> | |
| <meta name="twitter:player:stream" content="https://broadfield-fast-voice-full.hf.space/?pdfurl=https://arxiv.org/pdf/2412.01821"/> | |
| <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 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]; | |
| } | |
| """ | |
| with gr.Blocks(head=head) as app: | |
| 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) | |
| 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(read_pdf,t,bulk) | |
| mc=m.change(pp.load_mod,m,None) | |
| bc=bulk.change(pp.stream_tts,[bulk,m,l,n,w,p],a) | |
| uc=upd.click(pp.stream_tts,[bulk,m,l,n,w,p],a) | |
| sc=stp.click(None,None,None, cancels=[tc,mc,bc,uc]) | |
| app.queue(default_concurrency_limit=20).launch(max_threads=40) |