Spaces:
Paused
Paused
auth
Browse files
app.py
CHANGED
@@ -1,18 +1,5 @@
|
|
1 |
import subprocess
|
2 |
-
import gradio as gr
|
3 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
-
from langchain.vectorstores import Chroma
|
5 |
-
from langchain.llms import HuggingFacePipeline
|
6 |
-
from langchain.chains import RetrievalQA
|
7 |
-
from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausalLM
|
8 |
-
from langchain_community.document_loaders import DirectoryLoader
|
9 |
-
import torch
|
10 |
-
import re
|
11 |
-
import requests
|
12 |
-
from urllib.parse import urlencode, urlparse, parse_qs
|
13 |
-
import spaces
|
14 |
|
15 |
-
# Step 1: Run the setup script
|
16 |
script_path = './setup.sh' # Adjust the path if needed
|
17 |
|
18 |
# Run the script
|
@@ -23,6 +10,20 @@ if exit_code == 0:
|
|
23 |
else:
|
24 |
print(f"Script failed with exit code {exit_code}.")
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Initialize embeddings and ChromaDB
|
27 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -37,8 +38,9 @@ books_db_client = books_db.as_retriever()
|
|
37 |
|
38 |
# Initialize the model and tokenizer
|
39 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
40 |
-
|
41 |
-
|
|
|
42 |
model_name,
|
43 |
trust_remote_code=True,
|
44 |
config=model_config,
|
@@ -47,7 +49,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
47 |
|
48 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
49 |
|
50 |
-
query_pipeline = pipeline(
|
51 |
"text-generation",
|
52 |
model=model,
|
53 |
tokenizer=tokenizer,
|
@@ -75,8 +77,8 @@ TENANT_ID = '2b093ced-2571-463f-bc3e-b4f8bcb427ee'
|
|
75 |
CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5'
|
76 |
CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW'
|
77 |
REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/'
|
78 |
-
AUTH_URL = f"https://login.microsoftonline.com/
|
79 |
-
TOKEN_URL = f"https://login.microsoftonline.com/
|
80 |
|
81 |
params = {
|
82 |
'client_id': CLIENT_ID,
|
@@ -84,17 +86,30 @@ params = {
|
|
84 |
'redirect_uri': REDIRECT_URI,
|
85 |
'response_mode': 'query',
|
86 |
'scope': 'User.Read',
|
87 |
-
'state': '12345'
|
88 |
-
'prompt': 'login' # This ensures the login prompt appears even if already logged in
|
89 |
}
|
90 |
|
91 |
# Construct the login URL
|
92 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
93 |
|
|
|
94 |
def show_login_button():
|
95 |
-
return f'<a href="{login_url}"
|
96 |
-
|
97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def exchange_code_for_token(auth_code):
|
99 |
data = {
|
100 |
'grant_type': 'authorization_code',
|
@@ -103,6 +118,7 @@ def exchange_code_for_token(auth_code):
|
|
103 |
'code': auth_code,
|
104 |
'redirect_uri': REDIRECT_URI
|
105 |
}
|
|
|
106 |
response = requests.post(TOKEN_URL, data=data)
|
107 |
|
108 |
if response.status_code == 200:
|
@@ -112,17 +128,14 @@ def exchange_code_for_token(auth_code):
|
|
112 |
else:
|
113 |
return None
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
query_params = parse_qs(parsed_url.query)
|
119 |
-
auth_code = query_params.get('code')
|
120 |
|
121 |
-
if
|
122 |
-
token
|
123 |
-
|
124 |
-
|
125 |
-
return "Login failed", False
|
126 |
|
127 |
# Function to retrieve answer using the RAG system
|
128 |
@spaces.GPU(duration=60)
|
@@ -152,43 +165,34 @@ def chat(query, history=None):
|
|
152 |
def clear_input():
|
153 |
return "", # Return empty string to clear input field
|
154 |
|
155 |
-
|
156 |
-
# Gradio Interface
|
157 |
with gr.Blocks() as interface:
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
# Components for chat (initially hidden)
|
187 |
-
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
188 |
-
submit_btn = gr.Button("Submit", visible=False)
|
189 |
-
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
190 |
-
|
191 |
-
# Chat submission
|
192 |
-
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
193 |
|
194 |
interface.launch()
|
|
|
1 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
3 |
script_path = './setup.sh' # Adjust the path if needed
|
4 |
|
5 |
# Run the script
|
|
|
10 |
else:
|
11 |
print(f"Script failed with exit code {exit_code}.")
|
12 |
|
13 |
+
import gradio as gr
|
14 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
15 |
+
from langchain.vectorstores import Chroma
|
16 |
+
from langchain.llms import HuggingFacePipeline
|
17 |
+
from langchain.chains import RetrievalQA
|
18 |
+
from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausalLM
|
19 |
+
from langchain_community.document_loaders import DirectoryLoader
|
20 |
+
import torch
|
21 |
+
import re
|
22 |
+
import transformers
|
23 |
+
import spaces
|
24 |
+
import requests
|
25 |
+
from urllib.parse import urlencode
|
26 |
+
|
27 |
# Initialize embeddings and ChromaDB
|
28 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
38 |
|
39 |
# Initialize the model and tokenizer
|
40 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
41 |
+
|
42 |
+
model_config = transformers.AutoConfig.from_pretrained(model_name, max_new_tokens=1024)
|
43 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(
|
44 |
model_name,
|
45 |
trust_remote_code=True,
|
46 |
config=model_config,
|
|
|
49 |
|
50 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
51 |
|
52 |
+
query_pipeline = transformers.pipeline(
|
53 |
"text-generation",
|
54 |
model=model,
|
55 |
tokenizer=tokenizer,
|
|
|
77 |
CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5'
|
78 |
CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW'
|
79 |
REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/'
|
80 |
+
AUTH_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/authorize"
|
81 |
+
TOKEN_URL = f"https://login.microsoftonline.com/2b093ced-2571-463f-bc3e-b4f8bcb427ee/oauth2/v2.0/token"
|
82 |
|
83 |
params = {
|
84 |
'client_id': CLIENT_ID,
|
|
|
86 |
'redirect_uri': REDIRECT_URI,
|
87 |
'response_mode': 'query',
|
88 |
'scope': 'User.Read',
|
89 |
+
'state': '12345' # Optional state parameter
|
|
|
90 |
}
|
91 |
|
92 |
# Construct the login URL
|
93 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
94 |
|
95 |
+
# Gradio interface
|
96 |
def show_login_button():
|
97 |
+
return f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>'
|
98 |
+
|
99 |
+
# Dummy function to simulate token validation (you will replace this with actual validation)
|
100 |
+
def is_logged_in(token):
|
101 |
+
# Check if the token exists (or check if it's valid)
|
102 |
+
return token is not None
|
103 |
+
|
104 |
+
# Gradio interface
|
105 |
+
def check_login(status):
|
106 |
+
# If logged in, show the chatbot interface, otherwise show login link
|
107 |
+
if status:
|
108 |
+
return gr.update(visible=True), gr.update(visible=True)
|
109 |
+
else:
|
110 |
+
return gr.update(visible=False), gr.update(visible=False)
|
111 |
+
|
112 |
+
# Function to exchange authorization code for access token
|
113 |
def exchange_code_for_token(auth_code):
|
114 |
data = {
|
115 |
'grant_type': 'authorization_code',
|
|
|
118 |
'code': auth_code,
|
119 |
'redirect_uri': REDIRECT_URI
|
120 |
}
|
121 |
+
|
122 |
response = requests.post(TOKEN_URL, data=data)
|
123 |
|
124 |
if response.status_code == 200:
|
|
|
128 |
else:
|
129 |
return None
|
130 |
|
131 |
+
def login_user(auth_code):
|
132 |
+
# Exchange the authorization code for an access token
|
133 |
+
token = exchange_code_for_token(auth_code)
|
|
|
|
|
134 |
|
135 |
+
if token:
|
136 |
+
return token
|
137 |
+
else:
|
138 |
+
return None
|
|
|
139 |
|
140 |
# Function to retrieve answer using the RAG system
|
141 |
@spaces.GPU(duration=60)
|
|
|
165 |
def clear_input():
|
166 |
return "", # Return empty string to clear input field
|
167 |
|
|
|
|
|
168 |
with gr.Blocks() as interface:
|
169 |
+
gr.Markdown("## RAG Chatbot")
|
170 |
+
gr.Markdown("Please log in to continue.")
|
171 |
+
|
172 |
+
# Custom HTML to show login link
|
173 |
+
login_link = gr.HTML(f'<a href="{login_url}" target="_blank">Click here to login with Microsoft</a>')
|
174 |
+
|
175 |
+
# Login button to simulate the login process
|
176 |
+
login_button = gr.Button("Login")
|
177 |
+
|
178 |
+
# Components for chat (initially hidden)
|
179 |
+
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
180 |
+
submit_btn = gr.Button("Submit", visible=False)
|
181 |
+
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
182 |
+
|
183 |
+
# Handle login button click
|
184 |
+
login_button.click(
|
185 |
+
login_user,
|
186 |
+
inputs=[],
|
187 |
+
outputs=[login_button], # You can also update the UI to show login status
|
188 |
+
queue=False
|
189 |
+
).then(
|
190 |
+
lambda token: check_login(is_logged_in(token)),
|
191 |
+
inputs=[],
|
192 |
+
outputs=[input_box, submit_btn]
|
193 |
+
)
|
194 |
+
|
195 |
+
# Input submission and chat handling
|
196 |
+
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
interface.launch()
|