soroushsrd commited on
Commit
59fa7b6
·
verified ·
1 Parent(s): d5af60c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -14,7 +14,7 @@ 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')
@@ -94,17 +94,32 @@ def get_answer(question):
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")
 
14
 
15
  @st.cache_resource
16
  def load_resources():
17
+ llm = ChatOpenAI(model='gpt-4o', temperature=0.2)
18
  embeddings = OpenAIEmbeddings()
19
  vector_store = Chroma(embedding_function=embeddings, persist_directory="mining-rag")
20
  print('vector store loaded')
 
94
  answer = chain.invoke({"question": question})
95
  return answer
96
 
97
+ # Precompute the answers to the FAQ questions
98
+ def precompute_faq_answers(faq_questions):
99
+ answers = {}
100
+ for faq in faq_questions:
101
+ answers[faq] = get_answer(faq)
102
+ return answers
103
+
104
+ # Cache the precomputed answers to avoid recomputation
105
+ @st.cache_data
106
+ def get_precomputed_answers():
107
+ return precompute_faq_answers(faq_questions)
108
+
109
+ precomputed_faq_answers = get_precomputed_answers()
110
+
111
  # Streamlit UI
112
  st.title('Mining Minerals Expert RAG App')
113
 
114
  # Sidebar with FAQ Section
115
  st.sidebar.subheader("Frequently Asked Questions")
116
+ # Show initial 10 questions
117
  faq_expander = st.sidebar.expander("FAQs")
118
  with faq_expander:
119
  for i, faq in enumerate(faq_questions):
120
+ if st.sidebar.button(f"Q{i+1}: {faq}", key=f"faq{i+1}"):
121
+ st.session_state.faq_answer = f"**Answer to Q{i+1}:** {precomputed_faq_answers[faq]}"
122
+
123
 
124
  # Custom Question Section
125
  st.subheader("Ask Your Own Question")