Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -26,86 +26,19 @@ weaviate_username = os.getenv('WEAVIATE_USERNAME')
|
|
26 |
weaviate_password = os.getenv('WEAVIATE_PASSWORD')
|
27 |
|
28 |
|
29 |
-
|
30 |
-
def
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
response_auth = requests.get(href)
|
43 |
-
|
44 |
-
if "grant_types_supported" in response_auth.json():
|
45 |
-
# For resource owner password flow
|
46 |
-
assert "password" in response_auth.json()["grant_types_supported"]
|
47 |
-
|
48 |
-
username = "username" # <-- Replace with the actual username
|
49 |
-
password = "password" # <-- Replace with the actual password
|
50 |
-
|
51 |
-
# Construct the POST request to send to 'token_endpoint'
|
52 |
-
auth_body = {
|
53 |
-
"grant_type": "password",
|
54 |
-
"client_id": client_id,
|
55 |
-
"username": username,
|
56 |
-
"password": password,
|
57 |
-
}
|
58 |
-
response_post = requests.post(response_auth.json()["token_endpoint"], auth_body)
|
59 |
-
print("Your access_token is:")
|
60 |
-
print(response_post.json()["access_token"])
|
61 |
-
else:
|
62 |
-
# For hybrid flow
|
63 |
-
authorization_url = response_auth.json()["authorization_endpoint"]
|
64 |
-
parameters = {
|
65 |
-
"client_id": client_id,
|
66 |
-
"response_type": "code%20id_token",
|
67 |
-
"response_mode": "fragment",
|
68 |
-
"redirect_url": url,
|
69 |
-
"scope": "openid",
|
70 |
-
"nonce": "abcd",
|
71 |
-
}
|
72 |
-
# Construct 'auth_url'
|
73 |
-
parameter_string = "&".join([key + "=" + item for key, item in parameters.items()])
|
74 |
-
response_auth = requests.get(authorization_url + "?" + parameter_string)
|
75 |
-
|
76 |
-
print("Please visit the following url with your browser to login:")
|
77 |
-
print(authorization_url + "?" + parameter_string)
|
78 |
-
print(
|
79 |
-
"After the login you will be redirected, the token is the 'id_token' parameter of the redirection url."
|
80 |
-
)
|
81 |
-
|
82 |
-
# You could use this regular expression to parse the token
|
83 |
-
resp_txt = "Redirection URL"
|
84 |
-
token = re.search("(?<=id_token=).+(?=&)", resp_txt)[0]
|
85 |
-
|
86 |
-
print("Set as bearer token in the clients to access Weaviate.")
|
87 |
-
|
88 |
-
# Create a scheduler
|
89 |
-
scheduler = BackgroundScheduler()
|
90 |
-
|
91 |
-
# Schedule the token refresh function
|
92 |
-
scheduler.add_job(refresh_token, 'interval', minutes=30) # Adjust the interval as needed
|
93 |
-
|
94 |
-
# Start the scheduler
|
95 |
-
scheduler.start()
|
96 |
-
|
97 |
-
# Keep the script running
|
98 |
-
try:
|
99 |
-
while True:
|
100 |
-
time.sleep(2)
|
101 |
-
except (KeyboardInterrupt, SystemExit):
|
102 |
-
scheduler.shutdown()
|
103 |
-
|
104 |
-
|
105 |
-
# Weaviate connection
|
106 |
-
auth_config = weaviate.auth.AuthApiKey(api_key=weaviate_api_key)
|
107 |
-
client = weaviate.Client(url=weaviate_url, auth_client_secret=auth_config,
|
108 |
-
additional_headers={"X-Cohere-Api-Key": cohere_api_key})
|
109 |
|
110 |
# Initialize vectorstore
|
111 |
vectorstore = Weaviate(client, index_name="HereChat", text_key="text")
|
|
|
26 |
weaviate_password = os.getenv('WEAVIATE_PASSWORD')
|
27 |
|
28 |
|
29 |
+
# Function to refresh authentication
|
30 |
+
def refresh_authentication():
|
31 |
+
global my_credentials, client
|
32 |
+
my_credentials = weaviate.auth.AuthClientPassword(username=weaviate_username, password=weaviate_password)
|
33 |
+
client = weaviate.Client(weaviate_url, auth_client_secret=my_credentials)
|
34 |
+
|
35 |
+
# Initialize the scheduler for authentication refresh
|
36 |
+
scheduler = BackgroundScheduler()
|
37 |
+
scheduler.add_job(refresh_authentication, 'interval', minutes=30)
|
38 |
+
scheduler.start()
|
39 |
+
|
40 |
+
# Initial authentication
|
41 |
+
refresh_authentication()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Initialize vectorstore
|
44 |
vectorstore = Weaviate(client, index_name="HereChat", text_key="text")
|