Update app.py
Browse files
app.py
CHANGED
|
@@ -182,11 +182,11 @@ openai.api_key = openai_api_key
|
|
| 182 |
|
| 183 |
# Fetch Pinecone API key and environment from Streamlit secrets
|
| 184 |
pinecone_api_key = st.secrets["pinecone_api_key"]
|
| 185 |
-
pinecone_environment = st.secrets["pinecone_environment"]
|
| 186 |
|
| 187 |
# AUTHENTICATE/INITIALIZE PINCONE SERVICE
|
| 188 |
from pinecone import Pinecone
|
| 189 |
-
pc = Pinecone(api_key=pinecone_api_key)
|
| 190 |
# pc = Pinecone (api_key= 'YOUR_API_KEY')
|
| 191 |
|
| 192 |
# Define the name of the Pinecone index
|
|
@@ -200,54 +200,54 @@ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
|
| 200 |
from langchain_community.vectorstores import Pinecone
|
| 201 |
vector_store = Pinecone.from_existing_index(index_name='mimtssinkqa', embedding=embeddings)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
|
| 212 |
-
|
| 213 |
|
| 214 |
-
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
|
| 232 |
-
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
|
| 237 |
-
|
| 238 |
-
#
|
| 239 |
-
|
| 240 |
|
| 241 |
-
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
|
| 245 |
-
|
| 246 |
|
| 247 |
-
# #
|
| 248 |
|
| 249 |
-
#
|
| 250 |
-
#
|
| 251 |
|
| 252 |
#Add the image with a specified width
|
| 253 |
image_width = 300 # Set the desired width in pixels
|
|
@@ -259,82 +259,82 @@ st.markdown("""
|
|
| 259 |
Resource: **Intensifying Literacy Instruction: Essential Practices**
|
| 260 |
""", unsafe_allow_html=True)
|
| 261 |
|
| 262 |
-
|
| 263 |
-
#
|
| 264 |
-
#
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
|
| 274 |
-
#
|
| 275 |
-
|
| 276 |
-
#
|
| 277 |
-
|
| 278 |
|
| 279 |
-
#
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
#
|
| 302 |
-
#
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
#
|
| 319 |
-
|
| 320 |
-
|
| 321 |
|
| 322 |
-
|
| 323 |
-
#
|
| 324 |
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
|
| 329 |
-
#
|
| 330 |
-
|
| 331 |
|
| 332 |
-
|
| 333 |
|
| 334 |
-
#
|
| 335 |
-
#
|
| 336 |
-
#
|
| 337 |
-
|
| 338 |
|
| 339 |
-
#
|
| 340 |
-
|
|
|
|
| 182 |
|
| 183 |
# Fetch Pinecone API key and environment from Streamlit secrets
|
| 184 |
pinecone_api_key = st.secrets["pinecone_api_key"]
|
| 185 |
+
# pinecone_environment = st.secrets["pinecone_environment"]
|
| 186 |
|
| 187 |
# AUTHENTICATE/INITIALIZE PINCONE SERVICE
|
| 188 |
from pinecone import Pinecone
|
| 189 |
+
# pc = Pinecone(api_key=pinecone_api_key)
|
| 190 |
# pc = Pinecone (api_key= 'YOUR_API_KEY')
|
| 191 |
|
| 192 |
# Define the name of the Pinecone index
|
|
|
|
| 200 |
from langchain_community.vectorstores import Pinecone
|
| 201 |
vector_store = Pinecone.from_existing_index(index_name='mimtssinkqa', embedding=embeddings)
|
| 202 |
|
| 203 |
+
def ask_with_memory(vector_store, query, chat_history=[]):
|
| 204 |
+
from langchain_openai import ChatOpenAI
|
| 205 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 206 |
+
from langchain.memory import ConversationBufferMemory
|
| 207 |
|
| 208 |
+
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 209 |
|
| 210 |
+
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0.5)
|
| 211 |
|
| 212 |
+
retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3})
|
| 213 |
|
| 214 |
+
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 215 |
|
| 216 |
+
system_template = r'''
|
| 217 |
+
Use the following pieces of context to answer the user's question. The title of the article is Intensifying literacy Instruction: Essential Practices. Do not mention the Header unless asked.
|
| 218 |
+
----------------
|
| 219 |
+
Context: ```{context}```
|
| 220 |
+
'''
|
| 221 |
|
| 222 |
+
user_template = '''
|
| 223 |
+
Question: ```{question}```
|
| 224 |
+
Chat History: ```{chat_history}```
|
| 225 |
+
'''
|
| 226 |
|
| 227 |
+
messages= [
|
| 228 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
| 229 |
+
HumanMessagePromptTemplate.from_template(user_template)
|
| 230 |
+
]
|
| 231 |
|
| 232 |
+
qa_prompt = ChatPromptTemplate.from_messages (messages)
|
| 233 |
|
| 234 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory,chain_type='stuff', combine_docs_chain_kwargs={'prompt': qa_prompt}, verbose=False
|
| 235 |
+
)
|
| 236 |
|
| 237 |
+
result = chain.invoke({'question': query, 'chat_history': st.session_state['history']})
|
| 238 |
+
# Append to chat history as a dictionary
|
| 239 |
+
st.session_state['history'].append((query, result['answer']))
|
| 240 |
|
| 241 |
+
return (result['answer'])
|
| 242 |
|
| 243 |
+
# Initialize chat history
|
| 244 |
+
if 'history' not in st.session_state:
|
| 245 |
+
st.session_state['history'] = []
|
| 246 |
|
| 247 |
+
# # STREAMLIT APPLICATION SETUP WITH PASSWORD
|
| 248 |
|
| 249 |
+
# Define the correct password
|
| 250 |
+
# correct_password = "MiBLSi"
|
| 251 |
|
| 252 |
#Add the image with a specified width
|
| 253 |
image_width = 300 # Set the desired width in pixels
|
|
|
|
| 259 |
Resource: **Intensifying Literacy Instruction: Essential Practices**
|
| 260 |
""", unsafe_allow_html=True)
|
| 261 |
|
| 262 |
+
with st.sidebar:
|
| 263 |
+
# Password input field
|
| 264 |
+
# password = st.text_input("Enter Password:", type="password")
|
| 265 |
|
| 266 |
+
st.image('mimtss.png', width=200)
|
| 267 |
+
st.image('Literacy_Cover.png', width=200)
|
| 268 |
+
st.link_button("View | Download", "https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf")
|
| 269 |
|
| 270 |
+
Audio_Header_text = """
|
| 271 |
+
**Tune into Dr. St. Martin's introduction**"""
|
| 272 |
+
st.markdown(Audio_Header_text)
|
| 273 |
|
| 274 |
+
# Path or URL to the audio file
|
| 275 |
+
audio_file_path = 'Audio_Introduction_Literacy.m4a'
|
| 276 |
+
# Display the audio player widget
|
| 277 |
+
st.audio(audio_file_path, format='audio/mp4', start_time=0)
|
| 278 |
|
| 279 |
+
# Citation text with Markdown formatting
|
| 280 |
+
citation_Content_text = """
|
| 281 |
+
**Citation**
|
| 282 |
+
St. Martin, K., Vaughn, S., Troia, G., Fien, & H., Coyne, M. (2023). *Intensifying literacy instruction: Essential practices, Version 2.0*. Lansing, MI: MiMTSS Technical Assistance Center, Michigan Department of Education.
|
| 283 |
|
| 284 |
+
**Table of Contents**
|
| 285 |
+
* **Introduction**: pg. 1
|
| 286 |
+
* **Intensifying Literacy Instruction: Essential Practices**: pg. 4
|
| 287 |
+
* **Purpose**: pg. 4
|
| 288 |
+
* **Practice 1**: Knowledge and Use of a Learning Progression for Developing Skilled Readers and Writers: pg. 6
|
| 289 |
+
* **Practice 2**: Design and Use of an Intervention Platform as the Foundation for Effective Intervention: pg. 13
|
| 290 |
+
* **Practice 3**: On-going Data-Based Decision Making for Providing and Intensifying Interventions: pg. 16
|
| 291 |
+
* **Practice 4**: Adaptations to Increase the Instructional Intensity of the Intervention: pg. 20
|
| 292 |
+
* **Practice 5**: Infrastructures to Support Students with Significant and Persistent Literacy Needs: pg. 24
|
| 293 |
+
* **Motivation and Engagement**: pg. 28
|
| 294 |
+
* **Considerations for Understanding How Students' Learning and Behavior are Enhanced**: pg. 28
|
| 295 |
+
* **Summary**: pg. 29
|
| 296 |
+
* **Endnotes**: pg. 30
|
| 297 |
+
* **Acknowledgment**: pg. 39
|
| 298 |
+
"""
|
| 299 |
+
st.markdown(citation_Content_text)
|
| 300 |
+
|
| 301 |
+
# if password == correct_password:
|
| 302 |
+
# Define a list of possible placeholder texts
|
| 303 |
+
placeholders = [
|
| 304 |
+
'Example: Summarize the article in 200 words or less',
|
| 305 |
+
'Example: What are the essential practices?',
|
| 306 |
+
'Example: I am a teacher, why is this resource important?',
|
| 307 |
+
'Example: How can this resource support my instruction in reading and writing?',
|
| 308 |
+
'Example: Does this resource align with the learning progression for developing skilled readers and writers?',
|
| 309 |
+
'Example: How does this resource address the needs of students scoring below the 20th percentile?',
|
| 310 |
+
'Example: Are there assessment tools included in this resource to monitor student progress?',
|
| 311 |
+
'Example: Does this resource provide guidance on data collection and analysis for monitoring student outcomes?',
|
| 312 |
+
"Example: How can this resource be used to support students' social-emotional development?",
|
| 313 |
+
"Example: How does this resource align with the district's literacy goals and objectives?",
|
| 314 |
+
'Example: What research and evidence support the effectiveness of this resource?',
|
| 315 |
+
'Example: Does this resource provide guidance on implementation fidelity'
|
| 316 |
+
]
|
| 317 |
+
|
| 318 |
+
# Select a random placeholder from the list
|
| 319 |
+
if 'placeholder' not in st.session_state:
|
| 320 |
+
st.session_state.placeholder = random.choice(placeholders)
|
| 321 |
|
| 322 |
+
q = st.text_input(label='Ask a question or make a request ', value='', placeholder=st.session_state.placeholder)
|
| 323 |
+
# q = st.text_input(label='Ask a question or make a request ', value='')
|
| 324 |
|
| 325 |
+
if q:
|
| 326 |
+
with st.spinner('Thinking...'):
|
| 327 |
+
answer = ask_with_memory(vector_store, q, st.session_state.history)
|
| 328 |
|
| 329 |
+
# Display the response in a text area
|
| 330 |
+
st.text_area('Response: ', value=answer, height=400, key="response_text_area")
|
| 331 |
|
| 332 |
+
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
| 333 |
|
| 334 |
+
# Prepare chat history text for display
|
| 335 |
+
# history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in st.session_state.history)
|
| 336 |
+
# Prepare chat history text for display in reverse order
|
| 337 |
+
history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in reversed(st.session_state.history))
|
| 338 |
|
| 339 |
+
# Display chat history
|
| 340 |
+
st.text_area('Chat History', value=history_text, height=800)
|