Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.document_loaders import WikipediaLoader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain.llms import Ollama # You can change to ChatOpenAI or DeepSeek
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
|
9 |
+
# Title
|
10 |
+
st.set_page_config(page_title="Cultural Bias Explorer")
|
11 |
+
st.title("π Cultural Bias Explorer in LLMs (RAG + LangChain)")
|
12 |
+
st.markdown("Explore how answers vary across cultures using region-specific knowledge bases.")
|
13 |
+
|
14 |
+
# Inputs
|
15 |
+
region = st.selectbox("Choose Cultural Region:", ["India", "United States"])
|
16 |
+
prompt = st.text_input("Enter your question here:")
|
17 |
+
|
18 |
+
# Load LLM
|
19 |
+
llm = Ollama(model="llama3") # You can change to "deepseek-chat" if available
|
20 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
21 |
+
|
22 |
+
# Function to build retriever
|
23 |
+
@st.cache_resource
|
24 |
+
def build_retriever(region_topic):
|
25 |
+
loader = WikipediaLoader(query=region_topic, load_max_docs=3)
|
26 |
+
documents = loader.load()
|
27 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
28 |
+
docs = text_splitter.split_documents(documents)
|
29 |
+
db = FAISS.from_documents(docs, embeddings)
|
30 |
+
return db.as_retriever()
|
31 |
+
|
32 |
+
# Run RAG if prompt submitted
|
33 |
+
if st.button("Generate Answer"):
|
34 |
+
if prompt.strip() == "":
|
35 |
+
st.warning("Please enter a prompt.")
|
36 |
+
else:
|
37 |
+
retriever = build_retriever(region)
|
38 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
39 |
+
with st.spinner("Thinking..."):
|
40 |
+
answer = qa_chain.run(prompt)
|
41 |
+
st.markdown("#### β¨ Region-Specific Answer:")
|
42 |
+
st.success(answer)
|