Spaces:
No application file
No application file
Create aap.py
Browse files
aap.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import login
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
# Authenticate with Hugging Face using the API key from environment variables
|
| 10 |
+
# If running on Hugging Face Spaces, make sure the API key is stored as a secret.
|
| 11 |
+
login(os.environ["HF_API_KEY"])
|
| 12 |
+
|
| 13 |
+
# Initialize a question-answering model
|
| 14 |
+
question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 15 |
+
|
| 16 |
+
# Load or create data on Pakistan's economic and population growth trends
|
| 17 |
+
documents = [
|
| 18 |
+
{"id": 1, "text": "Pakistan's population growth rate is approximately 2%, making it one of the fastest-growing populations in South Asia."},
|
| 19 |
+
{"id": 2, "text": "The youth population in Pakistan is significant, with over 60% of the population under the age of 30."},
|
| 20 |
+
{"id": 3, "text": "Pakistan's economy relies heavily on agriculture, with about 20% of GDP coming from this sector."},
|
| 21 |
+
{"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."},
|
| 22 |
+
{"id": 5, "text": "Urbanization is rapidly increasing in Pakistan, with cities like Karachi and Lahore seeing substantial population inflows."},
|
| 23 |
+
{"id": 6, "text": "Remittances from overseas Pakistanis play a critical role in supporting the country's economy."},
|
| 24 |
+
{"id": 7, "text": "Pakistan's literacy rate has improved over the years but remains lower than the regional average."},
|
| 25 |
+
{"id": 8, "text": "The government is focusing on initiatives for digital economy growth, particularly in the technology and freelancing sectors."},
|
| 26 |
+
{"id": 9, "text": "Pakistan’s unemployment rate is a concern, especially among young people entering the job market."},
|
| 27 |
+
{"id": 10, "text": "The fertility rate in Pakistan has been declining but remains above the replacement rate."},
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Embed documents for retrieval using SentenceTransformer
|
| 31 |
+
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 32 |
+
document_embeddings = [embedder.encode(doc['text']) for doc in documents]
|
| 33 |
+
|
| 34 |
+
# Convert embeddings to a FAISS index for similarity search
|
| 35 |
+
index = faiss.IndexFlatL2(384) # Dimension of embeddings
|
| 36 |
+
index.add(np.array(document_embeddings))
|
| 37 |
+
|
| 38 |
+
# Define the RAG retrieval function
|
| 39 |
+
def retrieve_documents(query, top_k=3):
|
| 40 |
+
query_embedding = embedder.encode(query).reshape(1, -1)
|
| 41 |
+
distances, indices = index.search(query_embedding, top_k)
|
| 42 |
+
return [documents[i]['text'] for i in indices[0]]
|
| 43 |
+
|
| 44 |
+
# Implement the question-answering function with retrieval
|
| 45 |
+
def ask_question(question):
|
| 46 |
+
# Retrieve relevant documents
|
| 47 |
+
retrieved_docs = retrieve_documents(question)
|
| 48 |
+
# Combine retrieved documents into a single context
|
| 49 |
+
context = " ".join(retrieved_docs)
|
| 50 |
+
|
| 51 |
+
# Generate an answer based on retrieved context
|
| 52 |
+
answer = question_answerer(question=question, context=context)
|
| 53 |
+
return answer['answer']
|
| 54 |
+
|
| 55 |
+
# Streamlit Interface
|
| 56 |
+
def streamlit_interface():
|
| 57 |
+
# Set title and description
|
| 58 |
+
st.title("Pakistan Economic and Population Growth Advisor")
|
| 59 |
+
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.")
|
| 60 |
+
|
| 61 |
+
# Input: User enters a question
|
| 62 |
+
question = st.text_input("Ask a question:")
|
| 63 |
+
|
| 64 |
+
if question:
|
| 65 |
+
# Get the answer using the RAG system
|
| 66 |
+
answer = ask_question(question)
|
| 67 |
+
# Output the answer
|
| 68 |
+
st.write("Answer:", answer)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
# Run the Streamlit app
|
| 72 |
+
streamlit_interface()
|