File size: 4,927 Bytes
56833e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d84ed
5804b7a
56833e9
8dc407a
56833e9
 
 
 
 
 
8dc407a
56833e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d84ed
56833e9
 
 
9cbfeeb
 
56833e9
 
c6d84ed
56833e9
 
9cbfeeb
 
56833e9
 
 
 
 
9cbfeeb
56833e9
 
 
 
 
 
 
 
c6d84ed
56833e9
 
 
 
 
 
 
 
 
 
 
 
c6d84ed
56833e9
 
 
9cbfeeb
56833e9
 
 
 
 
 
 
 
 
 
 
 
 
90c0607
56833e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Installing and Importing all these packages 🖤

from time import sleep # Inbuilt
from selenium import webdriver # pip install selenium
from selenium.webdriver.chrome.options import Options # pip install selenium
from selenium.webdriver.common.by import By # pip install selenium
import warnings # Inbuilt
from selenium.webdriver.chrome.service import Service
import streamlit as st 

# Configuring the webdriver to automate the program for utilizing ChatGPT as a backend model.

warnings.simplefilter("ignore")

try:
    Link = "https://chat.openai.com/c/58ea6e4a-67e3-460e-99c8-8e701f863534"
    chrome_driver_path = 'chromedriver.exe'
    chrome_options = Options()
    # chrome_options.headless = True
    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    chrome_options.add_argument('--log-level=3')
    service = Service(chrome_driver_path)
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.maximize_window()
    driver.get(Link)
    st.write("Worked till here")

except Exception as e:
    print("To resolve this error, you should set up the ChromeDriver properly.")
    print("*For the successful execution of this program, it is essential to configure and set up the ChromeDriver.")
    print(e)

# Storing the previous conversation ID for reference.

def FileReader():
        
    try:
        File = open("Chatnumber.txt","r")
        Data = File.read()
        File.close()
        return Data

    except Exception as e:
        print(e)

# Reading the previous conversation ID for reference.

def FileWriter(Data):

    try:
        File = open("Chatnumber.txt","w")
        File.write(Data)
        File.close()

    except Exception as e:
        print(e)

# Initiating the primary execution phase while utilizing ChatGPT as the backend model.

def ChatGPTBrain(Query):
    Query = str(Query)

    try:
        driver.find_element(by=By.XPATH,value="/html/body/div[1]/div[1]/div/main/div[1]/div[2]/form/div/div[1]/div/textarea").send_keys(Query)
        sleep(1)

    except:
        st.write("*The Input button or Input Section does not appear to be readily accessible or available within the current context or environment.*")
        st.write("*Consider modifying the path for the 'Input' element, which is available on the website.*")

    try:
        driver.find_element(by=By.XPATH,value="/html/body/div[1]/div[1]/div/main/div[1]/div[2]/form/div/div[1]/div/button").click()

    except:
        st.write("*The Send.Keys() function or button does not appear to be readily accessible or available within the current context or environment.*")
        st.write("*Consider modifying the path for the 'Send' button, which is available on the website.*")

    try:
        Data = str(FileReader())

    except:
        st.write("*Could Not Be able to access the file Where Chatnumber is saved for reference.")

    while True:

        sleep(0.5)
        
        try:

            try:
                AnswerXpath = f"/html/body/div[1]/div[1]/div/main/div[1]/div[1]/div/div/div/div[2]/div{Data}"
                Answer = driver.find_element(by=By.XPATH,value=AnswerXpath).is_displayed()
                if str(Answer)=="True":
                    break

            except:
                # print("*The response text element cannot be located. Please ensure that you update its selector or locator.")
                pass

        except:
            pass
    
    try:
        AnswerXpath = f"/html/body/div[1]/div[1]/div/main/div[1]/div[1]/div/div/div/div[4]/div{Data}"
        Answer = driver.find_element(by=By.XPATH,value=AnswerXpath).text

    except:
        st.write("*The response text element cannot be located. Please ensure that you update its selector or locator.")

    NewData = int(Data) + 2
    FileWriter(Data=str(NewData))
    return Answer

# Commencing the main execution phase.

def MainExecutionChatGPT():

    st.write("")
    st.write("Initiating the GPT-4 model.")
    FileWriter(Data='3')

    if uinput := st.chat_input("Enter your query: "):
        answer = ChatGPTBrain(uinput)
        with st.chat_message("assistant"):
            st.write(answer)
    
    # while True:
        
            
        # try:
        #     File = open("Body\\SpeechRecognition.txt","r")
        #     Data = File.read()
        #     File.close()
        #     FileHistory = open("Brain\\HistoryChat.txt","r")
        #     DataHistory = FileHistory.read()
        #     FileHistory.close()

        #     if str(Data)==str(DataHistory):
        #         sleep(0.5)
        #         pass

        #     else:
        #         Result = ChatGPTBrain(Data)
        #         print(Result)

        #         FileHistory = open("Brain\\HistoryChat.txt","w")
        #         FileHistory.write(Data)
        #         FileHistory.close()

        # except Exception as e:
        #     print(e)
        
MainExecutionChatGPT()