chatwebpage.com / app.py
jackculpan's picture
Add application file
e1ec379
raw
history blame
1.99 kB
import openai
import gradio as gr
import requests
from bs4 import BeautifulSoup
openai.api_key = "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"
# messages = [
# {"role": "system", "content": "You are a helpful and kind AI Assistant."},
# ]
url = 'https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth'
def get_data(url):
html = requests.get(url).text
doc = BeautifulSoup(html, 'html.parser')
headings_1 = [e.text for e in doc.find_all('h1')]
headings_2 = [e.text for e in doc.find_all('h2')]
headings_3 = [e.text for e in doc.find_all('h3')]
paragraphs = [e.text for e in doc.find_all('p')]
spans = [e.text for e in doc.find_all('span')]
messages = []
messages.append({'role': 'system', 'content': "You are a helpful assistant that must answer questions about a website. The website data will be submitted to you shortly."})
messages.append({'role': 'system', 'content': f"here are the h1s - {headings_1}"})
messages.append({'role': 'system', 'content': f"here are the h2s - {headings_2}"})
messages.append({'role': 'system', 'content': f"here are the h3s - {headings_3}"})
messages.append({'role': 'system', 'content': f"here are the paragraphs - {paragraphs}"})
messages.append({'role': 'system', 'content': f"here are the spans - {spans}"})
return messages
def chatbot(input):
if input:
messages.append({"role": "user", "content": input})
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
messages = get_data(url)
inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
outputs = gr.outputs.Textbox(label="Reply")
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="Chattosite AI Chatbot",
description="Ask anything you want",
theme="compact").launch(share=True)