File size: 3,662 Bytes
ce4e314
 
 
 
 
 
 
 
9fce912
 
 
ce4e314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from huggingface_hub import login
from transformers import pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import streamlit as st

# Authenticate with Hugging Face using the API key from environment variables
# If running on Hugging Face Spaces, make sure the API key is stored as a secret.
login(os.environ["HF_API_KEY"])

# Initialize a question-answering model
question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")

# Load or create data on Pakistan's economic and population growth trends
documents = [
    {"id": 1, "text": "Pakistan's population growth rate is approximately 2%, making it one of the fastest-growing populations in South Asia."},
    {"id": 2, "text": "The youth population in Pakistan is significant, with over 60% of the population under the age of 30."},
    {"id": 3, "text": "Pakistan's economy relies heavily on agriculture, with about 20% of GDP coming from this sector."},
    {"id": 4, "text": "In recent years, Pakistan has been investing in infrastructure projects, such as the China-Pakistan Economic Corridor (CPEC), to boost economic growth."},
    {"id": 5, "text": "Urbanization is rapidly increasing in Pakistan, with cities like Karachi and Lahore seeing substantial population inflows."},
    {"id": 6, "text": "Remittances from overseas Pakistanis play a critical role in supporting the country's economy."},
    {"id": 7, "text": "Pakistan's literacy rate has improved over the years but remains lower than the regional average."},
    {"id": 8, "text": "The government is focusing on initiatives for digital economy growth, particularly in the technology and freelancing sectors."},
    {"id": 9, "text": "Pakistan’s unemployment rate is a concern, especially among young people entering the job market."},
    {"id": 10, "text": "The fertility rate in Pakistan has been declining but remains above the replacement rate."},
]

# Embed documents for retrieval using SentenceTransformer
embedder = SentenceTransformer('all-MiniLM-L6-v2')
document_embeddings = [embedder.encode(doc['text']) for doc in documents]

# Convert embeddings to a FAISS index for similarity search
index = faiss.IndexFlatL2(384)  # Dimension of embeddings
index.add(np.array(document_embeddings))

# Define the RAG retrieval function
def retrieve_documents(query, top_k=3):
    query_embedding = embedder.encode(query).reshape(1, -1)
    distances, indices = index.search(query_embedding, top_k)
    return [documents[i]['text'] for i in indices[0]]

# Implement the question-answering function with retrieval
def ask_question(question):
    # Retrieve relevant documents
    retrieved_docs = retrieve_documents(question)
    # Combine retrieved documents into a single context
    context = " ".join(retrieved_docs)
    
    # Generate an answer based on retrieved context
    answer = question_answerer(question=question, context=context)
    return answer['answer']

# Streamlit Interface
def streamlit_interface():
    # Set title and description
    st.title("Pakistan Economic and Population Growth Advisor")
    st.write("Ask questions related to Pakistan's economic and population growth. This app uses retrieval-augmented generation to provide answers based on relevant documents about Pakistan.")

    # Input: User enters a question
    question = st.text_input("Ask a question:")

    if question:
        # Get the answer using the RAG system
        answer = ask_question(question)
        # Output the answer
        st.write("Answer:", answer)

if __name__ == "__main__":
    # Run the Streamlit app
    streamlit_interface()