jackculpan commited on
Commit
28df3d5
·
1 Parent(s): 224c370
Files changed (2) hide show
  1. app.py +165 -18
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,25 +1,172 @@
 
 
1
  import gradio as gr
2
- import random
3
- import time
4
 
5
- with gr.Blocks() as demo:
6
- chatbot = gr.Chatbot()
7
- msg = gr.Textbox()
8
- clear = gr.Button("Clear")
 
9
 
10
- def user(user_message, history):
11
- return "", history + [[user_message, None]]
12
 
13
- def bot(history):
14
- bot_message = random.choice(["Yes", "No"])
15
- history[-1][1] = bot_message
16
- time.sleep(1)
17
- return history
 
18
 
19
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
20
- bot, chatbot, chatbot
21
- )
22
- clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if __name__ == "__main__":
25
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
  import gradio as gr
4
+ import requests
5
+ from bs4 import BeautifulSoup
6
 
7
+ try:
8
+ from dotenv import load_dotenv
9
+ load_dotenv()
10
+ except ImportError:
11
+ pass # In production, python-dotenv may not be installed
12
 
13
+ openai.api_key = os.getenv("OPEN_API_KEY")
 
14
 
15
+ def get_data(url):
16
+ if not url.startswith("https://") or url.startswith("http://") or url.startswith("www."):
17
+ gr.Error("Please enter a valid URL")
18
+ global messages
19
+ html = requests.get(url).text
20
+ doc = BeautifulSoup(html, 'html.parser')
21
 
22
+ headings_1 = [e.text for e in doc.find_all('h1')]
23
+ headings_2 = [e.text for e in doc.find_all('h2')]
24
+ # headings_3 = [e.text for e in doc.find_all('h3')]
25
+ paragraphs = [e.text for e in doc.find_all('p')]
26
+ # spans = [e.text for e in doc.find_all('span')]
27
+
28
+ messages = []
29
+ messages.append({'role': 'system', 'content': "You are a helpful assistant that must answer questions about a website."})
30
+ messages.append({'role': 'system', 'content': f"here are the h1s - {headings_1}"})
31
+ messages.append({'role': 'system', 'content': f"here are the h2s - {headings_2}"})
32
+ # messages.append({'role': 'system', 'content': f"here are the h3s - {headings_3}"})
33
+ messages.append({'role': 'system', 'content': f"here are the paragraphs - {paragraphs}"})
34
+ # messages.append({'role': 'system', 'content': f"here are the spans - {spans}"})
35
+ return messages
36
+
37
+ def ask_chatbot(input):
38
+ if input:
39
+ messages.append({"role": "user", "content": input})
40
+ chat = openai.ChatCompletion.create(
41
+ model="gpt-3.5-turbo", messages=messages
42
+ )
43
+ reply = chat.choices[0].message.content
44
+ messages.append({"role": "assistant", "content": reply})
45
+ return reply
46
+
47
+
48
+ def user(user_message, history):
49
+ return "", history + [[user_message, None]]
50
+
51
+ def bot(history):
52
+ user_message = history[-1][0]
53
+ try:
54
+ bot_message = ask_chatbot(user_message)
55
+ except NameError:
56
+ bot_message = "Please try again"
57
+ history[-1][1] = bot_message
58
+ return history
59
+
60
+ with gr.Blocks(css="footer {visibility: hidden}", title="chatWEBPAGE") as demo:
61
+ gr.Markdown("Enter your website url, then ask the AI a question.")
62
+
63
+ url = gr.Textbox(label="1. Enter a webpage URL to chat about")
64
+ url.change(fn=get_data, inputs=url)
65
+ gr.Examples(["https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth",
66
+ "https://www.bbc.com/news/business-64937251",
67
+ "https://www.ycombinator.com/"], inputs=[url])
68
+
69
+ chatbot = gr.Chatbot().style(height=250)
70
+
71
+ msg = gr.Textbox(label="2. Chat with AI about the webpage")
72
+ a1 = msg.submit(user, [msg, chatbot], [msg, chatbot])
73
+ a1.then(bot, chatbot, chatbot)
74
+
75
+ gr.Examples(["Please summarise the webpage", "What is the tone of the webpage?", "Tell me your favorite part of the webpage"], inputs=[msg])
76
+
77
+ # with gr.Row():
78
+ # with gr.Column(scale=2):
79
+ # clear_button = gr.Button("Clear")
80
+ # clear_button.click(lambda: None, None, chatbot, queue=False)
81
+ # with gr.Column(scale=4):
82
+ # submit_button = gr.Button("Submit")
83
+ # submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).success(
84
+ # bot, chatbot, chatbot
85
+ # )
86
 
87
  if __name__ == "__main__":
88
+ demo.launch()
89
+
90
+ # msg.submit(user, [msg, chatbot_item], [msg, chatbot_item], queue=False)
91
+ # submit_button.click(user, [msg, chatbot_item], [msg, chatbot_item], queue=False)
92
+ # clear_button.click(bot, chatbot_item, chatbot_item, queue=False)
93
+
94
+
95
+ # if __name__ == "__main__":
96
+
97
+ # with gr.Row():
98
+ # with gr.Column(scale=2):
99
+ # inputs = gr.Textbox(lines=7, label="Chat with AI about the webpage")
100
+ # gr.Examples(["Please summarise the webpage", "What is the tone of the webpage?", "Tell me your favorite part of the webpage"], inputs=[inputs])
101
+ # with gr.Column(scale=4):
102
+ # outputs = gr.Textbox(label="Here's the reply from the AI")
103
+ # with gr.Row():
104
+ # greet_btn = gr.Button("Submit")
105
+ # greet_btn.click(fn=chatbot, inputs=inputs, outputs=outputs, show_progress=True)
106
+
107
+ # demo.queue(concurrency_count=3)
108
+
109
+
110
+ # with gr.Blocks() as demo:
111
+ # chatbot = gr.Chatbot()
112
+ # msg = gr.Textbox()
113
+ # clear = gr.Button("Clear")
114
+
115
+ # def user(user_message, history):
116
+ # return "", history + [[user_message, None]]
117
+
118
+ # def bot(history):
119
+ # bot_message = random.choice(["Yes", "No"])
120
+ # history[-1][1] = bot_message
121
+ # time.sleep(1)
122
+ # return history
123
+
124
+ # msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
125
+ # bot, chatbot, chatbot
126
+ # )
127
+ # clear.click(lambda: None, None, chatbot, queue=False)
128
+
129
+ # if __name__ == "__main__":
130
+ # demo.launch()
131
+
132
+ # interface = gr.Interface(fn=lambda x: x, inputs=demo, outputs=None, theme="compact")
133
+ # # interface.launch()
134
+
135
+ # url = gr.Textbox(label="Enter a webpage URL to chat about")
136
+ # # save_btn = gr.Button("Save URL")
137
+ # # save_btn.click(fn=get_data, inputs=url, outputs=update_button(save_btn))
138
+ # gr.Examples(["https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth",
139
+ # "https://www.bbc.com/news/business-64937251",
140
+ # "https://www.ycombinator.com/"], inputs=[url])
141
+ # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI about the webpage")
142
+ # outputs = gr.outputs.Textbox(label="Here's the reply from the AI")
143
+
144
+ # iface = gr.Interface(
145
+ # chatbot,
146
+ # [url, inputs],
147
+ # [outputs],
148
+ # live=True,
149
+ # )
150
+ # iface.launch()
151
+
152
+
153
+ # url_box = gr.Textbox(label="Url")
154
+ # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
155
+ # outputs = gr.outputs.Textbox(label="Reply")
156
+
157
+ # gr.Interface(fn=chatbot, inputs=[url_box,inputs], outputs=outputs, title="CHATwebpage AI Chatbot",
158
+ # description="Ask anything you want",
159
+ # theme="compact").launch()
160
+
161
+ # with gr.Blocks() as demo:
162
+ # used_letters_var = gr.State([])
163
+
164
+ # url = gr.Textbox(label="Url")
165
+ # save_btn = gr.Button("Save")
166
+ # save_btn.click(fn=get_data, inputs=url, outputs)
167
+ # # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
168
+ # # outputs = gr.outputs.Textbox(label="Reply")
169
+ # # greet_btn = gr.Button("Submit")
170
+ # # greet_btn.click(fn=chatbot, inputs=inputs, outputs=outputs)
171
+
172
+ # demo.launch()
requirements.txt CHANGED
@@ -4,4 +4,4 @@ langchain
4
  tiktoken
5
  requests
6
  bs4
7
- gradio
 
4
  tiktoken
5
  requests
6
  bs4
7
+ gradio=3.21.0