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