File size: 3,546 Bytes
7dde095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import os
from huggingface_hub import login
from transformers import pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import gradio as gr

# Step 1: Authenticate with Hugging Face using an environment variable
api_key = os.getenv("HF_API_KEY")  # Retrieves the API key from environment variables
login(api_key)

# Initialize a free question-answering model from Hugging Face
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."},
]

# Step 2: Embed documents for retrieval using SentenceTransformer
embedder = SentenceTransformer('all-MiniLM-L6-v2')  # A lightweight embedding model
document_embeddings = [embedder.encode(doc['text']) for doc in documents]
index = faiss.IndexFlatL2(384)  # Dimension of the embeddings
index.add(np.array(document_embeddings))

# Step 3: 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]]

# Step 4: 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)
    
    # Use the model to generate an answer based on retrieved context
    answer = question_answerer(question=question, context=context)
    return answer['answer']

# Step 5: Create Gradio Interface for the RAG app
def rag_interface(question):
    answer = ask_question(question)
    return answer

# Step 6: Launch the Gradio app
interface = gr.Interface(
    fn=rag_interface,
    inputs="text",
    outputs="text",
    title="Pakistan Economic and Population Growth Advisor",
    description="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."
)

interface.launch()