# Required Libraries Installation !pip install transformers sentence-transformers faiss-cpu streamlit # Import necessary modules from transformers import pipeline from sentence_transformers import SentenceTransformer import faiss import numpy as np import streamlit as st # Initialize a Question-Answering model from Hugging Face question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") # Example dataset on economic and population growth trends documents = [ {"id": 1, "text": "Global economic growth is projected to slow down due to inflation."}, {"id": 2, "text": "Population growth in developing countries continues to increase."}, {"id": 3, "text": "Economic growth in advanced economies is experiencing fluctuations due to market changes."}, # Additional documents can be added here ] # Embed documents using SentenceTransformer for retrieval embedder = SentenceTransformer('all-MiniLM-L6-v2') # Lightweight model for embeddings document_embeddings = [embedder.encode(doc['text']) for doc in documents] # Convert embeddings to a FAISS index for similarity search dimension = 384 # Make sure this matches the SentenceTransformer embedding dimension index = faiss.IndexFlatL2(dimension) index.add(np.array(document_embeddings)) # Function for retrieving relevant documents based on query 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]] # Function to generate an answer using the retrieved documents def ask_question(question): retrieved_docs = retrieve_documents(question) context = " ".join(retrieved_docs) answer = question_answerer(question=question, context=context) return answer['answer'] # Streamlit Interface for the RAG App st.title("Economic and Population Growth Advisor") st.write("Ask questions related to economic and population growth. This app uses retrieval-augmented generation to provide answers based on relevant documents.") # Input for the question question = st.text_input("Enter your question:") if question: answer = ask_question(question) st.write("Answer:", answer)