# 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 ret