Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
|
@@ -7,10 +7,10 @@ from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausal
|
|
| 7 |
from langchain_community.document_loaders import DirectoryLoader
|
| 8 |
import torch
|
| 9 |
import re
|
| 10 |
-
import requests
|
| 11 |
-
from urllib.parse import urlencode
|
| 12 |
import transformers
|
| 13 |
import spaces
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Initialize embeddings and ChromaDB
|
| 16 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
@@ -26,6 +26,7 @@ books_db_client = books_db.as_retriever()
|
|
| 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,
|
|
@@ -67,22 +68,19 @@ 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 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
'client_id': CLIENT_ID,
|
| 77 |
-
'response_type': 'code',
|
| 78 |
-
'redirect_uri': REDIRECT_URI,
|
| 79 |
-
'response_mode': 'query',
|
| 80 |
-
'scope': 'User.Read',
|
| 81 |
-
'state': '12345' # Optional state parameter
|
| 82 |
-
}
|
| 83 |
-
return f"{AUTH_URL}?{urlencode(params)}"
|
| 84 |
|
| 85 |
-
# Exchange authorization code for an access token
|
| 86 |
def exchange_code_for_token(auth_code):
|
| 87 |
data = {
|
| 88 |
'grant_type': 'authorization_code',
|
|
@@ -91,22 +89,12 @@ def exchange_code_for_token(auth_code):
|
|
| 91 |
'code': auth_code,
|
| 92 |
'redirect_uri': REDIRECT_URI
|
| 93 |
}
|
|
|
|
| 94 |
response = requests.post(TOKEN_URL, data=data)
|
| 95 |
token_data = response.json()
|
|
|
|
| 96 |
return token_data.get('access_token')
|
| 97 |
|
| 98 |
-
# Function to fetch user profile from Microsoft Graph
|
| 99 |
-
def get_user_profile(token):
|
| 100 |
-
headers = {
|
| 101 |
-
'Authorization': f'Bearer {token}'
|
| 102 |
-
}
|
| 103 |
-
response = requests.get(GRAPH_API_URL, headers=headers)
|
| 104 |
-
return response.json()
|
| 105 |
-
|
| 106 |
-
# Function to check if the user is authenticated
|
| 107 |
-
def is_authenticated():
|
| 108 |
-
return access_token is not None
|
| 109 |
-
|
| 110 |
# Function to retrieve answer using the RAG system
|
| 111 |
@spaces.GPU(duration=60)
|
| 112 |
def test_rag(query):
|
|
@@ -121,60 +109,31 @@ def test_rag(query):
|
|
| 121 |
corrected_text_books = "No helpful answer found."
|
| 122 |
|
| 123 |
return corrected_text_books
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# If the user is not authenticated, redirect to Microsoft login
|
| 130 |
-
if not is_authenticated():
|
| 131 |
-
auth_url = get_auth_url()
|
| 132 |
-
return gr.Markdown(f"Please [log in]({auth_url}) to use the chatbot.")
|
| 133 |
-
|
| 134 |
-
# Gradio chatbot interface
|
| 135 |
-
def chat(query, history=None):
|
| 136 |
-
if history is None:
|
| 137 |
-
history = []
|
| 138 |
-
if query:
|
| 139 |
-
# Chatbot logic here
|
| 140 |
-
answer = test_rag(query)
|
| 141 |
-
history.append((query, answer))
|
| 142 |
-
return history, "" # Clear input after submission
|
| 143 |
-
|
| 144 |
-
with gr.Blocks() as interface:
|
| 145 |
-
gr.Markdown("## RAG Chatbot")
|
| 146 |
-
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
| 147 |
-
|
| 148 |
-
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...")
|
| 149 |
-
submit_btn = gr.Button("Submit")
|
| 150 |
-
chat_history = gr.Chatbot(label="Chat History")
|
| 151 |
-
|
| 152 |
-
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
# Function to handle OAuth callback
|
| 157 |
-
def handle_auth_callback(auth_code):
|
| 158 |
-
global access_token
|
| 159 |
-
|
| 160 |
-
# Exchange authorization code for access token
|
| 161 |
-
access_token = exchange_code_for_token(auth_code)
|
| 162 |
-
return "Authentication successful. You can now use the chatbot."
|
| 163 |
-
|
| 164 |
-
# Gradio app launch
|
| 165 |
-
with gr.Blocks() as app:
|
| 166 |
-
gr.Markdown("## OAuth2.0 Chatbot")
|
| 167 |
-
|
| 168 |
-
# Add an input field to manually input the authorization code for testing
|
| 169 |
-
auth_code_input = gr.Textbox(label="Enter the OAuth Authorization Code")
|
| 170 |
-
|
| 171 |
-
# Button to handle authentication and exchange the code for the access token
|
| 172 |
-
auth_button = gr.Button("Authenticate")
|
| 173 |
-
|
| 174 |
-
# Callback for authentication
|
| 175 |
-
auth_button.click(fn=handle_auth_callback, inputs=auth_code_input, outputs="text")
|
| 176 |
-
|
| 177 |
-
# Display the chat interface or authentication prompt
|
| 178 |
-
chat_interface()
|
| 179 |
|
| 180 |
-
|
|
|
|
| 7 |
from langchain_community.document_loaders import DirectoryLoader
|
| 8 |
import torch
|
| 9 |
import re
|
|
|
|
|
|
|
| 10 |
import transformers
|
| 11 |
import spaces
|
| 12 |
+
import requests
|
| 13 |
+
from urllib.parse import urlencode
|
| 14 |
|
| 15 |
# Initialize embeddings and ChromaDB
|
| 16 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
|
|
| 26 |
|
| 27 |
# Initialize the model and tokenizer
|
| 28 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
| 29 |
+
|
| 30 |
model_config = transformers.AutoConfig.from_pretrained(model_name, max_new_tokens=1024)
|
| 31 |
model = transformers.AutoModelForCausalLM.from_pretrained(
|
| 32 |
model_name,
|
|
|
|
| 68 |
AUTH_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/authorize"
|
| 69 |
TOKEN_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/token"
|
| 70 |
|
| 71 |
+
params = {
|
| 72 |
+
'client_id': CLIENT_ID,
|
| 73 |
+
'response_type': 'code',
|
| 74 |
+
'redirect_uri': REDIRECT_URI,
|
| 75 |
+
'response_mode': 'query',
|
| 76 |
+
'scope': 'User.Read',
|
| 77 |
+
'state': '12345' # Optional state parameter
|
| 78 |
+
}
|
| 79 |
|
| 80 |
+
# Redirect the user to Microsoft's OAuth endpoint
|
| 81 |
+
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 82 |
+
print("Redirect to:", login_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
def exchange_code_for_token(auth_code):
|
| 85 |
data = {
|
| 86 |
'grant_type': 'authorization_code',
|
|
|
|
| 89 |
'code': auth_code,
|
| 90 |
'redirect_uri': REDIRECT_URI
|
| 91 |
}
|
| 92 |
+
|
| 93 |
response = requests.post(TOKEN_URL, data=data)
|
| 94 |
token_data = response.json()
|
| 95 |
+
|
| 96 |
return token_data.get('access_token')
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# Function to retrieve answer using the RAG system
|
| 99 |
@spaces.GPU(duration=60)
|
| 100 |
def test_rag(query):
|
|
|
|
| 109 |
corrected_text_books = "No helpful answer found."
|
| 110 |
|
| 111 |
return corrected_text_books
|
| 112 |
+
|
| 113 |
+
# Define the Gradio interface
|
| 114 |
+
def chat(query, history=None):
|
| 115 |
+
if history is None:
|
| 116 |
+
history = []
|
| 117 |
+
if query:
|
| 118 |
+
answer = test_rag(query)
|
| 119 |
+
history.append((query, answer))
|
| 120 |
+
return history, "" # Clear input after submission
|
| 121 |
+
|
| 122 |
+
# Function to clear input text
|
| 123 |
+
def clear_input():
|
| 124 |
+
return "", # Return empty string to clear input field
|
| 125 |
+
|
| 126 |
+
# Gradio interface
|
| 127 |
+
with gr.Blocks() as interface:
|
| 128 |
+
gr.Markdown("## RAG Chatbot")
|
| 129 |
+
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
| 130 |
|
| 131 |
+
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...")
|
| 132 |
+
submit_btn = gr.Button("Submit")
|
| 133 |
+
# clear_btn = gr.Button("Clear")
|
| 134 |
+
chat_history = gr.Chatbot(label="Chat History")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 137 |
+
# clear_btn.click(clear_input, outputs=input_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
interface.launch()
|