File size: 2,609 Bytes
6c65c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

import openai
import streamlit as st
import requests
from bs4 import BeautifulSoup


# Fetching The website
@st.cache_data()
def fetch_website_content(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.content
    else:
        return response.status_code

# return the relevant information of website
def fetch_relevant_information(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    text_content = soup.get_text()
    return text_content

# ChatGpt to to answer our query
def get_chatGpt_response(user_input, web_scraping_data):

    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a chatbot."},
                {"role": "user", "content": user_input},
                {"role": "assistant", "content": web_scraping_data},
            ],
            max_tokens=150,
            temperature=0.7
        )
        reply = response['choices'][0].message.content
        return reply
    except openai.OpenAIError as e:
        st.error("An error occurred while generating the Chatbot response.")
        st.error(f"Error details: {e}")
        return None




# GUI and main function Implimentation
def main():

    openai.api_key="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"

    st.set_page_config(
        page_title="Relinns ChatBot",
        page_icon=":robot_face:"
    )

    st.header("Relinns ChatBOT :robot_face:")
    st.write("Hello User")
    user_input_url = st.text_input("Please provide a URL:")


    if st.button("Fetch Website Content"):
        website_content = fetch_website_content(user_input_url)

        if website_content:
            st.success("Data Fetched Successfully")

    user_input = st.text_area("Chatbot: How can I assist you?", value="")

    if st.button("Get Response"):
        website_content = fetch_website_content(user_input_url)
        extracted_info = fetch_relevant_information(website_content)
        web_scraping_data = " ".join(extracted_info.split())

        if web_scraping_data:
            try:
                chatbot_response = get_chatGpt_response(user_input, web_scraping_data)

                # Display the chatbot's response
                st.write("Chatbot:", chatbot_response)

            except Exception as e:
                st.error(f"Error details: {e}")  # The error is already handled in the get_chatGpt_response function
        else:
            st.error("Please fetch website content first.")

if __name__ == '__main__':
    main()