sanjeevbora commited on
Commit
a75d764
·
verified ·
1 Parent(s): 22b0171
Files changed (1) hide show
  1. app.py +17 -104
app.py CHANGED
@@ -1,15 +1,3 @@
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
@@ -21,8 +9,6 @@ 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"
@@ -39,11 +25,19 @@ books_db_client = books_db.as_retriever()
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,
 
47
  device_map=device,
48
  )
49
 
@@ -72,71 +66,6 @@ books_db_client_retriever = RetrievalQA.from_chain_type(
72
  verbose=True
73
  )
74
 
75
- # OAuth Configuration
76
- 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/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,
85
- 'response_type': 'code',
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',
116
- 'client_id': CLIENT_ID,
117
- 'client_secret': CLIENT_SECRET,
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:
125
- token_data = response.json()
126
- access_token = token_data.get('access_token')
127
- return access_token
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)
142
  def test_rag(query):
@@ -165,34 +94,18 @@ def chat(query, history=None):
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from langchain.embeddings import HuggingFaceEmbeddings
3
  from langchain.vectorstores import Chroma
 
9
  import re
10
  import transformers
11
  import spaces
 
 
12
 
13
  # Initialize embeddings and ChromaDB
14
  model_name = "sentence-transformers/all-mpnet-base-v2"
 
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
  verbose=True
67
  )
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  # Function to retrieve answer using the RAG system
70
  @spaces.GPU(duration=60)
71
  def test_rag(query):
 
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")
100
+ gr.Markdown("Ask a question and get answers based on retrieved documents.")
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()
111