Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,57 +37,6 @@ print(authorization_url, state)
|
|
| 37 |
# and then handling the redirect back to your application to exchange the code for a token.
|
| 38 |
|
| 39 |
|
| 40 |
-
from flask import Flask, request, redirect
|
| 41 |
-
import base64
|
| 42 |
-
import requests
|
| 43 |
-
|
| 44 |
-
app = Flask(__name__)
|
| 45 |
-
|
| 46 |
-
@app.route('/login/callback')
|
| 47 |
-
def login_callback():
|
| 48 |
-
# Retrieve the authorization code and state from the callback URL
|
| 49 |
-
code = request.args.get('code')
|
| 50 |
-
state = request.args.get('state')
|
| 51 |
-
|
| 52 |
-
# You should verify the state here (compare it to the one you stored before redirecting the user)
|
| 53 |
-
# For simplicity, this step is not shown
|
| 54 |
-
|
| 55 |
-
# Exchange the code for tokens
|
| 56 |
-
token_url = 'https://huggingface.co/oauth/token'
|
| 57 |
-
credentials = f"{OAUTH_CLIENT_ID}:{OAUTH_CLIENT_SECRET}"
|
| 58 |
-
basic_auth_header = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
| 59 |
-
|
| 60 |
-
headers = {
|
| 61 |
-
'Authorization': f'Basic {basic_auth_header}',
|
| 62 |
-
'Content-Type': 'application/x-www-form-urlencoded'
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
data = {
|
| 66 |
-
'grant_type': 'authorization_code',
|
| 67 |
-
'code': code,
|
| 68 |
-
'redirect_uri': redirect_uri,
|
| 69 |
-
'client_id': OAUTH_CLIENT_ID
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
response = requests.post(token_url, headers=headers, data=data)
|
| 73 |
-
|
| 74 |
-
if response.ok:
|
| 75 |
-
tokens = response.json()
|
| 76 |
-
access_token = tokens['access_token']
|
| 77 |
-
id_token = tokens.get('id_token')
|
| 78 |
-
|
| 79 |
-
# Now you can use the access_token (and id_token) to access protected resources or identify the user
|
| 80 |
-
# For example, fetch user info from the userinfo endpoint if needed
|
| 81 |
-
|
| 82 |
-
return "Login successful" # Redirect to another page or show a message
|
| 83 |
-
else:
|
| 84 |
-
return "Error exchanging code for tokens", 400
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
login_callback()
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
# Instantiate the LLM
|
| 92 |
llm = HuggingFaceHub(
|
| 93 |
model_kwargs={"max_length": 500},
|
|
@@ -151,6 +100,3 @@ async def on_message(message: cl.Message):
|
|
| 151 |
await msg.stream_token(chunk)
|
| 152 |
|
| 153 |
await msg.send()
|
| 154 |
-
|
| 155 |
-
if __name__ == '__main__':
|
| 156 |
-
app.run(debug=True)
|
|
|
|
| 37 |
# and then handling the redirect back to your application to exchange the code for a token.
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Instantiate the LLM
|
| 41 |
llm = HuggingFaceHub(
|
| 42 |
model_kwargs={"max_length": 500},
|
|
|
|
| 100 |
await msg.stream_token(chunk)
|
| 101 |
|
| 102 |
await msg.send()
|
|
|
|
|
|
|
|
|