Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,70 @@ import gradio as gr
|
|
| 2 |
from langchain_community.document_loaders import TextLoader
|
| 3 |
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# 1. Load your tuition data
|
| 10 |
loader = TextLoader("knowledge.txt")
|
| 11 |
documents = loader.load()
|
| 12 |
|
| 13 |
-
# 2.
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 16 |
-
vectorstore = FAISS.from_documents(
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# 3. Connect to the Brain (LLM)
|
| 19 |
-
# We use an API so your computer/space doesn't crash.
|
| 20 |
llm = HuggingFaceEndpoint(
|
| 21 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 22 |
-
huggingfacehub_api_token=
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
qa_chain = RetrievalQA.from_chain_type(
|
| 27 |
llm=llm,
|
| 28 |
chain_type="stuff",
|
| 29 |
-
retriever=vectorstore.as_retriever(),
|
| 30 |
-
chain_type_kwargs={
|
| 31 |
-
"prompt": gr.Prompt("Use ONLY the following context to answer. If not there, say you don't know. \nContext: {context} \nQuestion: {question}")
|
| 32 |
-
}
|
| 33 |
)
|
| 34 |
|
| 35 |
def chat(message, history):
|
| 36 |
-
response = qa_chain.invoke(message)
|
| 37 |
return response["result"]
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
demo.launch()
|
|
|
|
| 2 |
from langchain_community.document_loaders import TextLoader
|
| 3 |
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# 1. Load the Examfriendly knowledge base
|
|
|
|
| 11 |
loader = TextLoader("knowledge.txt")
|
| 12 |
documents = loader.load()
|
| 13 |
|
| 14 |
+
# 2. CHUNKING (The ML Step)
|
| 15 |
+
# We split by double newlines first to keep sections together.
|
| 16 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 17 |
+
chunk_size=600,
|
| 18 |
+
chunk_overlap=50,
|
| 19 |
+
separators=["\n\n", "\n", " ", ""]
|
| 20 |
+
)
|
| 21 |
+
docs = text_splitter.split_documents(documents)
|
| 22 |
+
|
| 23 |
+
# 3. Setup Memory (Vector DB)
|
| 24 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 26 |
+
|
| 27 |
+
# 4. Connect to the Brain (Llama 3.2)
|
| 28 |
+
sec_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 29 |
|
|
|
|
|
|
|
| 30 |
llm = HuggingFaceEndpoint(
|
| 31 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 32 |
+
huggingfacehub_api_token=sec_token,
|
| 33 |
+
temperature=0.1
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# 5. Strict Examfriendly Prompt
|
| 37 |
+
template = """You are the Examfriendly AI Assistant. Use ONLY the provided context to answer.
|
| 38 |
+
|
| 39 |
+
Rules:
|
| 40 |
+
1. If asked about Class 11/12 Physics or Chemistry, say: "Examfriendly specializes specifically in Computer Science and IT for those grades."
|
| 41 |
+
2. Always clarify that tuition fees are a monthly subscription.
|
| 42 |
+
3. If a teacher asks, mention the free Teaching Aid PPTs.
|
| 43 |
+
4. If you don't know the answer, say: "Please email inquiry@examfriendly.in for more details."
|
| 44 |
+
|
| 45 |
+
Context: {context}
|
| 46 |
+
Question: {question}
|
| 47 |
+
|
| 48 |
+
Answer:"""
|
| 49 |
+
|
| 50 |
+
QA_CHAIN_PROMPT = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 51 |
+
|
| 52 |
+
# 6. Build the Chain
|
| 53 |
qa_chain = RetrievalQA.from_chain_type(
|
| 54 |
llm=llm,
|
| 55 |
chain_type="stuff",
|
| 56 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}), # Get top 3 most relevant chunks
|
| 57 |
+
chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
def chat(message, history):
|
| 61 |
+
response = qa_chain.invoke({"query": message})
|
| 62 |
return response["result"]
|
| 63 |
|
| 64 |
+
# 7. Professional UI
|
| 65 |
+
demo = gr.ChatInterface(
|
| 66 |
+
fn=chat,
|
| 67 |
+
title="Examfriendly Support Bot",
|
| 68 |
+
description="Ask about our CBSE tuitions, coding courses, or free resources."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
demo.launch()
|