Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,69 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
3 |
js="""
|
4 |
function start() {
|
5 |
-
|
|
|
6 |
return params;
|
7 |
}"""
|
8 |
|
9 |
with gr.Blocks() as app:
|
10 |
-
|
|
|
|
|
11 |
app.load(None,None,t,js=js)
|
|
|
12 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import io
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
+
import requests
|
6 |
+
import gradio as gr
|
7 |
+
from pypdf import PdfReader
|
8 |
+
from pypipertts import PyPiper
|
9 |
+
import subprocess
|
10 |
+
|
11 |
+
pp=PyPiper()
|
12 |
+
pp.load_mod()
|
13 |
+
|
14 |
+
def read_pdf(pdf_url):
|
15 |
+
file_name=f"{uuid.uuid4()}.pdf"
|
16 |
+
response = requests.get(pdf_url, stream=True)
|
17 |
+
if response.status_code == 200:
|
18 |
+
with open(file_name, "wb") as f:
|
19 |
+
f.write(response.content)
|
20 |
+
else:
|
21 |
+
print(response.status_code)
|
22 |
+
if not os.path.isdir("./images"):
|
23 |
+
os.mkdir("./images")
|
24 |
+
text=[]
|
25 |
+
images = ""
|
26 |
+
reader = PdfReader(file_name)
|
27 |
+
number_of_pages = len(reader.pages)
|
28 |
+
#file_name=str(pdf_path).split("\\")[-1]
|
29 |
+
for i in range(number_of_pages):
|
30 |
+
page = reader.pages[i]
|
31 |
+
images=""
|
32 |
+
if len(page.images) >0:
|
33 |
+
for count, image_file_object in enumerate(page.images):
|
34 |
+
with open( "./images/" + str(count) + image_file_object.name, "wb") as fp:
|
35 |
+
fp.write(image_file_object.data)
|
36 |
+
#buffer = io.BytesIO(image_file_object.data)
|
37 |
+
#images.append({"name":file_name,"page":i,"cnt":count,"image":Image.open(buffer)})
|
38 |
+
#images.append(str(image_file_object.data))
|
39 |
+
images += "./images/" + str(i) + image_file_object.name + "\n"
|
40 |
+
#text = f'{text}\n{page.extract_text()}'
|
41 |
+
else:
|
42 |
+
images=""
|
43 |
+
|
44 |
+
text.append({"page":i,"text":page.extract_text(),"images":images})
|
45 |
+
return text
|
46 |
+
def stream_aud(url):
|
47 |
+
inp=read_pdf(url)
|
48 |
+
for ea in inp:
|
49 |
+
txt=ea['text']
|
50 |
+
yield(pp.stream_tts(in_text=txt,model="en_US-joe-medium"))
|
51 |
+
def load_html(url):
|
52 |
+
html=f"""<iframe src="https://docs.google.com/viewer?url={url})}&embedded=true" frameborder="0" height="1200px" width="100%"></iframe></div>"""
|
53 |
+
return html
|
54 |
|
55 |
+
|
56 |
js="""
|
57 |
function start() {
|
58 |
+
const urlParams = new URLSearchParams(window.location.search;);
|
59 |
+
const params = urlParams.get('q')
|
60 |
return params;
|
61 |
}"""
|
62 |
|
63 |
with gr.Blocks() as app:
|
64 |
+
a=gr.Audio(streaming=True,autoplay=True)
|
65 |
+
h=gr.HTML()
|
66 |
+
t=gr.Textbox(interactive=False).change(load_html,t,h).then(stream_aud,t,a)
|
67 |
app.load(None,None,t,js=js)
|
68 |
+
|
69 |
app.launch()
|