| import streamlit as st |
| import pickle |
| import cohere |
| import os |
|
|
| |
| api_key = os.environ["API_KEY"] |
| model_id= os.environ['MODEL_ID'] |
| co = cohere.Client(api_key) |
|
|
| |
| |
| def generate_text(prompt, temperature): |
| response = co.generate( |
| model=model_id, |
| prompt=prompt, |
| max_tokens= 2000, |
| temperature=temperature, |
| stop_sequences=['\n\n'], |
| ) |
| return response |
|
|
| |
| def save_chat_history(chat_history): |
| with open("chat_history.pkl", "wb") as file: |
| pickle.dump(chat_history, file) |
|
|
| |
| def load_chat_history(): |
| try: |
| with open("chat_history.pkl", "rb") as file: |
| return pickle.load(file) |
| except FileNotFoundError: |
| return [] |
|
|
| |
| def main(): |
| st.markdown( |
| """ |
| <style> |
| body { |
| background-image: url("https://www.creativefabrica.com/wp-content/uploads/2020/08/09/Set-of-hand-drawn-books-in-doodle-style-Graphics-4900608-1-580x385.jpg"); /* Replace "background.jpg" with your custom image */ |
| background-size: cover; |
| } |
| </style> |
| """, |
| unsafe_allow_html=True |
| ) |
| st.title("EdGuard ππ€") |
| st.write("Welcome to EdGuard your learning partner! ππ") |
| st.write("Use the example prompt or write your own. βπΌ") |
| |
| |
| chat_history = load_chat_history() |
| |
| |
| temperature = st.slider("Creativity", min_value=0.1, max_value=1.0, step=0.1, value=0.5) |
| |
| |
| example_prompts = [ |
| "A ball is thrown vertically upward with an initial velocity of 20 m/s. Assuming no air resistance, calculate the time it takes for the ball to reach its maximum height and the maximum height reached by the ball.", |
| "Calculate the number of moles of sodium chloride (NaCl) present in 250 grams of NaCl. (Molar Mass of NaCl = 58.44 g/mol)", |
| "What is the value of x: ((7x*2)/9)+8=10. ", |
| "A bakery sells cupcakes for $2.50 each. If Sarah buys 5 cupcakes and John buys 3 cupcakes, how much do they spend in total?" |
| ] |
| |
| |
| prompt_option = st.selectbox("Choose or enter a query:", ["Select an example query"] + example_prompts + ["Enter your own query"]) |
| |
| |
| if prompt_option == "Enter your own query": |
| prompt = st.text_area("Start Writing:") |
| elif prompt_option == "Select an example query": |
| prompt = "" |
| else: |
| prompt = prompt_option |
| |
| |
| if st.button("Let's start learning!"): |
| with st.spinner("Generating..."): |
| generated_text = generate_text(prompt, temperature) |
| st.success("Yay!π I found something, let's look at it.") |
| st.write(generated_text) |
| |
| |
| chat_history.append({"Question": prompt, "Response": generated_text}) |
| save_chat_history(chat_history) |
|
|
| if __name__ == "__main__": |
| main() |
|
|