Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 8 |
+
from langchain_community.llms import GPT4All
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# Load embeddings and FAISS vector store
|
| 13 |
+
def load_vectorstore():
|
| 14 |
+
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
| 15 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 16 |
+
db = FAISS.load_local("vectorstore", embeddings, allow_dangerous_deserialization=True)
|
| 17 |
+
return db
|
| 18 |
+
|
| 19 |
+
db = load_vectorstore()
|
| 20 |
+
|
| 21 |
+
# Initialize GPT4All model
|
| 22 |
+
local_path = "./models/ggml-gpt4all-j.bin" # Or any supported GPT4All model
|
| 23 |
+
|
| 24 |
+
callbacks = [StreamingStdOutCallbackHandler()]
|
| 25 |
+
llm = GPT4All(
|
| 26 |
+
model=local_path,
|
| 27 |
+
callbacks=callbacks,
|
| 28 |
+
verbose=True,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Create Retrieval QA Chain
|
| 32 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 33 |
+
llm=llm,
|
| 34 |
+
chain_type="stuff",
|
| 35 |
+
retriever=db.as_retriever(k=2),
|
| 36 |
+
return_source_documents=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Define chat function
|
| 40 |
+
def chat(message, chat_history):
|
| 41 |
+
result = qa_chain({"query": message})
|
| 42 |
+
response = result["result"]
|
| 43 |
+
sources = result.get("source_documents", [])
|
| 44 |
+
|
| 45 |
+
if sources:
|
| 46 |
+
source_info = "\n\nSources:\n" + "\n".join([f"- {doc.metadata}" for doc in sources])
|
| 47 |
+
response += source_info
|
| 48 |
+
|
| 49 |
+
return response
|
| 50 |
+
|
| 51 |
+
# Gradio Chat Interface
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## 🤖 My Offline RAG Chatbot (No API Key Needed)")
|
| 54 |
+
chatbot = gr.Chatbot()
|
| 55 |
+
msg = gr.Textbox(label="💬 Your Message")
|
| 56 |
+
clear = gr.Button("🗑️ Clear Chat")
|
| 57 |
+
|
| 58 |
+
state = gr.State([])
|
| 59 |
+
|
| 60 |
+
def respond(message, chat_history):
|
| 61 |
+
bot_response = chat(message, chat_history)
|
| 62 |
+
chat_history.append((message, bot_response))
|
| 63 |
+
return "", chat_history
|
| 64 |
+
|
| 65 |
+
msg.submit(respond, [msg, state], [msg, chatbot])
|
| 66 |
+
clear.click(lambda: ([], None), None, [chatbot, state])
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|