Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from src.paper import Paper
|
3 |
+
from src.reader import Reader
|
4 |
+
from src.utils import language_dict
|
5 |
+
|
6 |
+
class ChatArxiv:
|
7 |
+
def init(self, api_key, url, lang='English'):
|
8 |
+
self.paper = Paper(url)
|
9 |
+
self.reader = Reader(self.paper, api_key, language=lang)
|
10 |
+
embed_html = f'''
|
11 |
+
<div style='display:flex; height:80vh; border: 1px solid #e5e7eb; border-radius: 8px'>
|
12 |
+
<embed
|
13 |
+
src=file/{self.paper.path}
|
14 |
+
type="application/pdf"
|
15 |
+
width="100%"
|
16 |
+
height="100%"
|
17 |
+
/>
|
18 |
+
</div>
|
19 |
+
'''
|
20 |
+
reply = "我已经阅读完论文的基本信息 😋\n您可以开始提问一些基本问题了 \n 点击 📖 Read Full Paper 让我阅读整篇论文可以提高回答质量"
|
21 |
+
basic_info = self.reader.get_basic_info()
|
22 |
+
chapter_list = self.ret_chapter_list()
|
23 |
+
|
24 |
+
return embed_html, reply, basic_info, chapter_list
|
25 |
+
|
26 |
+
def ret_chapter_list(self):
|
27 |
+
chapter_ops = [str(k) for k in self.paper.paper_instance.get('content').keys()]
|
28 |
+
return gr.Dropdown.update(choices=chapter_ops)
|
29 |
+
|
30 |
+
def read_chap(self, chapter_list):
|
31 |
+
if getattr(self, 'paper', None) is None:
|
32 |
+
return "请先设置论文链接和 API key,点击 ⚙ Set 按钮"
|
33 |
+
return self.reader.read_paper(chapter_list)
|
34 |
+
|
35 |
+
def ask(self, prompt):
|
36 |
+
return self.reader.chat_with_paper(prompt)
|
37 |
+
|
38 |
+
if __name__ == '__main__':
|
39 |
+
chatArxiv = ChatArxiv()
|
40 |
+
title = "<div align='center'><h1> ChatArxiv 📑 </h1></div>"
|
41 |
+
desc = "<div align='center'>帮助您快速阅读 Arxiv 论文</div>"
|
42 |
+
with gr.Blocks() as app:
|
43 |
+
gr.HTML(title)
|
44 |
+
gr.HTML(desc)
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column(scale=1):
|
47 |
+
ip_config = [
|
48 |
+
gr.inputs.Textbox(label="请输入OpenAI api key", default=""),
|
49 |
+
gr.inputs.Textbox(label="请输入论文 Arxiv 链接", default=""),
|
50 |
+
]
|
51 |
+
# We do not need the ISO 639-1 language code since we interact with LLM by natural language!!
|
52 |
+
lang = gr.Dropdown(list(language_dict.values()), label="语言", value='中文')
|
53 |
+
configure_btn = gr.Button("⚙ 初始设置")
|
54 |
+
simple_rate = gr.Textbox(label="基本简介与评价", default="", interactive=False)
|
55 |
+
|
56 |
+
#test_btn = gr.Button("Test Btn")
|
57 |
+
with gr.Column(scale=1):
|
58 |
+
chapter_sel = gr.Dropdown(label="请选择阅读章节", multiselect=True, interactive=True)
|
59 |
+
read_btn = gr.Button("📖 阅读章节")
|
60 |
+
op_submit = gr.Textbox(label="🤖 Arxiv Bot ", default="")
|
61 |
+
ip_submit = gr.inputs.Textbox(label="请输入问题", default="")
|
62 |
+
submit_btn = gr.Button("🚀 提交 ")
|
63 |
+
|
64 |
+
with gr.Column(scale=1.5):
|
65 |
+
embed_html = '''
|
66 |
+
<div style='display:flex; height:80vh; border: 1px solid #e5e7eb; border-radius: 8px'>
|
67 |
+
<embed
|
68 |
+
src=file/assets/blank.pdf
|
69 |
+
type="application/pdf"
|
70 |
+
width="100%"
|
71 |
+
height="100%"
|
72 |
+
/>
|
73 |
+
</div>
|
74 |
+
'''
|
75 |
+
pdf_preview = gr.HTML(value=embed_html)
|
76 |
+
|
77 |
+
configure_btn.click(fn=chatArxiv.init, inputs=[*ip_config, lang], outputs=[pdf_preview, op_submit, simple_rate, chapter_sel])
|
78 |
+
submit_btn.click(fn=chatArxiv.ask, inputs=ip_submit, outputs=op_submit)
|
79 |
+
read_btn.click(fn=chatArxiv.read_chap, inputs=chapter_sel, outputs=op_submit)
|
80 |
+
#test_btn.click(fn=chatArxiv.test)
|
81 |
+
app.launch()
|