Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Installing and Importing all these packages 🖤
|
2 |
+
|
3 |
+
from time import sleep # Inbuilt
|
4 |
+
from selenium import webdriver # pip install selenium
|
5 |
+
from selenium.webdriver.chrome.options import Options # pip install selenium
|
6 |
+
from selenium.webdriver.common.by import By # pip install selenium
|
7 |
+
import warnings # Inbuilt
|
8 |
+
from selenium.webdriver.chrome.service import Service
|
9 |
+
import streamlit as st
|
10 |
+
|
11 |
+
# Configuring the webdriver to automate the program for utilizing ChatGPT as a backend model.
|
12 |
+
|
13 |
+
warnings.simplefilter("ignore")
|
14 |
+
|
15 |
+
try:
|
16 |
+
Link = "https://gpt4login.com/use-chatgpt-online-free/"
|
17 |
+
chrome_driver_path = 'Brain\\chromedriver.exe'
|
18 |
+
chrome_options = Options()
|
19 |
+
chrome_options.headless = True
|
20 |
+
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
|
21 |
+
chrome_options.add_argument('--log-level=3')
|
22 |
+
service = Service(chrome_driver_path)
|
23 |
+
driver = webdriver.Chrome(service=service, options=chrome_options)
|
24 |
+
driver.maximize_window()
|
25 |
+
driver.get(Link)
|
26 |
+
|
27 |
+
except Exception as e:
|
28 |
+
print("To resolve this error, you should set up the ChromeDriver properly.")
|
29 |
+
print("*For the successful execution of this program, it is essential to configure and set up the ChromeDriver.")
|
30 |
+
print(e)
|
31 |
+
|
32 |
+
# Storing the previous conversation ID for reference.
|
33 |
+
|
34 |
+
def FileReader():
|
35 |
+
|
36 |
+
try:
|
37 |
+
File = open("Chatnumber.txt","r")
|
38 |
+
Data = File.read()
|
39 |
+
File.close()
|
40 |
+
return Data
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
print(e)
|
44 |
+
|
45 |
+
# Reading the previous conversation ID for reference.
|
46 |
+
|
47 |
+
def FileWriter(Data):
|
48 |
+
|
49 |
+
try:
|
50 |
+
File = open("Chatnumber.txt","w")
|
51 |
+
File.write(Data)
|
52 |
+
File.close()
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
print(e)
|
56 |
+
|
57 |
+
# Initiating the primary execution phase while utilizing ChatGPT as the backend model.
|
58 |
+
|
59 |
+
def ChatGPTBrain(Query):
|
60 |
+
Query = str(Query)
|
61 |
+
|
62 |
+
try:
|
63 |
+
driver.find_element(by=By.XPATH,value="/html/body/div[1]/div/div/main/article/div/div/div/div/div/div/div[2]/div/div/div[2]/div/textarea").send_keys(Query)
|
64 |
+
sleep(1)
|
65 |
+
|
66 |
+
except:
|
67 |
+
print("*The Input button or Input Section does not appear to be readily accessible or available within the current context or environment.*")
|
68 |
+
print("*Consider modifying the path for the 'Input' element, which is available on the website.*")
|
69 |
+
|
70 |
+
try:
|
71 |
+
driver.find_element(by=By.XPATH,value="/html/body/div[1]/div/div/main/article/div/div/div/div/div/div/div[2]/div/div/div[2]/button/span").click()
|
72 |
+
|
73 |
+
except:
|
74 |
+
print("*The Send.Keys() function or button does not appear to be readily accessible or available within the current context or environment.*")
|
75 |
+
print("*Consider modifying the path for the 'Send' button, which is available on the website.*")
|
76 |
+
|
77 |
+
try:
|
78 |
+
Data = str(FileReader())
|
79 |
+
|
80 |
+
except:
|
81 |
+
print("*Could Not Be able to access the file Where Chatnumber is saved for reference.")
|
82 |
+
|
83 |
+
while True:
|
84 |
+
|
85 |
+
sleep(0.5)
|
86 |
+
|
87 |
+
try:
|
88 |
+
|
89 |
+
try:
|
90 |
+
AnswerXpath = f"/html/body/div[1]/div/div/main/article/div/div/div/div/div/div/div[2]/div/div/div[1]/div[{Data}]/span[2]"
|
91 |
+
Answer = driver.find_element(by=By.XPATH,value=AnswerXpath).is_displayed()
|
92 |
+
if str(Answer)=="True":
|
93 |
+
break
|
94 |
+
|
95 |
+
except:
|
96 |
+
# print("*The response text element cannot be located. Please ensure that you update its selector or locator.")
|
97 |
+
pass
|
98 |
+
|
99 |
+
except:
|
100 |
+
pass
|
101 |
+
|
102 |
+
try:
|
103 |
+
AnswerXpath = f"/html/body/div[1]/div/div/main/article/div/div/div/div/div/div/div[2]/div/div/div[1]/div[{Data}]/span[2]"
|
104 |
+
Answer = driver.find_element(by=By.XPATH,value=AnswerXpath).text
|
105 |
+
|
106 |
+
except:
|
107 |
+
print("*The response text element cannot be located. Please ensure that you update its selector or locator.")
|
108 |
+
|
109 |
+
NewData = int(Data) + 2
|
110 |
+
FileWriter(Data=str(NewData))
|
111 |
+
return Answer
|
112 |
+
|
113 |
+
# Commencing the main execution phase.
|
114 |
+
|
115 |
+
def MainExecutionChatGPT():
|
116 |
+
|
117 |
+
st.write("")
|
118 |
+
st.write("Initiating the GPT-4 model.")
|
119 |
+
FileWriter(Data='3')
|
120 |
+
|
121 |
+
if uinput = st.chat_input("Enter your query: "):
|
122 |
+
answer = ChatGPTBrain(uinput)
|
123 |
+
with st.chat_message("assistant"):
|
124 |
+
st.write(answer)
|
125 |
+
|
126 |
+
# while True:
|
127 |
+
|
128 |
+
|
129 |
+
# try:
|
130 |
+
# File = open("Body\\SpeechRecognition.txt","r")
|
131 |
+
# Data = File.read()
|
132 |
+
# File.close()
|
133 |
+
# FileHistory = open("Brain\\HistoryChat.txt","r")
|
134 |
+
# DataHistory = FileHistory.read()
|
135 |
+
# FileHistory.close()
|
136 |
+
|
137 |
+
# if str(Data)==str(DataHistory):
|
138 |
+
# sleep(0.5)
|
139 |
+
# pass
|
140 |
+
|
141 |
+
# else:
|
142 |
+
# Result = ChatGPTBrain(Data)
|
143 |
+
# print(Result)
|
144 |
+
|
145 |
+
# FileHistory = open("Brain\\HistoryChat.txt","w")
|
146 |
+
# FileHistory.write(Data)
|
147 |
+
# FileHistory.close()
|
148 |
+
|
149 |
+
# except Exception as e:
|
150 |
+
# print(e)
|
151 |
+
|
152 |
+
MainExecutionChatGPT()
|