Musawir19 commited on
Commit
8d93a66
·
verified ·
1 Parent(s): 9b7d6f9

Create APP

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