Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,118 @@
|
|
| 1 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
-
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 3 |
-
from langchain_community.embeddings import OllamaEmbeddings
|
| 4 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
-
from langchain_community.vectorstores import Chroma
|
| 6 |
-
import os
|
| 7 |
-
from langchain.retrievers.multi_query import MultiQueryRetriever
|
| 8 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 9 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
-
from langchain.prompts import ChatPromptTemplate, PromptTemplate
|
| 11 |
-
import streamlit as st
|
| 12 |
-
|
| 13 |
-
os.environ["OPENAI_API_KEY"] =st.secrets["OPENAI_API_KEY"]
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
vector_store = Chroma(embedding_function=embeddings, persist_directory="mining-rag")
|
| 20 |
-
print(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
You
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
answer
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 3 |
+
from langchain_community.embeddings import OllamaEmbeddings
|
| 4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import Chroma
|
| 6 |
+
import os
|
| 7 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
| 8 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 9 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
+
from langchain.prompts import ChatPromptTemplate, PromptTemplate
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
os.environ["OPENAI_API_KEY"] =st.secrets["OPENAI_API_KEY"]
|
| 14 |
+
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_resources():
|
| 17 |
+
llm = ChatOpenAI(model='gpt-4o', temperature=0.1)
|
| 18 |
+
embeddings = OpenAIEmbeddings()
|
| 19 |
+
vector_store = Chroma(embedding_function=embeddings, persist_directory="mining-rag")
|
| 20 |
+
print('vector store loaded')
|
| 21 |
+
return llm, vector_store
|
| 22 |
+
|
| 23 |
+
llm, vector_store = load_resources()
|
| 24 |
+
|
| 25 |
+
# Define the FAQ questions
|
| 26 |
+
faq_questions = [
|
| 27 |
+
"What are the primary methods of mineral extraction used in the mining industry?",
|
| 28 |
+
"How do open-pit mining and underground mining differ in terms of process and environmental impact?",
|
| 29 |
+
"What are the most common minerals extracted through mining, and what are their primary uses?",
|
| 30 |
+
"How do mineral exploration techniques like geophysical surveys and drilling work?",
|
| 31 |
+
"What are the environmental impacts of mining, and how can they be mitigated?",
|
| 32 |
+
"What role does beneficiation play in the mining process, and what are some common beneficiation techniques?",
|
| 33 |
+
"What are tailings, and how are they managed in mining operations?",
|
| 34 |
+
"How does the mining industry ensure the safety of its workers?",
|
| 35 |
+
"What are the key regulations and standards governing the mining industry?",
|
| 36 |
+
"How is technology transforming the mining industry, particularly in terms of automation and data analysis?",
|
| 37 |
+
"What is the significance of sustainable mining practices, and what initiatives support sustainability in mining?",
|
| 38 |
+
"How do economic factors such as commodity prices influence mining operations?",
|
| 39 |
+
"What are the potential impacts of mining on local communities, and how are these addressed?",
|
| 40 |
+
"What is the role of reclamation and rehabilitation in the mining lifecycle?",
|
| 41 |
+
"How do companies assess the feasibility of a mining project?",
|
| 42 |
+
"What are some examples of innovative mining technologies or practices that are currently being developed?",
|
| 43 |
+
"What is artisanal and small-scale mining, and what are its challenges and opportunities?",
|
| 44 |
+
"How do geopolitical factors affect the global mining industry?",
|
| 45 |
+
"What are some of the ethical considerations in mining, particularly in relation to conflict minerals?",
|
| 46 |
+
"How can mining companies ensure they are adhering to best practices in environmental, social, and governance (ESG) criteria?"
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
def get_answer(question):
|
| 50 |
+
QUERY_PROMPT = PromptTemplate(
|
| 51 |
+
input_variables=["question"],
|
| 52 |
+
template="""You are an AI language model assistant. Your task is to generate three
|
| 53 |
+
different versions of the given user question to retrieve relevant documents from
|
| 54 |
+
a vector database. By generating multiple perspectives on the user question, your
|
| 55 |
+
goal is to help the user overcome some of the limitations of the distance-based
|
| 56 |
+
similarity search. Provide these alternative questions separated by newlines.
|
| 57 |
+
Original question: {question}""",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
retriever = MultiQueryRetriever.from_llm(
|
| 61 |
+
vector_store.as_retriever(),
|
| 62 |
+
llm,
|
| 63 |
+
prompt=QUERY_PROMPT
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
WRITER_SYSTEM_PROMPT = "You are an AI critical thinker research assistant. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text."
|
| 67 |
+
RESEARCH_REPORT_TEMPLATE = """Information:
|
| 68 |
+
--------
|
| 69 |
+
{text}
|
| 70 |
+
--------
|
| 71 |
+
Using the above information, answer the following question or topic: "{question}" in a short manner-- \
|
| 72 |
+
The answer should focus on the answer to the question, should be well structured, informative, \
|
| 73 |
+
in depth, with facts and numbers if available and a minimum of 150 words and a maximum of 300 words.
|
| 74 |
+
You should strive to write the report using all relevant and necessary information provided.
|
| 75 |
+
You must write the report with markdown syntax.
|
| 76 |
+
You MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions.
|
| 77 |
+
You must write the sources used in the context. if any article is used, mentioned in the end.
|
| 78 |
+
Please do your best, this is very important to my career."""
|
| 79 |
+
|
| 80 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 81 |
+
[
|
| 82 |
+
("system", WRITER_SYSTEM_PROMPT),
|
| 83 |
+
("user", RESEARCH_REPORT_TEMPLATE),
|
| 84 |
+
]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
chain = (
|
| 88 |
+
{"text": retriever, "question": RunnablePassthrough()}
|
| 89 |
+
| prompt
|
| 90 |
+
| llm
|
| 91 |
+
| StrOutputParser()
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
answer = chain.invoke({"question": question})
|
| 95 |
+
return answer
|
| 96 |
+
|
| 97 |
+
# Streamlit UI
|
| 98 |
+
st.title('Mining Minerals Expert RAG App')
|
| 99 |
+
|
| 100 |
+
# Sidebar with FAQ Section
|
| 101 |
+
st.sidebar.subheader("Frequently Asked Questions")
|
| 102 |
+
faq_expander = st.sidebar.expander("FAQs")
|
| 103 |
+
with faq_expander:
|
| 104 |
+
for i, faq in enumerate(faq_questions):
|
| 105 |
+
if st.sidebar.button(f"Q{i+1}: {faq}"):
|
| 106 |
+
answer = get_answer(faq)
|
| 107 |
+
st.sidebar.write(f"**Answer to Q{i+1}:** {answer}")
|
| 108 |
+
|
| 109 |
+
# Custom Question Section
|
| 110 |
+
st.subheader("Ask Your Own Question")
|
| 111 |
+
question = st.text_input('Write your question below:')
|
| 112 |
+
if st.button('Enter'):
|
| 113 |
+
if question:
|
| 114 |
+
answer = get_answer(question)
|
| 115 |
+
st.write(f"**Answer:** {answer}")
|
| 116 |
+
else:
|
| 117 |
+
st.write("Please enter a question.")
|
| 118 |
+
|