Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import weaviate
|
2 |
import langchain
|
|
|
3 |
import gradio as gr
|
4 |
from langchain.embeddings import CohereEmbeddings
|
5 |
from langchain.document_loaders import UnstructuredFileLoader
|
@@ -12,6 +13,8 @@ import ssl
|
|
12 |
import mimetypes
|
13 |
from dotenv import load_dotenv
|
14 |
import cohere
|
|
|
|
|
15 |
|
16 |
# Load environment variables
|
17 |
load_dotenv()
|
@@ -19,6 +22,85 @@ openai_api_key = os.getenv('OPENAI')
|
|
19 |
cohere_api_key = os.getenv('COHERE')
|
20 |
weaviate_api_key = os.getenv('WEAVIATE')
|
21 |
weaviate_url = os.getenv('WEAVIATE_URL')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Weaviate connection
|
24 |
auth_config = weaviate.auth.AuthApiKey(api_key=weaviate_api_key)
|
|
|
1 |
import weaviate
|
2 |
import langchain
|
3 |
+
import apscheduler
|
4 |
import gradio as gr
|
5 |
from langchain.embeddings import CohereEmbeddings
|
6 |
from langchain.document_loaders import UnstructuredFileLoader
|
|
|
13 |
import mimetypes
|
14 |
from dotenv import load_dotenv
|
15 |
import cohere
|
16 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
17 |
+
import time
|
18 |
|
19 |
# Load environment variables
|
20 |
load_dotenv()
|
|
|
22 |
cohere_api_key = os.getenv('COHERE')
|
23 |
weaviate_api_key = os.getenv('WEAVIATE')
|
24 |
weaviate_url = os.getenv('WEAVIATE_URL')
|
25 |
+
weaviate_username = os.getenv('WEAVIATE_USERNAME')
|
26 |
+
weaviate_password = os.getenv('WEAVIATE_PASSWORD')
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
def refresh_token():
|
31 |
+
url = weaviate_url
|
32 |
+
# Get Weaviate's OIDC configuration
|
33 |
+
weaviate_open_id_config = requests.get(url + "/v1/.well-known/openid-configuration")
|
34 |
+
if weaviate_open_id_config.status_code == "404":
|
35 |
+
print("Your Weaviate instance is not configured with openid")
|
36 |
+
|
37 |
+
response_json = weaviate_open_id_config.json()
|
38 |
+
client_id = response_json["clientId"]
|
39 |
+
href = response_json["href"]
|
40 |
+
|
41 |
+
# Get the token issuer's OIDC configuration
|
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)
|