arXiv-reader / app.py
broadfield's picture
Update app.py
055e117 verified
raw
history blame
5.1 kB
import gradio as gr
import xmltodict
import requests
import json
import uuid
from pypipertts import PyPiper
pp=PyPiper()
style="""
.title_div{
font-size: x-large;
font-weight: 700;
margin-bottom: 10px;
color: white;
}
.card_div{
background: #050523;
color: white;
margin: 10px;
padding: 15px;
border-radius: 5px;
cursor: pointer;
}
.x_btn{
font-size: xx-large;
font-weight: 900;
background: chocolate;
width: fit-content;
padding: 0 10px;
border-radius: 10px 10px 0 0;
float: right;
cursor: pointer;
}
.read_btn{
font-size: xx-large;
font-weight: 900;
background: chocolate;
width: fit-content;
padding: 0 10px;
border-radius: 10px 10px 0 0;
float: left;
cursor: pointer;
}
.frame_class{
display:none;
}
"""
head = """
<script>
function run(inn,url) {
console.log(inn);
console.log(url);
var frame_on = document.getElementById("frame" + String(inn));
frame_on.style.display = 'block';
var framediv = document.getElementById("framediv" + String(inn));
let child = framediv.lastElementChild;
while (child) {
framediv.removeChild(child);
child = framediv.lastElementChild;
}
const iframediv = document.createElement('iframe');
iframediv.src = String(url);
iframediv.width = '100%';
iframediv.height = '1000px';
framediv.appendChild(iframediv);
}
function closefn(inn) {
console.log(inn);
var frame_off = document.getElementById("frame" + String(inn));
var aud = document.getElementById("aud" + String(inn));
var framedivdocument.getElementById("framediv" + String(inn));
frame_off.style.display = 'none';
let child1 = aud.lastElementChild;
while (child1) {
aud.removeChild(child);
child = aud.lastElementChild;
}
let child2 = framediv.lastElementChild;
while (child2) {
framediv.removeChild(child);
child = framediv.lastElementChild;
}
}
function readfn(inn,url) {
console.log(inn);
console.log(url);
var readit = document.getElementById("aud" + String(inn));
let api = 'https://broadfield-fast-voice.hf.space/?pdfurl=' + url;
console.log(api);
let child = readit.lastElementChild;
while (child) {
readit.removeChild(child);
child = readit.lastElementChild;
}
const iframe = document.createElement('iframe');
iframe.src = api;
iframe.width = '100%';
iframe.height = '200px';
readit.appendChild(iframe);
}
</script>
"""
def search(q,rn,st):
api=f"http://export.arxiv.org/api/query?search_query={str(q)}&start={str(st)}&max_results={str(rn)}"
r=requests.get(api)
cont=xmltodict.parse(r.content)['feed']['entry']
html=""
for i,c in enumerate(cont):
pdflink=c['id'].replace('/abs/','/pdf/').replace('http:','https:')
html+=f"""<div class='card_div' id='id{i}' onclick="run('{i}','{pdflink}')">
<div class='title_div'>{c['title']}</div>
<div style='color:white;'>{c['summary']}</div>
<div><a href='{pdflink}' target='_blank'>{pdflink}</a></div>
</div>
<div id=frame{i} class='frame_class'>
<div id=read{i} class='read_btn' onclick="readfn('{i}','{pdflink}')">Read</div>
<div id=close{i} class='x_btn' onclick='closefn({i})'>X</div>
<div id=aud{i}></div>
<div id=frmdiv{i}></div>
</div>"""
return html
def next_show(cur):
new=int(cur)+10
return gr.update(interactive=True),new
def prev_show(cur):
if int(cur)-10 <=0:
new=0
out_gr=gr.update(interactive=False)
else:
new=int(cur)-10
out_gr=gr.update(interactive=True)
return out_gr,new
def show_num(cur):
new=int(cur)+10
html_out=f"<div><center>Showing {cur} through {new}</center></div>"
return html_out
def stream_aud(inp):
txt=inp[0]['text']
pp=PyPiper()
pp.load_mod()
yield(pp.stream_tts(in_text=txt,model="en_US-joe-medium"))
with gr.Blocks(css=style,head=head) as b:
with gr.Group():
with gr.Row():
query=gr.Textbox(label="Query")
num=gr.Number(label="Count",step=1,value=10,interactive=False)
with gr.Row():
sub=gr.Button(size='lg')
with gr.Group():
with gr.Row():
prev_btn=gr.Button("Previous",interactive=False)
show_html=gr.HTML()
next_btn=gr.Button("Next")
html_out=gr.HTML()
json_out=gr.JSON()
hid_start1=gr.Textbox(elem_id='readit')
hid_start=gr.Number(step=1,value=0,visible=False, interactive=False)
next_btn.click(next_show,[hid_start],[prev_btn,hid_start]).then(show_num,hid_start,show_html).then(search,[query,num,hid_start],[html_out])
prev_btn.click(prev_show,[hid_start],[prev_btn,hid_start]).then(show_num,hid_start,show_html).then(search,[query,num,hid_start],[html_out])
sub.click(search,[query,num,hid_start],[html_out]).then(show_num,hid_start,show_html)
b.launch()