Spaces:
Running
Running
Commit ·
e1ec379
1
Parent(s): 08e990f
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
# messages = [
|
| 9 |
+
# {"role": "system", "content": "You are a helpful and kind AI Assistant."},
|
| 10 |
+
# ]
|
| 11 |
+
|
| 12 |
+
url = 'https://www.nerdwallet.com/article/travel/how-much-are-my-united-miles-worth'
|
| 13 |
+
|
| 14 |
+
def get_data(url):
|
| 15 |
+
html = requests.get(url).text
|
| 16 |
+
doc = BeautifulSoup(html, 'html.parser')
|
| 17 |
+
|
| 18 |
+
headings_1 = [e.text for e in doc.find_all('h1')]
|
| 19 |
+
headings_2 = [e.text for e in doc.find_all('h2')]
|
| 20 |
+
headings_3 = [e.text for e in doc.find_all('h3')]
|
| 21 |
+
paragraphs = [e.text for e in doc.find_all('p')]
|
| 22 |
+
spans = [e.text for e in doc.find_all('span')]
|
| 23 |
+
|
| 24 |
+
messages = []
|
| 25 |
+
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."})
|
| 26 |
+
messages.append({'role': 'system', 'content': f"here are the h1s - {headings_1}"})
|
| 27 |
+
messages.append({'role': 'system', 'content': f"here are the h2s - {headings_2}"})
|
| 28 |
+
messages.append({'role': 'system', 'content': f"here are the h3s - {headings_3}"})
|
| 29 |
+
messages.append({'role': 'system', 'content': f"here are the paragraphs - {paragraphs}"})
|
| 30 |
+
messages.append({'role': 'system', 'content': f"here are the spans - {spans}"})
|
| 31 |
+
return messages
|
| 32 |
+
|
| 33 |
+
def chatbot(input):
|
| 34 |
+
if input:
|
| 35 |
+
messages.append({"role": "user", "content": input})
|
| 36 |
+
chat = openai.ChatCompletion.create(
|
| 37 |
+
model="gpt-3.5-turbo", messages=messages
|
| 38 |
+
)
|
| 39 |
+
reply = chat.choices[0].message.content
|
| 40 |
+
messages.append({"role": "assistant", "content": reply})
|
| 41 |
+
return reply
|
| 42 |
+
|
| 43 |
+
messages = get_data(url)
|
| 44 |
+
inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
|
| 45 |
+
outputs = gr.outputs.Textbox(label="Reply")
|
| 46 |
+
|
| 47 |
+
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="Chattosite AI Chatbot",
|
| 48 |
+
description="Ask anything you want",
|
| 49 |
+
theme="compact").launch(share=True)
|