makkzone commited on
Commit
f64c13f
·
verified ·
1 Parent(s): 75445bf

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +33 -0
  2. README.md +4 -10
  3. app.py +101 -0
  4. packages.txt +0 -0
  5. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13
2
+
3
+ WORKDIR /app
4
+
5
+ COPY ./requirements.txt /app/requirements.txt
6
+ COPY ./packages.txt /app/packages.txt
7
+
8
+ # Install curl and other dependencies
9
+ RUN apt-get update && apt-get install -y curl && apt-get clean && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Install Ollama
12
+ RUN curl -fsSL https://ollama.ai/install.sh | sh
13
+
14
+ RUN apt-get update && xargs -r -a /app/packages.txt apt-get install -y && rm -rf /var/lib/apt/lists/*
15
+ RUN pip3 install --no-cache-dir -r /app/requirements.txt
16
+
17
+ # User
18
+ RUN useradd -m -u 1000 user
19
+ USER user
20
+ ENV HOME /home/user
21
+ ENV PATH $HOME/.local/bin:$PATH
22
+
23
+ WORKDIR $HOME
24
+ RUN mkdir app
25
+ WORKDIR $HOME/app
26
+ COPY . $HOME/app
27
+
28
+ EXPOSE 8501
29
+ CMD ollama serve & sleep 5 && ollama pull llama3.2 && streamlit run app.py \
30
+ --server.headless true \
31
+ --server.enableCORS false \
32
+ --server.enableXsrfProtection false \
33
+ --server.fileWatcherType none
README.md CHANGED
@@ -1,11 +1,5 @@
1
- ---
2
- title: Projectresilience Assistant
3
- emoji: 📚
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
+ # QA Bot for Project Resilience
 
 
 
 
 
 
 
 
2
 
3
+ ## Overview
4
+
5
+ The QA Bot for [Project Resilience](https://www.itu.int/en/ITU-T/extcoop/ai-data-commons/Pages/project-resilience.aspx) is designed to assist public by answering queries related to Project Resilience. This bot leverages Generative AI to provide contextual responses to questions regarding sustainable development goals (SDGs), project proposals, contributions, and platform navigation.
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ from langchain.vectorstores import FAISS
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain_community.llms import Ollama
6
+ import requests
7
+
8
+ # Function to fetch GitHub repo data
9
+ def fetch_github_data(repo_url):
10
+ parts = repo_url.split('/')
11
+ owner, repo = parts[-2], parts[-1]
12
+
13
+ headers = {'Accept': 'application/vnd.github.v3+json'}
14
+ base_url = 'https://api.github.com'
15
+
16
+ content = ""
17
+ repo_response = requests.get(f"{base_url}/repos/{owner}/{repo}", headers=headers)
18
+ if repo_response.status_code == 200:
19
+ repo_data = repo_response.json()
20
+ content += f"Description: {repo_data.get('description', '')}\n"
21
+
22
+ readme_response = requests.get(f"{base_url}/repos/{owner}/{repo}/readme", headers=headers)
23
+ if readme_response.status_code == 200:
24
+ import base64
25
+ readme_data = readme_response.json()
26
+ content += base64.b64decode(readme_data['content']).decode('utf-8') + "\n"
27
+
28
+ return content
29
+
30
+ # Function to create vector store
31
+ def create_vector_store(text_data):
32
+ text_splitter = RecursiveCharacterTextSplitter(
33
+ chunk_size=1000,
34
+ chunk_overlap=200
35
+ )
36
+ chunks = text_splitter.split_text(text_data)
37
+
38
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
39
+ vector_store = FAISS.from_texts(chunks, embeddings)
40
+ return vector_store
41
+
42
+ # Streamlit app
43
+ def main():
44
+ st.title("Project Resilience Q&A Assistant")
45
+ st.write("Ask anything about Project Resilience - answers always come from repo data!")
46
+
47
+ # Hardcoded GitHub URL
48
+ github_url = 'https://github.com/Project-Resilience/platform'
49
+ repo_data = fetch_github_data(github_url)
50
+
51
+ # Initialize session state
52
+ if 'vector_store' not in st.session_state:
53
+ st.session_state.vector_store = create_vector_store(repo_data)
54
+ st.session_state.llm = Ollama(model="llama3.2", temperature=0.7)
55
+
56
+ # Question input
57
+ question = st.text_input("Ask a question about the project")
58
+
59
+ # Get and display answer
60
+ if question:
61
+ with st.spinner("Generating answer..."):
62
+ # Retrieve top-k documents
63
+ k = 3
64
+ docs = st.session_state.vector_store.similarity_search(question, k=k)
65
+
66
+ # Extract the text from the documents
67
+ context = "\n\n".join([doc.page_content for doc in docs])
68
+
69
+ # Create the custom prompt for zero-shot prompting
70
+ prompt = (
71
+ f"You are a helpful assistant answers to the question about project Project Resilience.\n"
72
+ f"Based on the following context, answer the question:\n\n"
73
+ f"Context:\n{context}\n\n"
74
+ f"Question: {question}\n\n"
75
+ f"If the question cannot be answered by the document, say so.\n\n"
76
+ f"Answer:"
77
+ )
78
+
79
+ # Generate the answer using the language model
80
+ answer_container = st.empty()
81
+ stream = st.session_state.llm.stream(prompt)
82
+ answer = ""
83
+ for chunk in stream:
84
+ answer += chunk
85
+ answer_container.write(answer)
86
+
87
+
88
+
89
+
90
+ # Sidebar with additional info
91
+ st.sidebar.header("Project Resilience Assistant")
92
+ st.sidebar.write("""
93
+ Project Resilience's platform for decision makers, data scientists and the public.
94
+
95
+ Project Resilience, initiated under the Global Initiative on AI and Data Commons, is a collaborative effort to build a public AI utility that could inform and help address global decision-augmentation challenges.
96
+
97
+ The project empowers a global community of innovators, thought leaders, and the public to enhance and use a shared collection of data and AI tools, improving preparedness, intervention, and response to environmental, health, information, or economic threats in our communities. It also supports broader efforts toward achieving the Sustainable Development Goals (SDGs).
98
+ """)
99
+
100
+ if __name__ == "__main__":
101
+ main()
packages.txt ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ faiss-cpu==1.10.0
2
+ langchain==0.3.20
3
+ langchain-community==0.3.19
4
+ langchain-ollama==0.2.3
5
+ langgraph==0.3.5
6
+ ollama==0.4.7
7
+ requests==2.32.3
8
+ sentence-transformers==3.4.1
9
+ streamlit==1.43.0