Spaces:
Sleeping
Sleeping
yashkavaiya
commited on
Commit
•
bc572b9
1
Parent(s):
2af1f7f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv, find_dotenv
|
3 |
+
load_dotenv(find_dotenv())
|
4 |
+
from langchain_community.document_loaders import WebBaseLoader
|
5 |
+
import google.generativeai as genai
|
6 |
+
from langchain import hub
|
7 |
+
from langchain_core.output_parsers import StrOutputParser
|
8 |
+
from langchain_core.runnables import RunnablePassthrough
|
9 |
+
from langchain_google_genai import GoogleGenerativeAI
|
10 |
+
from langchain_community.vectorstores import FAISS
|
11 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
12 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
13 |
+
|
14 |
+
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
|
15 |
+
os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'
|
16 |
+
os.environ['LANGCHAIN_PROJECT'] = 'websiteRAG'
|
17 |
+
os.environ['LANGCHAIN_API_KEY'] = os.getenv("LANGCHAIN_API_KEY")
|
18 |
+
os.getenv('GOOGLE_API_KEY')
|
19 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
20 |
+
|
21 |
+
import gradio as gr
|
22 |
+
|
23 |
+
def respond(message, chat_history):
|
24 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
25 |
+
vectorstore = FAISS.load_local("./Vector_DB/faiss_index", embeddings, allow_dangerous_deserialization=True)
|
26 |
+
retriever = vectorstore.as_retriever()
|
27 |
+
prompt = hub.pull("rlm/rag-prompt")
|
28 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro")
|
29 |
+
|
30 |
+
def format_docs(docs):
|
31 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
32 |
+
|
33 |
+
rag_chain = (
|
34 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
35 |
+
| prompt
|
36 |
+
| llm
|
37 |
+
| StrOutputParser()
|
38 |
+
)
|
39 |
+
|
40 |
+
result = rag_chain.invoke(message)
|
41 |
+
return result
|
42 |
+
css = """
|
43 |
+
body {
|
44 |
+
font-family: 'Segoe UI', sans-serif;
|
45 |
+
background-color: #f0f0f0;
|
46 |
+
}
|
47 |
+
.container {
|
48 |
+
max-width: 1500px;
|
49 |
+
margin: 0 auto;
|
50 |
+
background-color: white;
|
51 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
52 |
+
border-radius: 8px;
|
53 |
+
display: flex;
|
54 |
+
flex-direction: column;
|
55 |
+
height: 100px;
|
56 |
+
}
|
57 |
+
h1 {
|
58 |
+
color: #0078d4;
|
59 |
+
text-align: center;
|
60 |
+
margin-bottom: 10px;
|
61 |
+
}
|
62 |
+
.description {
|
63 |
+
text-align: center;
|
64 |
+
margin-bottom: 20px;
|
65 |
+
}
|
66 |
+
.chatbot-container {
|
67 |
+
flex-grow: 1;
|
68 |
+
display: flex;
|
69 |
+
flex-direction: column;
|
70 |
+
}
|
71 |
+
.footer {
|
72 |
+
text-align: center;
|
73 |
+
font-size: 0.9em;
|
74 |
+
color: #666;
|
75 |
+
margin-top: 20px;
|
76 |
+
}
|
77 |
+
.footer a {
|
78 |
+
color: #0078d4;
|
79 |
+
text-decoration: none;
|
80 |
+
}
|
81 |
+
"""
|
82 |
+
|
83 |
+
with gr.Blocks(css=css) as demo:
|
84 |
+
gr.HTML("<div class='container'> <h1>Website RAG</h1> <p class='description'>This website takes a list of URLs and creates a Retrieval-Augmented Generation (RAG) system on top of it. The answers provided are more accurate than previous versions.</p>")
|
85 |
+
with gr.Column(elem_classes="chatbot-container"):
|
86 |
+
chatbot = gr.ChatInterface(
|
87 |
+
respond,
|
88 |
+
analytics_enabled=True,
|
89 |
+
show_progress="full",
|
90 |
+
chatbot=gr.Chatbot(height="70vh"),
|
91 |
+
textbox=gr.Textbox(placeholder="Enter your question here...", container=False),
|
92 |
+
|
93 |
+
)
|
94 |
+
|
95 |
+
gr.HTML("<div class='footer'>Created by Yash Kavaiya | <a href='https://github.com/Yash-Kavaiya'>GitHub</a> | <a href='https://linkedin.com/in/Yash Kavaiya'>LinkedIn</a></div>")
|
96 |
+
gr.HTML("</div>")
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch(share=True)
|