Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
+
from langchain_core.runnables import RunnablePassthrough, RunnableLambda, RunnableParallel
|
| 9 |
+
|
| 10 |
+
# 1. Unzip stored FAISS index
|
| 11 |
+
if not os.path.exists("faiss_store"):
|
| 12 |
+
with zipfile.ZipFile("faiss_store.zip", "r") as zip_ref:
|
| 13 |
+
zip_ref.extractall(".")
|
| 14 |
+
|
| 15 |
+
# 2. Reload FAISS vector store
|
| 16 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 17 |
+
vector_store = FAISS.load_local("faiss_store", embeddings, allow_dangerous_deserialization=True)
|
| 18 |
+
|
| 19 |
+
# 3. Setup retriever + RAG chain
|
| 20 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
| 21 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 22 |
+
client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.2", token=hf_token)
|
| 23 |
+
|
| 24 |
+
prompt_template = """Use the following context about my work experience to answer the question. If you don't know, say so.
|
| 25 |
+
Context: {context}
|
| 26 |
+
Question: {question}
|
| 27 |
+
Answer:"""
|
| 28 |
+
|
| 29 |
+
def generate_with_client(query, context):
|
| 30 |
+
messages = [
|
| 31 |
+
{"role": "system", "content": "You are a helpful assistant summarizing work experience."},
|
| 32 |
+
{"role": "user", "content": prompt_template.format(context=context, question=query)}
|
| 33 |
+
]
|
| 34 |
+
response = client.chat_completion(messages, max_tokens=200)
|
| 35 |
+
return response.choices[0].message.content
|
| 36 |
+
|
| 37 |
+
rag_chain = (
|
| 38 |
+
RunnableParallel({
|
| 39 |
+
"context": retriever | RunnableLambda(lambda docs: "\n\n".join([doc.page_content for doc in docs])),
|
| 40 |
+
"question": RunnablePassthrough()
|
| 41 |
+
})
|
| 42 |
+
| RunnableLambda(lambda x: generate_with_client(x["question"], x["context"]))
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# 4. Gradio UI
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# 🚀 RAG Chatbot (Prebuilt Vector Store)")
|
| 48 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
| 49 |
+
msg = gr.Textbox(placeholder="Ask about your work experience...")
|
| 50 |
+
clear = gr.Button("Clear")
|
| 51 |
+
|
| 52 |
+
def user(user_message, history):
|
| 53 |
+
history.append({"role": "user", "content": user_message})
|
| 54 |
+
return "", history
|
| 55 |
+
|
| 56 |
+
def respond(history):
|
| 57 |
+
user_message_content = history[-1]["content"]
|
| 58 |
+
bot_response_content = rag_chain.invoke(user_message_content)
|
| 59 |
+
history.append({"role": "assistant", "content": bot_response_content})
|
| 60 |
+
return history
|
| 61 |
+
|
| 62 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 63 |
+
respond, [chatbot], chatbot
|
| 64 |
+
)
|
| 65 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
| 66 |
+
|
| 67 |
+
demo.launch()
|