Musawir19 commited on
Commit
7dde095
·
verified ·
1 Parent(s): db6cede

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -1 +1,69 @@
1
  streamlit run app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  streamlit run app.py
2
+ # app.py
3
+ import os
4
+ from huggingface_hub import login
5
+ from transformers import pipeline
6
+ from sentence_transformers import SentenceTransformer
7
+ import faiss
8
+ import numpy as np
9
+ import gradio as gr
10
+
11
+ # Step 1: Authenticate with Hugging Face using an environment variable
12
+ api_key = os.getenv("HF_API_KEY") # Retrieves the API key from environment variables
13
+ login(api_key)
14
+
15
+ # Initialize a free question-answering model from Hugging Face
16
+ question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
17
+
18
+ # Load or create data on Pakistan's economic and population growth trends
19
+ documents = [
20
+ {"id": 1, "text": "Pakistan's population growth rate is approximately 2%, making it one of the fastest-growing populations in South Asia."},
21
+ {"id": 2, "text": "The youth population in Pakistan is significant, with over 60% of the population under the age of 30."},
22
+ {"id": 3, "text": "Pakistan's economy relies heavily on agriculture, with about 20% of GDP coming from this sector."},
23
+ {"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."},
24
+ {"id": 5, "text": "Urbanization is rapidly increasing in Pakistan, with cities like Karachi and Lahore seeing substantial population inflows."},
25
+ {"id": 6, "text": "Remittances from overseas Pakistanis play a critical role in supporting the country's economy."},
26
+ {"id": 7, "text": "Pakistan's literacy rate has improved over the years but remains lower than the regional average."},
27
+ {"id": 8, "text": "The government is focusing on initiatives for digital economy growth, particularly in the technology and freelancing sectors."},
28
+ {"id": 9, "text": "Pakistan’s unemployment rate is a concern, especially among young people entering the job market."},
29
+ {"id": 10, "text": "The fertility rate in Pakistan has been declining but remains above the replacement rate."},
30
+ ]
31
+
32
+ # Step 2: Embed documents for retrieval using SentenceTransformer
33
+ embedder = SentenceTransformer('all-MiniLM-L6-v2') # A lightweight embedding model
34
+ document_embeddings = [embedder.encode(doc['text']) for doc in documents]
35
+ index = faiss.IndexFlatL2(384) # Dimension of the embeddings
36
+ index.add(np.array(document_embeddings))
37
+
38
+ # Step 3: Define the RAG retrieval function
39
+ def retrieve_documents(query, top_k=3):
40
+ query_embedding = embedder.encode(query).reshape(1, -1)
41
+ distances, indices = index.search(query_embedding, top_k)
42
+ return [documents[i]['text'] for i in indices[0]]
43
+
44
+ # Step 4: Implement the question-answering function with retrieval
45
+ def ask_question(question):
46
+ # Retrieve relevant documents
47
+ retrieved_docs = retrieve_documents(question)
48
+ # Combine retrieved documents into a single context
49
+ context = " ".join(retrieved_docs)
50
+
51
+ # Use the model to generate an answer based on retrieved context
52
+ answer = question_answerer(question=question, context=context)
53
+ return answer['answer']
54
+
55
+ # Step 5: Create Gradio Interface for the RAG app
56
+ def rag_interface(question):
57
+ answer = ask_question(question)
58
+ return answer
59
+
60
+ # Step 6: Launch the Gradio app
61
+ interface = gr.Interface(
62
+ fn=rag_interface,
63
+ inputs="text",
64
+ outputs="text",
65
+ title="Pakistan Economic and Population Growth Advisor",
66
+ 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."
67
+ )
68
+
69
+ interface.launch()