Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,84 @@
|
|
| 1 |
-
|
| 2 |
-
# import os
|
| 3 |
-
# from authlib.integrations.flask_client import OAuth
|
| 4 |
-
# from langchain.llms.huggingface_hub import HuggingFaceHub
|
| 5 |
-
# from langchain.prompts import ChatPromptTemplate
|
| 6 |
-
# from langchain.schema import StrOutputParser
|
| 7 |
-
# from langchain.schema.runnable import Runnable
|
| 8 |
-
# from langchain.schema.runnable.config import RunnableConfig
|
| 9 |
-
# import chainlit as cl
|
| 10 |
-
|
| 11 |
-
# app = Flask(__name__)
|
| 12 |
-
# app.secret_key = 'YourSecretKey' # Change this to a real secret key for production
|
| 13 |
-
|
| 14 |
-
# # OAuth setup with Authlib
|
| 15 |
-
# oauth = OAuth(app)
|
| 16 |
-
# oauth.register(
|
| 17 |
-
# name='oauth_provider',
|
| 18 |
-
# client_id=os.getenv("OAUTH_CLIENT_ID"),
|
| 19 |
-
# client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
|
| 20 |
-
# authorize_url=os.getenv("OPENID_PROVIDER_URL") + '/authorize',
|
| 21 |
-
# access_token_url=os.getenv("OPENID_PROVIDER_URL") + '/token',
|
| 22 |
-
# client_kwargs={'scope': os.getenv("OAUTH_SCOPES").split(',')},
|
| 23 |
-
# redirect_uri=f"https://{os.getenv('SPACE_HOST')}/login/callback"
|
| 24 |
-
# )
|
| 25 |
-
|
| 26 |
-
# print(f"REDIRECT URI: https://{os.getenv('SPACE_HOST')}/login/callback")
|
| 27 |
-
|
| 28 |
-
# # Instantiate the LLM
|
| 29 |
-
# llm = HuggingFaceHub(
|
| 30 |
-
# model_kwargs={"max_length": 500},
|
| 31 |
-
# repo_id="google/flan-t5-xxl",
|
| 32 |
-
# huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_TOKEN"),
|
| 33 |
-
# )
|
| 34 |
-
|
| 35 |
-
# # Initialize ChainLit with LLM
|
| 36 |
-
# def initialize_chainlit():
|
| 37 |
-
# add_llm_provider(
|
| 38 |
-
# LangchainGenericProvider(
|
| 39 |
-
# id=llm._llm_type,
|
| 40 |
-
# name="HuggingFaceHub",
|
| 41 |
-
# llm=llm,
|
| 42 |
-
# is_chat=False,
|
| 43 |
-
# )
|
| 44 |
-
# )
|
| 45 |
-
|
| 46 |
-
# # Setup chainlit callbacks
|
| 47 |
-
# @cl.on_chat_start
|
| 48 |
-
# async def on_chat_start():
|
| 49 |
-
# prompt = ChatPromptTemplate.from_messages([("human", "{question}")])
|
| 50 |
-
# runnable = prompt | llm | StrOutputParser()
|
| 51 |
-
# cl.user_session.set("runnable", runnable)
|
| 52 |
-
|
| 53 |
-
# @cl.on_message
|
| 54 |
-
# async def on_message(message: cl.Message):
|
| 55 |
-
# runnable = cl.user_session.get("runnable") # type: Runnable
|
| 56 |
-
# msg = cl.Message(content="")
|
| 57 |
-
# async for chunk in runnable.astream(
|
| 58 |
-
# {"question": message.content},
|
| 59 |
-
# config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
| 60 |
-
# ):
|
| 61 |
-
# await msg.stream_token(chunk)
|
| 62 |
-
# await msg.send()
|
| 63 |
-
|
| 64 |
-
# @app.route('/')
|
| 65 |
-
# def home():
|
| 66 |
-
# return 'Home - <a href="/login">Login with OAuth Provider</a>'
|
| 67 |
-
|
| 68 |
-
# @app.route('/login')
|
| 69 |
-
# def login():
|
| 70 |
-
# redirect_uri = url_for('authorize', _external=True)
|
| 71 |
-
# return oauth.oauth_provider.authorize_redirect(redirect_uri)
|
| 72 |
-
|
| 73 |
-
# @app.route('/login/callback')
|
| 74 |
-
# def authorize():
|
| 75 |
-
# print('Logged in and language model initialized. Proceed with operations.')
|
| 76 |
-
# token = oauth.oauth_provider.authorize_access_token()
|
| 77 |
-
# # Initialize ChainLit or perform actions based on the authenticated user
|
| 78 |
-
# initialize_chainlit()
|
| 79 |
-
# return 'Logged in and language model initialized. Proceed with operations.'
|
| 80 |
-
|
| 81 |
-
# if __name__ == "__main__":
|
| 82 |
-
# app.run(debug=True)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
from flask import Flask, redirect, request, session, url_for, jsonify
|
| 86 |
import os
|
| 87 |
-
import
|
| 88 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
app = Flask(__name__)
|
| 91 |
app.secret_key = 'YourSecretKey' # Change this to a real secret key for production
|
| 92 |
|
| 93 |
-
# OAuth
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
@app.route('/')
|
| 101 |
def home():
|
| 102 |
-
|
| 103 |
-
state = os.urandom(16).hex()
|
| 104 |
-
session['state'] = state
|
| 105 |
-
# Redirect URL for "Sign-in with HF"
|
| 106 |
-
return redirect(f"{AUTHORIZE_URL}?redirect_uri={REDIRECT_URI}&scope=openid%20profile&client_id={CLIENT_ID}&state={state}")
|
| 107 |
-
|
| 108 |
-
@app.route('/login/callback')
|
| 109 |
-
def login_callback():
|
| 110 |
-
# Verify state matches
|
| 111 |
-
state = request.args.get('state')
|
| 112 |
-
if state != session.pop('state', None):
|
| 113 |
-
return 'State mismatch', 400
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
'grant_type': 'authorization_code',
|
| 120 |
-
'code': code,
|
| 121 |
-
'redirect_uri': REDIRECT_URI,
|
| 122 |
-
'client_id': CLIENT_ID
|
| 123 |
-
}
|
| 124 |
-
response = requests.post(TOKEN_URL, headers=headers, data=data)
|
| 125 |
-
response_data = response.json()
|
| 126 |
-
print(response_data)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
|
| 134 |
-
return 'Logged in successfully.', 200
|
| 135 |
|
| 136 |
-
# if __name__ == "__main__":
|
| 137 |
-
app.run(debug=True, port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, redirect, request, session, url_for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
from authlib.integrations.flask_client import OAuth
|
| 4 |
+
from langchain.llms.huggingface_hub import HuggingFaceHub
|
| 5 |
+
from langchain.prompts import ChatPromptTemplate
|
| 6 |
+
from langchain.schema import StrOutputParser
|
| 7 |
+
from langchain.schema.runnable import Runnable
|
| 8 |
+
from langchain.schema.runnable.config import RunnableConfig
|
| 9 |
+
import chainlit as cl
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
app.secret_key = 'YourSecretKey' # Change this to a real secret key for production
|
| 13 |
|
| 14 |
+
# OAuth setup with Authlib
|
| 15 |
+
oauth = OAuth(app)
|
| 16 |
+
oauth.register(
|
| 17 |
+
name='oauth_provider',
|
| 18 |
+
client_id=os.getenv("OAUTH_CLIENT_ID"),
|
| 19 |
+
client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
|
| 20 |
+
authorize_url=os.getenv("OPENID_PROVIDER_URL") + '/authorize',
|
| 21 |
+
access_token_url=os.getenv("OPENID_PROVIDER_URL") + '/token',
|
| 22 |
+
client_kwargs={'scope': os.getenv("OAUTH_SCOPES").split(',')},
|
| 23 |
+
redirect_uri=f"https://{os.getenv('SPACE_HOST')}/login/callback"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
print(f"REDIRECT URI: https://{os.getenv('SPACE_HOST')}/login/callback")
|
| 27 |
+
|
| 28 |
+
# Instantiate the LLM
|
| 29 |
+
llm = HuggingFaceHub(
|
| 30 |
+
model_kwargs={"max_length": 500},
|
| 31 |
+
repo_id="google/flan-t5-xxl",
|
| 32 |
+
huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_TOKEN"),
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Initialize ChainLit with LLM
|
| 36 |
+
def initialize_chainlit():
|
| 37 |
+
add_llm_provider(
|
| 38 |
+
LangchainGenericProvider(
|
| 39 |
+
id=llm._llm_type,
|
| 40 |
+
name="HuggingFaceHub",
|
| 41 |
+
llm=llm,
|
| 42 |
+
is_chat=False,
|
| 43 |
+
)
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Setup chainlit callbacks
|
| 47 |
+
@cl.on_chat_start
|
| 48 |
+
async def on_chat_start():
|
| 49 |
+
prompt = ChatPromptTemplate.from_messages([("human", "{question}")])
|
| 50 |
+
runnable = prompt | llm | StrOutputParser()
|
| 51 |
+
cl.user_session.set("runnable", runnable)
|
| 52 |
+
|
| 53 |
+
@cl.on_message
|
| 54 |
+
async def on_message(message: cl.Message):
|
| 55 |
+
runnable = cl.user_session.get("runnable") # type: Runnable
|
| 56 |
+
msg = cl.Message(content="")
|
| 57 |
+
async for chunk in runnable.astream(
|
| 58 |
+
{"question": message.content},
|
| 59 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
| 60 |
+
):
|
| 61 |
+
await msg.stream_token(chunk)
|
| 62 |
+
await msg.send()
|
| 63 |
|
| 64 |
@app.route('/')
|
| 65 |
def home():
|
| 66 |
+
return 'Home - <a href="/login">Login with OAuth Provider</a>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
@app.route('/login')
|
| 69 |
+
def login():
|
| 70 |
+
redirect_uri = url_for('authorize', _external=True)
|
| 71 |
+
return oauth.oauth_provider.authorize_redirect(redirect_uri)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
@app.route('/login/callback')
|
| 74 |
+
def authorize():
|
| 75 |
+
print('Logged in and language model initialized. Proceed with operations.')
|
| 76 |
+
token = oauth.oauth_provider.authorize_access_token()
|
| 77 |
+
# Initialize ChainLit or perform actions based on the authenticated user
|
| 78 |
+
initialize_chainlit()
|
| 79 |
+
return 'Logged in and language model initialized. Proceed with operations.'
|
| 80 |
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
app.run(debug=True)
|
| 83 |
|
|
|
|
| 84 |
|
|
|
|
|
|