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"
" def load_html(url): html=f"""""" return html head = """ """ 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"