Spaces:
Paused
Paused
authentication
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausal
|
|
| 7 |
from langchain_community.document_loaders import DirectoryLoader
|
| 8 |
import torch
|
| 9 |
import re
|
|
|
|
|
|
|
| 10 |
import transformers
|
| 11 |
import spaces
|
| 12 |
|
|
@@ -24,20 +26,11 @@ books_db_client = books_db.as_retriever()
|
|
| 24 |
|
| 25 |
# Initialize the model and tokenizer
|
| 26 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
| 27 |
-
|
| 28 |
-
# bnb_config = transformers.BitsAndBytesConfig(
|
| 29 |
-
# load_in_4bit=True,
|
| 30 |
-
# bnb_4bit_quant_type='nf4',
|
| 31 |
-
# bnb_4bit_use_double_quant=True,
|
| 32 |
-
# bnb_4bit_compute_dtype=torch.bfloat16
|
| 33 |
-
# )
|
| 34 |
-
|
| 35 |
model_config = transformers.AutoConfig.from_pretrained(model_name, max_new_tokens=1024)
|
| 36 |
model = transformers.AutoModelForCausalLM.from_pretrained(
|
| 37 |
model_name,
|
| 38 |
trust_remote_code=True,
|
| 39 |
config=model_config,
|
| 40 |
-
# quantization_config=bnb_config,
|
| 41 |
device_map=device,
|
| 42 |
)
|
| 43 |
|
|
@@ -66,6 +59,62 @@ books_db_client_retriever = RetrievalQA.from_chain_type(
|
|
| 66 |
verbose=True
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Function to retrieve answer using the RAG system
|
| 70 |
@spaces.GPU(duration=60)
|
| 71 |
def test_rag(query):
|
|
@@ -81,7 +130,7 @@ def test_rag(query):
|
|
| 81 |
|
| 82 |
return corrected_text_books
|
| 83 |
|
| 84 |
-
#
|
| 85 |
def chat(query, history=None):
|
| 86 |
if history is None:
|
| 87 |
history = []
|
|
@@ -90,10 +139,6 @@ def chat(query, history=None):
|
|
| 90 |
history.append((query, answer))
|
| 91 |
return history, "" # Clear input after submission
|
| 92 |
|
| 93 |
-
# Function to clear input text
|
| 94 |
-
def clear_input():
|
| 95 |
-
return "", # Return empty string to clear input field
|
| 96 |
-
|
| 97 |
# Gradio interface
|
| 98 |
with gr.Blocks() as interface:
|
| 99 |
gr.Markdown("## RAG Chatbot")
|
|
@@ -101,10 +146,33 @@ with gr.Blocks() as interface:
|
|
| 101 |
|
| 102 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...")
|
| 103 |
submit_btn = gr.Button("Submit")
|
| 104 |
-
# clear_btn = gr.Button("Clear")
|
| 105 |
chat_history = gr.Chatbot(label="Chat History")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
|
|
|
| 107 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 108 |
-
# clear_btn.click(clear_input, outputs=input_box)
|
| 109 |
|
| 110 |
interface.launch()
|
|
|
|
| 7 |
from langchain_community.document_loaders import DirectoryLoader
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
+
import requests
|
| 11 |
+
from urllib.parse import urlencode, parse_qs, urlparse
|
| 12 |
import transformers
|
| 13 |
import spaces
|
| 14 |
|
|
|
|
| 26 |
|
| 27 |
# Initialize the model and tokenizer
|
| 28 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
model_config = transformers.AutoConfig.from_pretrained(model_name, max_new_tokens=1024)
|
| 30 |
model = transformers.AutoModelForCausalLM.from_pretrained(
|
| 31 |
model_name,
|
| 32 |
trust_remote_code=True,
|
| 33 |
config=model_config,
|
|
|
|
| 34 |
device_map=device,
|
| 35 |
)
|
| 36 |
|
|
|
|
| 59 |
verbose=True
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# OAuth Configuration
|
| 63 |
+
TENANT_ID = '2b093ced-2571-463f-bc3e-b4f8bcb427ee'
|
| 64 |
+
CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5'
|
| 65 |
+
CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW'
|
| 66 |
+
REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/'
|
| 67 |
+
AUTH_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/authorize"
|
| 68 |
+
TOKEN_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/token"
|
| 69 |
+
GRAPH_API_URL = "https://graph.microsoft.com/v1.0/me"
|
| 70 |
+
|
| 71 |
+
# Function to redirect to Microsoft login
|
| 72 |
+
def get_login_url():
|
| 73 |
+
params = {
|
| 74 |
+
'client_id': CLIENT_ID,
|
| 75 |
+
'response_type': 'code',
|
| 76 |
+
'redirect_uri': REDIRECT_URI,
|
| 77 |
+
'response_mode': 'query',
|
| 78 |
+
'scope': 'User.Read',
|
| 79 |
+
'state': '12345' # Optional state parameter for CSRF protection
|
| 80 |
+
}
|
| 81 |
+
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 82 |
+
return login_url
|
| 83 |
+
|
| 84 |
+
# Function to exchange auth code for an access token
|
| 85 |
+
def exchange_code_for_token(auth_code):
|
| 86 |
+
data = {
|
| 87 |
+
'grant_type': 'authorization_code',
|
| 88 |
+
'client_id': CLIENT_ID,
|
| 89 |
+
'client_secret': CLIENT_SECRET,
|
| 90 |
+
'code': auth_code,
|
| 91 |
+
'redirect_uri': REDIRECT_URI
|
| 92 |
+
}
|
| 93 |
+
response = requests.post(TOKEN_URL, data=data)
|
| 94 |
+
token_data = response.json()
|
| 95 |
+
return token_data.get('access_token')
|
| 96 |
+
|
| 97 |
+
# Step 3: Function to get user profile
|
| 98 |
+
def get_user_profile(access_token):
|
| 99 |
+
headers = {
|
| 100 |
+
'Authorization': f'Bearer {access_token}'
|
| 101 |
+
}
|
| 102 |
+
response = requests.get(GRAPH_API_URL, headers=headers)
|
| 103 |
+
return response.json()
|
| 104 |
+
|
| 105 |
+
# Function to handle OAuth callback
|
| 106 |
+
def handle_oauth_callback(url):
|
| 107 |
+
parsed_url = urlparse(url)
|
| 108 |
+
query_params = parse_qs(parsed_url.query)
|
| 109 |
+
auth_code = query_params.get('code', [None])[0]
|
| 110 |
+
|
| 111 |
+
if auth_code:
|
| 112 |
+
access_token = exchange_code_for_token(auth_code)
|
| 113 |
+
user_profile = get_user_profile(access_token)
|
| 114 |
+
return user_profile
|
| 115 |
+
else:
|
| 116 |
+
return "Authorization failed."
|
| 117 |
+
|
| 118 |
# Function to retrieve answer using the RAG system
|
| 119 |
@spaces.GPU(duration=60)
|
| 120 |
def test_rag(query):
|
|
|
|
| 130 |
|
| 131 |
return corrected_text_books
|
| 132 |
|
| 133 |
+
# Function for RAG Chat
|
| 134 |
def chat(query, history=None):
|
| 135 |
if history is None:
|
| 136 |
history = []
|
|
|
|
| 139 |
history.append((query, answer))
|
| 140 |
return history, "" # Clear input after submission
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# Gradio interface
|
| 143 |
with gr.Blocks() as interface:
|
| 144 |
gr.Markdown("## RAG Chatbot")
|
|
|
|
| 146 |
|
| 147 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...")
|
| 148 |
submit_btn = gr.Button("Submit")
|
|
|
|
| 149 |
chat_history = gr.Chatbot(label="Chat History")
|
| 150 |
+
|
| 151 |
+
# Add Microsoft OAuth Login
|
| 152 |
+
auth_btn = gr.Button("Login with Microsoft")
|
| 153 |
+
|
| 154 |
+
# OAuth callback URL input (for demonstration, replace with actual callback handler)
|
| 155 |
+
callback_url = gr.Textbox(label="OAuth Callback URL", placeholder="Paste the callback URL here...")
|
| 156 |
+
|
| 157 |
+
# Display user profile after login
|
| 158 |
+
profile_output = gr.JSON(label="User Profile")
|
| 159 |
+
|
| 160 |
+
# Action for OAuth login
|
| 161 |
+
def login_action():
|
| 162 |
+
return gr.redirect(get_login_url())
|
| 163 |
+
|
| 164 |
+
# Action for handling OAuth callback and displaying the user profile
|
| 165 |
+
def handle_callback_action(url):
|
| 166 |
+
user_profile = handle_oauth_callback(url)
|
| 167 |
+
return user_profile
|
| 168 |
+
|
| 169 |
+
# Bind login action to button
|
| 170 |
+
auth_btn.click(login_action)
|
| 171 |
+
|
| 172 |
+
# Bind OAuth callback handler to the callback input
|
| 173 |
+
callback_url.change(handle_callback_action, inputs=[callback_url], outputs=[profile_output])
|
| 174 |
|
| 175 |
+
# Submit action for chat
|
| 176 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
|
|
|
| 177 |
|
| 178 |
interface.launch()
|