tsissam commited on
Commit
b9f2622
Β·
verified Β·
1 Parent(s): eb65426

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import faiss
2
+ import pickle
3
+ import os
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
5
+ from langchain.llms import HuggingFacePipeline
6
+ from langchain.vectorstores import FAISS as LangChainFAISS
7
+ from langchain.docstore import InMemoryDocstore
8
+ from langchain.schema import Document
9
+ from langchain.chains import RetrievalQA
10
+ import gradio as gr
11
+
12
+ # Paths (relative to app root)
13
+ vector_path = "vector_store_faiss_chroma/faiss_index.index"
14
+ metadata_path = "vector_store_faiss_chroma/metadata.pkl"
15
+ model_path = "HuggingFaceModels/falcon-1b-instruct"
16
+
17
+ # Load the FAISS index
18
+ faiss_index = faiss.read_index(f"{vector_path}")
19
+
20
+ # Load metadata (text chunks)
21
+ with open(f"{metadata_path}", "rb") as f:
22
+ metadata = pickle.load(f)
23
+
24
+ # Rebuild LangChain Documents
25
+ docs = [Document(page_content=doc["page_content"]) for doc in metadata]
26
+
27
+ # Link documents to FAISS vectors
28
+ docstore = InMemoryDocstore({str(i): docs[i] for i in range(len(docs))})
29
+ id_map = {i: str(i) for i in range(len(docs))}
30
+
31
+ # Load the tokenizer and model
32
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
33
+ model = AutoModelForCausalLM.from_pretrained(model_path)
34
+
35
+ # Create a generation pipeline
36
+ text_generator_pipeline = pipeline(
37
+ model=model,
38
+ tokenizer=tokenizer,
39
+ task="text-generation",
40
+ return_full_text=False,
41
+ max_new_tokens=512,
42
+ temperature=0.2
43
+ )
44
+ # Wrap it as a LangChain LLM
45
+ llm = HuggingFacePipeline(pipeline=text_generator_pipeline)
46
+
47
+ # Create vectorstore and retriever
48
+ vectorstore_faiss = LangChainFAISS(
49
+ index=faiss_index,
50
+ docstore=docstore,
51
+ index_to_docstore_id=id_map,
52
+ embedding_function=None # Not needed for retrieval only
53
+ )
54
+
55
+ # Create a retriever that returns top-k most relevant chunks
56
+ retriever = vectorstore_faiss.as_retriever(search_kwargs={"k": 3})
57
+
58
+ # Create the RAG pipeline (Retriever + LLM)
59
+ qa_chain = RetrievalQA.from_chain_type(
60
+ llm=llm,
61
+ chain_type="stuff",
62
+ retriever=retriever,
63
+ return_source_documents=True
64
+ )
65
+
66
+ # πŸ” Chatbot function: takes a user question, returns generated answer
67
+ def ask_rag(query):
68
+ result = qa_chain({"query": query})
69
+ answer = result["result"]
70
+
71
+ # Optional: include sources (limited to 2)
72
+ sources = result.get("source_documents", [])
73
+ source_texts = "\n\n".join([f"πŸ”Ή Source {i+1}:\n{doc.page_content[:300]}..." for i, doc in enumerate(sources[:2])])
74
+
75
+ return f"πŸ“˜ Answer:\n{answer}\n\nπŸ“š Sources:\n{source_texts}"
76
+
77
+ # πŸŽ›οΈ Gradio UI components
78
+ gr.Interface(
79
+ fn=ask_rag,
80
+ inputs=gr.Textbox(lines=2, placeholder="Ask me about UCT admissions, housing, fees..."),
81
+ outputs="text",
82
+ title="πŸŽ“ University of Cape Town Course Advisor Chatbot",
83
+ description="Ask academic questions. Powered by FAISS + Falcon-E-1B + LangChain.",
84
+ allow_flagging="never"
85
+ ).launch()