Spaces:
Paused
Paused
auth update
Browse files
app.py
CHANGED
|
@@ -1,15 +1,4 @@
|
|
| 1 |
import subprocess
|
| 2 |
-
|
| 3 |
-
script_path = './setup.sh' # Adjust the path if needed
|
| 4 |
-
|
| 5 |
-
# Run the script
|
| 6 |
-
exit_code = subprocess.call(['bash', script_path])
|
| 7 |
-
|
| 8 |
-
if exit_code == 0:
|
| 9 |
-
print("Script executed successfully.")
|
| 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
|
|
@@ -19,11 +8,20 @@ from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausal
|
|
| 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, urlparse, parse_qs
|
| 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,9 +36,8 @@ books_db_client = books_db.as_retriever()
|
|
| 38 |
|
| 39 |
# Initialize the model and tokenizer
|
| 40 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
model = transformers.AutoModelForCausalLM.from_pretrained(
|
| 44 |
model_name,
|
| 45 |
trust_remote_code=True,
|
| 46 |
config=model_config,
|
|
@@ -49,7 +46,7 @@ model = transformers.AutoModelForCausalLM.from_pretrained(
|
|
| 49 |
|
| 50 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 51 |
|
| 52 |
-
query_pipeline =
|
| 53 |
"text-generation",
|
| 54 |
model=model,
|
| 55 |
tokenizer=tokenizer,
|
|
@@ -77,8 +74,8 @@ TENANT_ID = '2b093ced-2571-463f-bc3e-b4f8bcb427ee'
|
|
| 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/
|
| 81 |
-
TOKEN_URL = f"https://login.microsoftonline.com/
|
| 82 |
|
| 83 |
params = {
|
| 84 |
'client_id': CLIENT_ID,
|
|
@@ -93,7 +90,7 @@ params = {
|
|
| 93 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 94 |
|
| 95 |
def show_login_button():
|
| 96 |
-
return f'<a href="{login_url}" class
|
| 97 |
|
| 98 |
def exchange_code_for_token(auth_code):
|
| 99 |
data = {
|
|
@@ -120,11 +117,10 @@ def handle_redirect(url):
|
|
| 120 |
|
| 121 |
if auth_code:
|
| 122 |
token = exchange_code_for_token(auth_code[0])
|
| 123 |
-
return token #
|
| 124 |
return None
|
| 125 |
|
| 126 |
-
# Function to retrieve
|
| 127 |
-
@spaces.GPU(duration=60)
|
| 128 |
def test_rag(query):
|
| 129 |
books_retriever = books_db_client_retriever.run(query)
|
| 130 |
|
|
@@ -146,26 +142,38 @@ def chat(query, history=None):
|
|
| 146 |
history.append((query, answer))
|
| 147 |
return history, "" # Clear input after submission
|
| 148 |
|
|
|
|
| 149 |
with gr.Blocks() as interface:
|
| 150 |
-
gr.
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 4 |
from langchain.vectorstores import Chroma
|
|
|
|
| 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 |
|
| 14 |
+
# Step 1: Run the setup script
|
| 15 |
+
script_path = './setup.sh' # Adjust the path if needed
|
| 16 |
+
|
| 17 |
+
# Run the script
|
| 18 |
+
exit_code = subprocess.call(['bash', script_path])
|
| 19 |
+
|
| 20 |
+
if exit_code == 0:
|
| 21 |
+
print("Script executed successfully.")
|
| 22 |
+
else:
|
| 23 |
+
print(f"Script failed with exit code {exit_code}.")
|
| 24 |
+
|
| 25 |
# Initialize embeddings and ChromaDB
|
| 26 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
| 27 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 36 |
|
| 37 |
# Initialize the model and tokenizer
|
| 38 |
model_name = "stabilityai/stablelm-zephyr-3b"
|
| 39 |
+
model_config = AutoConfig.from_pretrained(model_name, max_new_tokens=1024)
|
| 40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 41 |
model_name,
|
| 42 |
trust_remote_code=True,
|
| 43 |
config=model_config,
|
|
|
|
| 46 |
|
| 47 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 48 |
|
| 49 |
+
query_pipeline = pipeline(
|
| 50 |
"text-generation",
|
| 51 |
model=model,
|
| 52 |
tokenizer=tokenizer,
|
|
|
|
| 74 |
CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5'
|
| 75 |
CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW'
|
| 76 |
REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/'
|
| 77 |
+
AUTH_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize"
|
| 78 |
+
TOKEN_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"
|
| 79 |
|
| 80 |
params = {
|
| 81 |
'client_id': CLIENT_ID,
|
|
|
|
| 90 |
login_url = f"{AUTH_URL}?{urlencode(params)}"
|
| 91 |
|
| 92 |
def show_login_button():
|
| 93 |
+
return f'<a href="{login_url}" class="GFG"> Click here to login with Microsoft </a>'
|
| 94 |
|
| 95 |
def exchange_code_for_token(auth_code):
|
| 96 |
data = {
|
|
|
|
| 117 |
|
| 118 |
if auth_code:
|
| 119 |
token = exchange_code_for_token(auth_code[0])
|
| 120 |
+
return token # Return the token or handle accordingly
|
| 121 |
return None
|
| 122 |
|
| 123 |
+
# Function to retrieve answers using the RAG system
|
|
|
|
| 124 |
def test_rag(query):
|
| 125 |
books_retriever = books_db_client_retriever.run(query)
|
| 126 |
|
|
|
|
| 142 |
history.append((query, answer))
|
| 143 |
return history, "" # Clear input after submission
|
| 144 |
|
| 145 |
+
# Gradio interface
|
| 146 |
with gr.Blocks() as interface:
|
| 147 |
+
with gr.Tab("Login"):
|
| 148 |
+
gr.Markdown("## Login Page")
|
| 149 |
+
login_link = gr.HTML(show_login_button())
|
| 150 |
+
|
| 151 |
+
# Hidden textbox for redirect URL
|
| 152 |
+
redirect_url_input = gr.Textbox(label="Redirect URL", visible=False)
|
| 153 |
+
|
| 154 |
+
# Handle redirect
|
| 155 |
+
redirect_url_input.change(
|
| 156 |
+
handle_redirect,
|
| 157 |
+
inputs=[redirect_url_input],
|
| 158 |
+
outputs=[redirect_url_input],
|
| 159 |
+
show_progress=True
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
with gr.Tab("Chatbot"):
|
| 163 |
+
gr.Markdown("## Chatbot Page")
|
| 164 |
+
|
| 165 |
+
# Components for chat (initially hidden)
|
| 166 |
+
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
| 167 |
+
submit_btn = gr.Button("Submit", visible=False)
|
| 168 |
+
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
| 169 |
+
|
| 170 |
+
redirect_url_input.change(
|
| 171 |
+
handle_redirect,
|
| 172 |
+
inputs=[redirect_url_input],
|
| 173 |
+
outputs=[input_box, submit_btn, chat_history], # Update visibility based on login
|
| 174 |
+
show_progress=True
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
| 178 |
+
|
| 179 |
+
interface.launch()
|