0% found this document useful (0 votes)
11 views5 pages

Kenya Constitution Q&A Streamlit App

The document outlines a Streamlit application that utilizes the Mistral-7B Instruct model to answer questions about the Constitution of Kenya. It loads the constitution from a PDF, splits the text for processing, and uses Pinecone for vector storage and retrieval of relevant information. Additionally, it integrates Google Translate to provide answers in both English and Kiswahili, with a user-friendly interface for querying the constitution.

Uploaded by

Dorcas Bwire
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Kenya Constitution Q&A Streamlit App

The document outlines a Streamlit application that utilizes the Mistral-7B Instruct model to answer questions about the Constitution of Kenya. It loads the constitution from a PDF, splits the text for processing, and uses Pinecone for vector storage and retrieval of relevant information. Additionally, it integrates Google Translate to provide answers in both English and Kiswahili, with a user-friendly interface for querying the constitution.

Uploaded by

Dorcas Bwire
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import streamlit as st

from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

from [Link] import HuggingFacePipeline

from langchain.document_loaders import PyPDFLoader

from langchain.text_splitter import RecursiveCharacterTextSplitter

from [Link] import Pinecone

from [Link] import RetrievalQA

from [Link] import PromptTemplate

from googletrans import Translator

# Initialize Mistral-7B Instruct Model

model_name = "mistralai/Mistral-7B-Instruct-v0.1"

tokenizer = AutoTokenizer.from_pretrained(model_name)

model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")

text_gen_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer,


max_new_tokens=512)

llm = HuggingFacePipeline(pipeline=text_gen_pipeline)

# Load Constitution of Kenya

loader = PyPDFLoader("constitution_of_kenya.pdf")

documents = [Link]()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)

docs = text_splitter.split_documents(documents)

# Prepare text for Pinecone vector store

texts = [doc.page_content for doc in docs]


# Initialize Pinecone

import pinecone

[Link](api_key="PINECONE_API_KEY", environment="PINECONE_ENV")

index_name = "kenya_constitution"

# Create Pinecone vector store

vector_store = Pinecone.from_texts(texts, embedding=llm, index_name=index_name)

# Set up retriever

retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})

# Initialize RetrievalQA chain

rag_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever,


return_source_documents=True)

# Initialize Google Translator

translator = Translator()

# Streamlit App Layout

[Link]("Kenya Constitution Q&A")

# User input (query)

query = st.text_input("Ask a question about the Constitution of Kenya:")

if query:
# Query the RAG chain

result = rag_chain({"query": query})

answer = result["result"]

# Translate answer to Kiswahili

translated_answer = [Link](answer, src='en', dest='sw').text

# Display Results

[Link]("Answer (English):")

[Link](answer)

[Link]("Answer (Kiswahili):")

[Link](translated_answer)

# Show source documents

[Link]("Source Documents:")

for doc in result["source_documents"]:

[Link](doc.page_content[:300] + "...\n---")

[Link]

import streamlit as st from transformers import AutoTokenizer, AutoModelForCausalLM,


pipeline from [Link] import HuggingFacePipeline from langchain.document_loaders
import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from
[Link] import Pinecone from [Link] import RetrievalQA from
googletrans import Translator import pinecone

Initialize Mistral-7B Instruct Model

model_name = "mistralai/Mistral-7B-Instruct-v0.1" tokenizer =


AutoTokenizer.from_pretrained(model_name) model =
AutoModelForCausalLM.from_pretrained(model_name, device_map="auto") text_gen_pipeline
= pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512) llm =
HuggingFacePipeline(pipeline=text_gen_pipeline)

Load and split PDF

loader = PyPDFLoader("constitution_of_kenya.pdf") documents = [Link]() splitter =


RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) docs =
splitter.split_documents(documents) texts = [doc.page_content for doc in docs]

Initialize Pinecone

[Link](api_key="${PINECONE_API_KEY}", environment="${PINECONE_ENV}")
index_name = "kenya_constitution" vector_store = Pinecone.from_texts(texts, embedding=llm,
index_name=index_name) retriever = vector_store.as_retriever(search_type="similarity",
search_kwargs={"k": 5}) rag_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever,
return_source_documents=True)

Translator

translator = Translator()

Streamlit UI

st.set_page_config(page_title="Kenya Constitution Q&A") [Link]("Kenya Constitution Q&A with


Mistral-7B")

query = st.text_input("Ask a question about the Constitution of Kenya:")

if query: result = rag_chain({"query": query}) answer = result["result"] sw =


[Link](answer, src='en', dest='sw').text

[Link]("Answer (English):")

[Link](answer)

[Link]("Answer (Kiswahili):")

[Link](sw)

[Link]("Source Chunks:")

for doc in result["source_documents"]:

[Link](doc.page_content[:300] + "...\n---")

Dockerfile
-----

Use an official Python runtime

FROM python:3.10-slim WORKDIR /app COPY [Link] ./[Link] RUN pip


install --no-cache-dir -r [Link] COPY . . EXPOSE 3000 CMD ["streamlit", "run",
"[Link]", "--[Link]", "${PORT}", "--[Link]", "[Link]"]

[Link]

-----

{ "version": 2, "builds": [ { "src": "Dockerfile", "use": "@vercel/docker" } ], "routes": [ { "src":


"/(.*)", "dest": "/" } ] }

Morris Munene

2:11 PM

Make a multi-modal agent (Doctor) that will integrate pictures and a question and
answer from the both.

Sasa, the guys wa business plan did a financial projection based on the Massachusetts market so
well need to ensure the EIA aligns with it. Just adjust that after the third year and we have
established our business model and achieve 15% market share then we can reach those
numbers. THe first few years are usually market testing

Just update the numbers and send ASAP we file by Friday. We say those numbers will be
achieved in 10 years of operation

You might also like