Spaces:
Sleeping
Sleeping
| # 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() | |