Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Required Libraries Installation
|
2 |
+
!pip install transformers sentence-transformers faiss-cpu streamlit
|
3 |
+
|
4 |
+
# Import necessary modules
|
5 |
+
from transformers import pipeline
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
import faiss
|
8 |
+
import numpy as np
|
9 |
+
import streamlit as st
|
10 |
+
|
11 |
+
# Initialize a Question-Answering model from Hugging Face
|
12 |
+
question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
13 |
+
|
14 |
+
# Example dataset on economic and population growth trends
|
15 |
+
documents = [
|
16 |
+
{"id": 1, "text": "Global economic growth is projected to slow down due to inflation."},
|
17 |
+
{"id": 2, "text": "Population growth in developing countries continues to increase."},
|
18 |
+
{"id": 3, "text": "Economic growth in advanced economies is experiencing fluctuations due to market changes."},
|
19 |
+
# Additional documents can be added here
|
20 |
+
]
|
21 |
+
|
22 |
+
# Embed documents using SentenceTransformer for retrieval
|
23 |
+
embedder = SentenceTransformer('all-MiniLM-L6-v2') # Lightweight model for embeddings
|
24 |
+
document_embeddings = [embedder.encode(doc['text']) for doc in documents]
|
25 |
+
|
26 |
+
# Convert embeddings to a FAISS index for similarity search
|
27 |
+
dimension = 384 # Make sure this matches the SentenceTransformer embedding dimension
|
28 |
+
index = faiss.IndexFlatL2(dimension)
|
29 |
+
index.add(np.array(document_embeddings))
|
30 |
+
|
31 |
+
# Function for retrieving relevant documents based on query
|
32 |
+
def retrieve_documents(query, top_k=3):
|
33 |
+
query_embedding = embedder.encode(query).reshape(1, -1)
|
34 |
+
distances, indices = index.search(query_embedding, top_k)
|
35 |
+
return [documents[i]['text'] for i in indices[0]]
|
36 |
+
|
37 |
+
# Function to generate an answer using the retrieved documents
|
38 |
+
def ask_question(question):
|
39 |
+
retrieved_docs = retrieve_documents(question)
|
40 |
+
context = " ".join(retrieved_docs)
|
41 |
+
answer = question_answerer(question=question, context=context)
|
42 |
+
return answer['answer']
|
43 |
+
|
44 |
+
# Streamlit Interface for the RAG App
|
45 |
+
st.title("Economic and Population Growth Advisor")
|
46 |
+
st.write("Ask questions related to economic and population growth. This app uses retrieval-augmented generation to provide answers based on relevant documents.")
|
47 |
+
|
48 |
+
# Input for the question
|
49 |
+
question = st.text_input("Enter your question:")
|
50 |
+
if question:
|
51 |
+
answer = ask_question(question)
|
52 |
+
st.write("Answer:", answer)
|