Spaces:
Runtime error
Runtime error
File size: 5,302 Bytes
cec6273 8733154 9d29527 56a4ec8 e84061b f5a0b90 0f5b1c1 f5a0b90 0f5b1c1 f5a0b90 0f5b1c1 f5a0b90 0f5b1c1 f5a0b90 9d29527 2adff4f 9d29527 56a4ec8 083c759 be0cb0e 47c742b be0cb0e 59f6c66 8733154 9d29527 47c742b 9d29527 8733154 9d29527 |
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 |
import os
from langchain.llms.huggingface_hub import HuggingFaceHub
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from langchain.schema.runnable import Runnable
from langchain.schema.runnable.config import RunnableConfig
from chainlit.playground.config import add_llm_provider
from chainlit.playground.providers.langchain import LangchainGenericProvider
import chainlit as cl
from authlib.integrations.requests_client import OAuth2Session
import os
# # Retrieving environment variables
# OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
# OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET")
# OAUTH_SCOPES = os.getenv("OAUTH_SCOPES").split(',') # Assuming OAUTH_SCOPES is a comma-separated list
# OPENID_PROVIDER_URL = os.getenv("OPENID_PROVIDER_URL")
# SPACE_HOST = os.getenv("SPACE_HOST")
# # Constructing the redirect URL using the SPACE_HOST variable
# redirect_uri = f"https://{SPACE_HOST}/login/callback"
# # Initializing the OAuth client/session with the retrieved environment variables
# oauth_client = OAuth2Session(client_id=OAUTH_CLIENT_ID,
# client_secret=OAUTH_CLIENT_SECRET, # Include client_secret if needed for the OAuth2Session setup
# scope=OAUTH_SCOPES,
# redirect_uri=redirect_uri)
# # Use the corrected method to generate the authorization URL
# authorization_url, state = oauth_client.create_authorization_url(OPENID_PROVIDER_URL + '/authorize')
# print(authorization_url, state)
# # The rest of your OAuth flow would go here, including redirecting the user to the authorization_url,
# # and then handling the redirect back to your application to exchange the code for a token.
from flask import Flask, request, redirect
import base64
import requests
app = Flask(__name__)
@app.route('/login/callback')
def login_callback():
# Retrieve the authorization code and state from the callback URL
code = request.args.get('code')
state = request.args.get('state')
# You should verify the state here (compare it to the one you stored before redirecting the user)
# For simplicity, this step is not shown
# Exchange the code for tokens
token_url = 'https://huggingface.co/oauth/token'
credentials = f"{OAUTH_CLIENT_ID}:{OAUTH_CLIENT_SECRET}"
basic_auth_header = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
headers = {
'Authorization': f'Basic {basic_auth_header}',
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': redirect_uri,
'client_id': OAUTH_CLIENT_ID
}
response = requests.post(token_url, headers=headers, data=data)
if response.ok:
tokens = response.json()
access_token = tokens['access_token']
id_token = tokens.get('id_token')
# Now you can use the access_token (and id_token) to access protected resources or identify the user
# For example, fetch user info from the userinfo endpoint if needed
return "Login successful" # Redirect to another page or show a message
else:
return "Error exchanging code for tokens", 400
# Instantiate the LLM
llm = HuggingFaceHub(
model_kwargs={"max_length": 500},
repo_id="google/flan-t5-xxl",
huggingfacehub_api_token=os.environ["HUGGINGFACE_API_TOKEN"],
)
# Add the LLM provider
add_llm_provider(
LangchainGenericProvider(
# It is important that the id of the provider matches the _llm_type
id=llm._llm_type,
# The name is not important. It will be displayed in the UI.
name="HuggingFaceHub",
# This should always be a Langchain llm instance (correctly configured)
llm=llm,
# If the LLM works with messages, set this to True
is_chat=False,
)
)
from typing import Dict, Optional
import chainlit as cl
# Retrieving environment variables
OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET")
OAUTH_SCOPES = os.getenv("OAUTH_SCOPES").split(',') # Assuming OAUTH_SCOPES is a comma-separated list
OPENID_PROVIDER_URL = os.getenv("OPENID_PROVIDER_URL")
SPACE_HOST = os.getenv("SPACE_HOST")
# Constructing the redirect URL using the SPACE_HOST variable
redirect_uri = f"https://{SPACE_HOST}/login/callback"
print(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPES, OPENID_PROVIDER_URL, SPACE_HOST, redirect_uri)
@cl.on_chat_start
async def on_chat_start():
app_user = cl.user_session.get("user")
await cl.Message(f"Hello {app_user.identifier}").send()
prompt = ChatPromptTemplate.from_messages(
[
("human", "{question}"),
]
)
runnable = prompt | llm | StrOutputParser()
cl.user_session.set("runnable", runnable)
@cl.on_message
async def on_message(message: cl.Message):
runnable = cl.user_session.get("runnable") # type: Runnable
msg = cl.Message(content="")
async for chunk in runnable.astream(
{"question": message.content},
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
):
await msg.stream_token(chunk)
await msg.send() |