lara1510 commited on
Commit
42e1dbe
1 Parent(s): 4d68f80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -114
app.py CHANGED
@@ -1,114 +1,114 @@
1
- import gradio as gr
2
- import pyperclip
3
- from scraper import scrape_websites
4
- from chatbot import create_chain, answer_query
5
- from langchain_core.messages import HumanMessage, AIMessage
6
- from pdf_converter import create_pdf
7
-
8
-
9
- data = None
10
- chains = [None]
11
-
12
-
13
-
14
-
15
- def load(pdf_doc):
16
- return create_chain(chains, pdf_doc, use_local_model=True)
17
-
18
-
19
- def answer(query, chat_history=[]):
20
- prepared_history = []
21
- for a, b in chat_history:
22
- prepared_history.append(HumanMessage(content=a))
23
- prepared_history.append(AIMessage(content=b))
24
- answer = answer_query(chain=chains[0], query=query, chat_history=prepared_history)
25
- return '', chat_history + [(query, answer)]
26
-
27
-
28
- async def scrape(topic, num_results):
29
- global data
30
- results = await scrape_websites(topic, num_results)
31
- data = results
32
- choices = [result.split('\n')[0] for result in results]
33
- return gr.update(choices=choices, value=None)
34
-
35
-
36
- def update_outlines(index):
37
- if index:
38
- return data[index]
39
- else:
40
- return 'choose a source to see its outlines'
41
-
42
-
43
- def copy_curr_page_link(curr_page_index):
44
- if data and curr_page_index:
45
- page_details = data[curr_page_index]
46
- page_link = page_details.split('\n')[1].split(' ')[1]
47
- pyperclip.copy(page_link)
48
- return update_outlines(curr_page_index)
49
-
50
-
51
- html = """
52
- <div style="text-align:center; max-width: 900px; margin: 0 auto; margin-top:5px">
53
- <h1>Research Assistant</h1>
54
- <p> Welcome to the Research Assistant app! This tool helps you find relevant information on your topic of interest.</p>
55
- </div>"""
56
- css = """container{max-width:900px; margin-left:auto; margin-right:auto;padding:20px}
57
- .centered{text-align:center;}"""
58
- theme = gr.themes.Monochrome(
59
- primary_hue=gr.themes.colors.orange,
60
- secondary_hue=gr.themes.colors.blue,
61
- neutral_hue=gr.themes.colors.gray,
62
- radius_size=gr.themes.sizes.radius_md
63
- )
64
-
65
- with gr.Blocks(css=css, theme=theme) as demo:
66
- gr.HTML(html)
67
- with gr.Tab("Scrape Google"):
68
- with gr.Row():
69
- with gr.Column(variant="panel"):
70
- topic = gr.Textbox(label="What is your Research Topic?", container=True)
71
- num_links = gr.Slider(label="Specify the Number of Links to Scrape!", minimum=0, maximum=15, step=1,
72
- container=True)
73
- text_button = gr.Button("Scrape")
74
-
75
- with gr.Column(min_width=600):
76
- websites_dropdown = gr.Dropdown(interactive=True, type='index', label='Sources')
77
- text_output = gr.Textbox(label="Outlines", lines=10, container=True, autoscroll=True, interactive=False)
78
- copy_button = gr.Button(value='copy this page link')
79
-
80
- text_button.click(scrape, inputs=[topic, num_links], outputs=websites_dropdown, scroll_to_output=False)
81
- websites_dropdown.change(fn=update_outlines, inputs=websites_dropdown, outputs=text_output)
82
- copy_button.click(copy_curr_page_link, inputs=websites_dropdown, outputs=text_output)
83
-
84
- with gr.Tab("Convert To PDF"):
85
- with gr.Column():
86
- url_input = gr.Textbox(label="Insert URL of webpage you want to convert to pdf")
87
- pdf_button = gr.Button("Convert")
88
- gr.Markdown('or', elem_classes='centered')
89
- pdf_doc = gr.File(label="Upload a pdf directly from your device:", file_types=['.pdf', '.docx'],type='filepath')
90
-
91
- with gr.Row():
92
- load_pdf = gr.Button('Load pdf file')
93
- status = gr.Textbox(label="Status", placeholder='', interactive=False)
94
-
95
- load_pdf.click(load, inputs=pdf_doc, outputs=status)
96
- pdf_button.click(create_pdf, inputs=url_input, outputs=pdf_doc)
97
-
98
- with gr.Tab('Chat with your PDF'):
99
- with gr.Column():
100
- chatbot = gr.Chatbot()
101
- user_input = gr.Textbox(label="type in your question")
102
- with gr.Row():
103
- submit_query = gr.Button("submit")
104
- clear = gr.ClearButton([user_input, chatbot])
105
-
106
-
107
- submit_query.click(answer, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
108
-
109
- #Optional
110
- user_input.submit(answer, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
111
-
112
-
113
- if __name__ == "__main__":
114
- demo.launch(share=False)
 
1
+ import gradio as gr
2
+ import pyperclip
3
+ from scraper import scrape_websites
4
+ from chatbot import create_chain, answer_query
5
+ from langchain_core.messages import HumanMessage, AIMessage
6
+ from pdf_converter import create_pdf
7
+
8
+
9
+ data = None
10
+ chains = [None]
11
+
12
+
13
+
14
+
15
+ def load(pdf_doc):
16
+ return create_chain(chains, pdf_doc)
17
+
18
+
19
+ def answer(query, chat_history=[]):
20
+ prepared_history = []
21
+ for a, b in chat_history:
22
+ prepared_history.append(HumanMessage(content=a))
23
+ prepared_history.append(AIMessage(content=b))
24
+ answer = answer_query(chain=chains[0], query=query, chat_history=prepared_history)
25
+ return '', chat_history + [(query, answer)]
26
+
27
+
28
+ async def scrape(topic, num_results):
29
+ global data
30
+ results = await scrape_websites(topic, num_results)
31
+ data = results
32
+ choices = [result.split('\n')[0] for result in results]
33
+ return gr.update(choices=choices, value=None)
34
+
35
+
36
+ def update_outlines(index):
37
+ if index:
38
+ return data[index]
39
+ else:
40
+ return 'choose a source to see its outlines'
41
+
42
+
43
+ def copy_curr_page_link(curr_page_index):
44
+ if data and curr_page_index:
45
+ page_details = data[curr_page_index]
46
+ page_link = page_details.split('\n')[1].split(' ')[1]
47
+ pyperclip.copy(page_link)
48
+ return update_outlines(curr_page_index)
49
+
50
+
51
+ html = """
52
+ <div style="text-align:center; max-width: 900px; margin: 0 auto; margin-top:5px">
53
+ <h1>Research Assistant</h1>
54
+ <p> Welcome to the Research Assistant app! This tool helps you find relevant information on your topic of interest.</p>
55
+ </div>"""
56
+ css = """container{max-width:900px; margin-left:auto; margin-right:auto;padding:20px}
57
+ .centered{text-align:center;}"""
58
+ theme = gr.themes.Monochrome(
59
+ primary_hue=gr.themes.colors.orange,
60
+ secondary_hue=gr.themes.colors.blue,
61
+ neutral_hue=gr.themes.colors.gray,
62
+ radius_size=gr.themes.sizes.radius_md
63
+ )
64
+
65
+ with gr.Blocks(css=css, theme=theme) as demo:
66
+ gr.HTML(html)
67
+ with gr.Tab("Scrape Google"):
68
+ with gr.Row():
69
+ with gr.Column(variant="panel"):
70
+ topic = gr.Textbox(label="What is your Research Topic?", container=True)
71
+ num_links = gr.Slider(label="Specify the Number of Links to Scrape!", minimum=0, maximum=15, step=1,
72
+ container=True)
73
+ text_button = gr.Button("Scrape")
74
+
75
+ with gr.Column(min_width=600):
76
+ websites_dropdown = gr.Dropdown(interactive=True, type='index', label='Sources')
77
+ text_output = gr.Textbox(label="Outlines", lines=10, container=True, autoscroll=True, interactive=False)
78
+ copy_button = gr.Button(value='copy this page link')
79
+
80
+ text_button.click(scrape, inputs=[topic, num_links], outputs=websites_dropdown, scroll_to_output=False)
81
+ websites_dropdown.change(fn=update_outlines, inputs=websites_dropdown, outputs=text_output)
82
+ copy_button.click(copy_curr_page_link, inputs=websites_dropdown, outputs=text_output)
83
+
84
+ with gr.Tab("Convert To PDF"):
85
+ with gr.Column():
86
+ url_input = gr.Textbox(label="Insert URL of webpage you want to convert to pdf")
87
+ pdf_button = gr.Button("Convert")
88
+ gr.Markdown('or', elem_classes='centered')
89
+ pdf_doc = gr.File(label="Upload a pdf directly from your device:", file_types=['.pdf', '.docx'],type='filepath')
90
+
91
+ with gr.Row():
92
+ load_pdf = gr.Button('Load pdf file')
93
+ status = gr.Textbox(label="Status", placeholder='', interactive=False)
94
+
95
+ load_pdf.click(load, inputs=pdf_doc, outputs=status)
96
+ pdf_button.click(create_pdf, inputs=url_input, outputs=pdf_doc)
97
+
98
+ with gr.Tab('Chat with your PDF'):
99
+ with gr.Column():
100
+ chatbot = gr.Chatbot()
101
+ user_input = gr.Textbox(label="type in your question")
102
+ with gr.Row():
103
+ submit_query = gr.Button("submit")
104
+ clear = gr.ClearButton([user_input, chatbot])
105
+
106
+
107
+ submit_query.click(answer, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
108
+
109
+ #Optional
110
+ user_input.submit(answer, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
111
+
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch(share=False)