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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pinecone
|
| 3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 4 |
+
from langchain.vectorstores import Pinecone, Chroma
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
import tiktoken
|
| 8 |
+
import random
|
| 9 |
+
|
| 10 |
+
# Fetch the OpenAI API key from Streamlit secrets
|
| 11 |
+
openai_api_key = st.secrets["openai_api_key"]
|
| 12 |
+
|
| 13 |
+
# Fetch Pinecone API key and environment from Streamlit secrets
|
| 14 |
+
pinecone_api_key = st.secrets["pinecone_api_key"]
|
| 15 |
+
pinecone_environment = st.secrets["pinecone_environment"]
|
| 16 |
+
|
| 17 |
+
# Initialize Pinecone
|
| 18 |
+
pinecone.init(api_key=pinecone_api_key, environment=pinecone_environment)
|
| 19 |
+
|
| 20 |
+
# Define the name of the Pinecone index
|
| 21 |
+
index_name = 'mi-resource-qa'
|
| 22 |
+
|
| 23 |
+
# Initialize the OpenAI embeddings object with the hardcoded API key
|
| 24 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
| 25 |
+
|
| 26 |
+
# Define functions
|
| 27 |
+
def insert_or_fetch_embeddings(index_name):
|
| 28 |
+
if index_name in pinecone.list_indexes():
|
| 29 |
+
vector_store = Pinecone.from_existing_index(index_name, embeddings)
|
| 30 |
+
return vector_store
|
| 31 |
+
else:
|
| 32 |
+
raise ValueError(f"Index {index_name} does not exist. Please create it before fetching.")
|
| 33 |
+
|
| 34 |
+
# Initialize or fetch Pinecone vector store
|
| 35 |
+
vector_store = insert_or_fetch_embeddings(index_name)
|
| 36 |
+
|
| 37 |
+
# Define the metadata for filtering
|
| 38 |
+
# metadata = {'source': '/Users/cheynelevesseur/Desktop/Python_Code/Projects/LLM/Intensifying Literacy Instruction - Essential Practices (NATIONAL).pdf'}
|
| 39 |
+
|
| 40 |
+
# calculate embedding cost using tiktoken
|
| 41 |
+
def calculate_embedding_cost(text):
|
| 42 |
+
import tiktoken
|
| 43 |
+
enc = tiktoken.encoding_for_model('text-embedding-ada-002')
|
| 44 |
+
total_tokens = len(enc.encode(text))
|
| 45 |
+
# print(f'Total Tokens: {total_tokens}')
|
| 46 |
+
# print(f'Embedding Cost in USD: {total_tokens / 1000 * 0.0004:.6f}')
|
| 47 |
+
return total_tokens, total_tokens / 1000 * 0.0004
|
| 48 |
+
|
| 49 |
+
def ask_with_memory(vector_store, query, chat_history=[]):
|
| 50 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 51 |
+
from langchain.chat_models import ChatOpenAI
|
| 52 |
+
|
| 53 |
+
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=1, openai_api_key=openai_api_key)
|
| 54 |
+
retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3})
|
| 55 |
+
|
| 56 |
+
chain= ConversationalRetrievalChain.from_llm(llm, retriever)
|
| 57 |
+
result = chain({'question': query, 'chat_history': st.session_state['history']})
|
| 58 |
+
# Append to chat history as a dictionary
|
| 59 |
+
st.session_state['history'].append((query, result['answer']))
|
| 60 |
+
|
| 61 |
+
return (result['answer'])
|
| 62 |
+
|
| 63 |
+
# Initialize chat history
|
| 64 |
+
if 'history' not in st.session_state:
|
| 65 |
+
st.session_state['history'] = []
|
| 66 |
+
|
| 67 |
+
# # STREAMLIT APPLICATION SETUP WITH PASSWORD
|
| 68 |
+
|
| 69 |
+
# Define the correct password
|
| 70 |
+
# correct_password = "MiBLSi"
|
| 71 |
+
|
| 72 |
+
#Add the image with a specified width
|
| 73 |
+
image_width = 300 # Set the desired width in pixels
|
| 74 |
+
st.image('MTSS.ai_Logo.png', width=image_width)
|
| 75 |
+
st.subheader('Ink QA™ | Dynamic PDFs')
|
| 76 |
+
|
| 77 |
+
# Using Markdown for formatted text
|
| 78 |
+
st.markdown("""
|
| 79 |
+
Resource: **Intensifying Literacy Instruction: Essential Practices**
|
| 80 |
+
""", unsafe_allow_html=True)
|
| 81 |
+
|
| 82 |
+
with st.sidebar:
|
| 83 |
+
# Password input field
|
| 84 |
+
# password = st.text_input("Enter Password:", type="password")
|
| 85 |
+
|
| 86 |
+
st.image('mimtss.png', width=200)
|
| 87 |
+
st.image('Literacy_Cover.png', width=200)
|
| 88 |
+
st.link_button("View | Download", "https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf")
|
| 89 |
+
|
| 90 |
+
Audio_Header_text = """
|
| 91 |
+
**Tune into Dr. St. Martin's introduction**"""
|
| 92 |
+
st.markdown(Audio_Header_text)
|
| 93 |
+
|
| 94 |
+
# Path or URL to the audio file
|
| 95 |
+
audio_file_path = '/Users/cheynelevesseur/Desktop/Python_Code/Projects/LLM/Streamlit_Document_Reader_Simple/Audio_Introduction_Literacy.m4a'
|
| 96 |
+
# Display the audio player widget
|
| 97 |
+
st.audio(audio_file_path, format='audio/mp4', start_time=0)
|
| 98 |
+
|
| 99 |
+
# Citation text with Markdown formatting
|
| 100 |
+
citation_Content_text = """
|
| 101 |
+
**Citation**
|
| 102 |
+
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.
|
| 103 |
+
|
| 104 |
+
**Table of Contents**
|
| 105 |
+
* **Introduction**: pg. 1
|
| 106 |
+
* **Intensifying Literacy Instruction: Essential Practices**: pg. 4
|
| 107 |
+
* **Purpose**: pg. 4
|
| 108 |
+
* **Practice 1**: Knowledge and Use of a Learning Progression for Developing Skilled Readers and Writers: pg. 6
|
| 109 |
+
* **Practice 2**: Design and Use of an Intervention Platform as the Foundation for Effective Intervention: pg. 13
|
| 110 |
+
* **Practice 3**: On-going Data-Based Decision Making for Providing and Intensifying Interventions: pg. 16
|
| 111 |
+
* **Practice 4**: Adaptations to Increase the Instructional Intensity of the Intervention: pg. 20
|
| 112 |
+
* **Practice 5**: Infrastructures to Support Students with Significant and Persistent Literacy Needs: pg. 24
|
| 113 |
+
* **Motivation and Engagement**: pg. 28
|
| 114 |
+
* **Considerations for Understanding How Students' Learning and Behavior are Enhanced**: pg. 28
|
| 115 |
+
* **Summary**: pg. 29
|
| 116 |
+
* **Endnotes**: pg. 30
|
| 117 |
+
* **Acknowledgment**: pg. 39
|
| 118 |
+
"""
|
| 119 |
+
st.markdown(citation_Content_text)
|
| 120 |
+
|
| 121 |
+
# if password == correct_password:
|
| 122 |
+
# Define a list of possible placeholder texts
|
| 123 |
+
placeholders = [
|
| 124 |
+
'Example: Summarize the article in 200 words or less',
|
| 125 |
+
'Example: What are the essential practices?',
|
| 126 |
+
'Example: I am a teacher, why is this resource important?',
|
| 127 |
+
'Example: How can this resource support my instruction in reading and writing?',
|
| 128 |
+
'Example: Does this resource align with the learning progression for developing skilled readers and writers?',
|
| 129 |
+
'Example: How does this resource address the needs of students scoring below the 20th percentile?',
|
| 130 |
+
'Example: Are there assessment tools included in this resource to monitor student progress?',
|
| 131 |
+
'Example: Does this resource provide guidance on data collection and analysis for monitoring student outcomes?',
|
| 132 |
+
"Example: How can this resource be used to support students' social-emotional development?",
|
| 133 |
+
"Example: How does this resource align with the district's literacy goals and objectives?",
|
| 134 |
+
'Example: What research and evidence support the effectiveness of this resource?',
|
| 135 |
+
'Example: Does this resource provide guidance on implementation fidelity'
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Select a random placeholder from the list
|
| 139 |
+
if 'placeholder' not in st.session_state:
|
| 140 |
+
st.session_state.placeholder = random.choice(placeholders)
|
| 141 |
+
|
| 142 |
+
q = st.text_input(label='Ask a question or make a request ', value='', placeholder=st.session_state.placeholder)
|
| 143 |
+
# q = st.text_input(label='Ask a question or make a request ', value='')
|
| 144 |
+
|
| 145 |
+
k = 3 # Set k to 3
|
| 146 |
+
|
| 147 |
+
# # Initialize chat history if not present
|
| 148 |
+
# if 'history' not in st.session_state:
|
| 149 |
+
# st.session_state.history = []
|
| 150 |
+
|
| 151 |
+
if q:
|
| 152 |
+
with st.spinner('Thinking...'):
|
| 153 |
+
answer = ask_with_memory(vector_store, q, st.session_state.history)
|
| 154 |
+
|
| 155 |
+
# Display the response in a text area
|
| 156 |
+
st.text_area('Response: ', value=answer, height=400, key="response_text_area")
|
| 157 |
+
|
| 158 |
+
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
| 159 |
+
|
| 160 |
+
# # Prepare chat history text for display
|
| 161 |
+
# history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in st.session_state.history)
|
| 162 |
+
# Prepare chat history text for display in reverse order
|
| 163 |
+
history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in reversed(st.session_state.history))
|
| 164 |
+
|
| 165 |
+
# Display chat history
|
| 166 |
+
st.text_area('Chat History', value=history_text, height=800)
|