jackculpan commited on
Commit
b98125f
·
1 Parent(s): 29e5662

working chatbot

Browse files
Files changed (1) hide show
  1. app.py +77 -11
app.py CHANGED
@@ -13,7 +13,8 @@ def update_button(button):
13
  button.update("Saved")
14
 
15
  def get_data(url):
16
- gr.update()
 
17
  global messages
18
  html = requests.get(url).text
19
  doc = BeautifulSoup(html, 'html.parser')
@@ -45,23 +46,88 @@ def chatbot(input):
45
  messages.append({"role": "assistant", "content": reply})
46
  return reply
47
 
48
- with gr.Blocks() as demo:
 
49
  url = gr.Textbox(label="Enter a webpage URL to chat about")
50
- save_btn = gr.Button("Save URL")
51
- save_btn.click(fn=get_data, inputs=url, outputs=update_button(save_btn))
52
  gr.Examples(["https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth",
53
  "https://www.bbc.com/news/business-64937251",
54
  "https://www.ycombinator.com/"], inputs=[url])
55
- with gr.Row():
56
- inputs = gr.inputs.Textbox(lines=7, label="Chat with AI about the webpage")
57
- outputs = gr.outputs.Textbox(label="Here's the reply from the AI")
58
- with gr.Row():
59
- greet_btn = gr.Button("Submit")
60
- greet_btn.click(fn=chatbot, inputs=inputs, outputs=outputs)
61
-
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # url_box = gr.Textbox(label="Url")
67
  # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
 
13
  button.update("Saved")
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')
 
46
  messages.append({"role": "assistant", "content": reply})
47
  return reply
48
 
49
+
50
+ with gr.Blocks(css=".gradio-container {background-color: transparent}") as demo:
51
  url = gr.Textbox(label="Enter a webpage URL to chat about")
52
+ url.change(fn=get_data, inputs=url)
 
53
  gr.Examples(["https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth",
54
  "https://www.bbc.com/news/business-64937251",
55
  "https://www.ycombinator.com/"], inputs=[url])
 
 
 
 
 
 
 
56
 
57
+ chatbot_item = gr.Chatbot()
58
+ msg = gr.Textbox(label="Chat with AI about the webpage")
59
+ gr.Examples(["Please summarise the webpage", "What is the tone of the webpage?", "Tell me your favorite part of the webpage"], inputs=[msg])
60
+ clear = gr.Button("Clear")
61
+
62
+ def user(user_message, history):
63
+ return "", history + [[user_message, None]]
64
+
65
+ def bot(history):
66
+ user_message = history[-1][0]
67
+ bot_message = chatbot(user_message)
68
+ history[-1][1] = bot_message
69
+ return history
70
+
71
+ msg.submit(user, [msg, chatbot_item], [msg, chatbot_item], queue=False).then(
72
+ bot, chatbot_item, chatbot_item
73
+ )
74
+ clear.click(lambda: None, None, chatbot_item, queue=False)
75
+
76
+ # with gr.Row():
77
+ # with gr.Column(scale=2):
78
+ # inputs = gr.Textbox(lines=7, label="Chat with AI about the webpage")
79
+ # gr.Examples(["Please summarise the webpage", "What is the tone of the webpage?", "Tell me your favorite part of the webpage"], inputs=[inputs])
80
+ # with gr.Column(scale=4):
81
+ # outputs = gr.Textbox(label="Here's the reply from the AI")
82
+ # with gr.Row():
83
+ # greet_btn = gr.Button("Submit")
84
+ # greet_btn.click(fn=chatbot, inputs=inputs, outputs=outputs, show_progress=True)
85
+
86
 
87
  demo.launch()
88
+
89
+ # with gr.Blocks() as demo:
90
+ # chatbot = gr.Chatbot()
91
+ # msg = gr.Textbox()
92
+ # clear = gr.Button("Clear")
93
+
94
+ # def user(user_message, history):
95
+ # return "", history + [[user_message, None]]
96
+
97
+ # def bot(history):
98
+ # bot_message = random.choice(["Yes", "No"])
99
+ # history[-1][1] = bot_message
100
+ # time.sleep(1)
101
+ # return history
102
+
103
+ # msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
104
+ # bot, chatbot, chatbot
105
+ # )
106
+ # clear.click(lambda: None, None, chatbot, queue=False)
107
+
108
+ # if __name__ == "__main__":
109
+ # demo.launch()
110
+
111
+ # interface = gr.Interface(fn=lambda x: x, inputs=demo, outputs=None, theme="compact")
112
+ # # interface.launch()
113
+
114
+ # url = gr.Textbox(label="Enter a webpage URL to chat about")
115
+ # # save_btn = gr.Button("Save URL")
116
+ # # save_btn.click(fn=get_data, inputs=url, outputs=update_button(save_btn))
117
+ # gr.Examples(["https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth",
118
+ # "https://www.bbc.com/news/business-64937251",
119
+ # "https://www.ycombinator.com/"], inputs=[url])
120
+ # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI about the webpage")
121
+ # outputs = gr.outputs.Textbox(label="Here's the reply from the AI")
122
+
123
+ # iface = gr.Interface(
124
+ # chatbot,
125
+ # [url, inputs],
126
+ # [outputs],
127
+ # live=True,
128
+ # )
129
+ # iface.launch()
130
+
131
 
132
  # url_box = gr.Textbox(label="Url")
133
  # inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")