Musawir19's picture
Create app.py
8898224 verified
# Import necessary modules
from transformers import pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import gradio as gr
# 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."},
# Add more documents as needed
]
# 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 # Match this with the embedding model's output 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