Spaces:
Paused
Paused
updated app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain.vectorstores import Chroma
|
5 |
-
from
|
6 |
from langchain.chains import RetrievalQA
|
7 |
from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausalLM
|
8 |
from langchain_community.document_loaders import DirectoryLoader
|
@@ -10,6 +10,7 @@ import torch
|
|
10 |
import re
|
11 |
import transformers
|
12 |
import spaces
|
|
|
13 |
|
14 |
# Initialize embeddings and ChromaDB
|
15 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
@@ -70,10 +71,11 @@ def test_rag(query):
|
|
70 |
|
71 |
return corrected_text_books
|
72 |
|
|
|
73 |
TENANT_ID = os.getenv("TENANT_ID")
|
74 |
CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
|
75 |
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
|
76 |
-
REDIRECT_URI = os.getenv("SPACE_HOST")
|
77 |
AUTH_URL = os.getenv("AUTH_URL")
|
78 |
TOKEN_URL = os.getenv("TOKEN_URL")
|
79 |
SCOPE = os.getenv("SCOPE")
|
@@ -105,23 +107,21 @@ def chat(query, history=None):
|
|
105 |
def clear_input():
|
106 |
return "", # Return empty string to clear input field
|
107 |
|
108 |
-
# Function to show/hide components based on login status
|
109 |
-
def on_login(success):
|
110 |
-
return gr.update(visible=success), gr.update(visible=success)
|
111 |
-
|
112 |
with gr.Blocks() as interface:
|
113 |
gr.Markdown("## RAG Chatbot")
|
114 |
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
115 |
|
116 |
# Sign-In Button
|
117 |
login_btn = gr.Button("Sign In with HF")
|
118 |
-
login_btn.click(lambda: oauth_login(), outputs=None) # Redirect user for OAuth login
|
119 |
|
|
|
|
|
|
|
120 |
# Hidden components initially
|
121 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
122 |
submit_btn = gr.Button("Submit", visible=False)
|
123 |
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
124 |
-
|
125 |
# Show components after login
|
126 |
def show_components():
|
127 |
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
@@ -130,4 +130,4 @@ with gr.Blocks() as interface:
|
|
130 |
submit_btn.click(show_components, outputs=[input_box, submit_btn, chat_history])
|
131 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
132 |
|
133 |
-
interface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain.vectorstores import Chroma
|
5 |
+
from langchain_huggingface import HuggingFacePipeline
|
6 |
from langchain.chains import RetrievalQA
|
7 |
from transformers import AutoConfig, AutoTokenizer, pipeline, AutoModelForCausalLM
|
8 |
from langchain_community.document_loaders import DirectoryLoader
|
|
|
10 |
import re
|
11 |
import transformers
|
12 |
import spaces
|
13 |
+
from urllib.parse import urlencode
|
14 |
|
15 |
# Initialize embeddings and ChromaDB
|
16 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
|
71 |
|
72 |
return corrected_text_books
|
73 |
|
74 |
+
# OAuth Configuration
|
75 |
TENANT_ID = os.getenv("TENANT_ID")
|
76 |
CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
|
77 |
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
|
78 |
+
REDIRECT_URI = os.getenv("SPACE_HOST") # Make sure this is the correct redirect URI
|
79 |
AUTH_URL = os.getenv("AUTH_URL")
|
80 |
TOKEN_URL = os.getenv("TOKEN_URL")
|
81 |
SCOPE = os.getenv("SCOPE")
|
|
|
107 |
def clear_input():
|
108 |
return "", # Return empty string to clear input field
|
109 |
|
|
|
|
|
|
|
|
|
110 |
with gr.Blocks() as interface:
|
111 |
gr.Markdown("## RAG Chatbot")
|
112 |
gr.Markdown("Ask a question and get answers based on retrieved documents.")
|
113 |
|
114 |
# Sign-In Button
|
115 |
login_btn = gr.Button("Sign In with HF")
|
|
|
116 |
|
117 |
+
# Redirect to OAuth login
|
118 |
+
login_btn.click(lambda: oauth_login(), outputs=None) # This will return the login URL
|
119 |
+
|
120 |
# Hidden components initially
|
121 |
input_box = gr.Textbox(label="Enter your question", placeholder="Type your question here...", visible=False)
|
122 |
submit_btn = gr.Button("Submit", visible=False)
|
123 |
chat_history = gr.Chatbot(label="Chat History", visible=False)
|
124 |
+
|
125 |
# Show components after login
|
126 |
def show_components():
|
127 |
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
|
|
130 |
submit_btn.click(show_components, outputs=[input_box, submit_btn, chat_history])
|
131 |
submit_btn.click(chat, inputs=[input_box, chat_history], outputs=[chat_history, input_box])
|
132 |
|
133 |
+
interface.launch()
|