import streamlit as st from sentence_transformers import SentenceTransformer import torch import json from streamlit_lottie import st_lottie from notion_client import Client from datetime import datetime # Set page config at the very beginning st.set_page_config(page_title="Prompt easz", layout="centered") # Initialize Notion client notion=Client(auth=st.secrets["NOTION_API_KEY"]) NOTION_DATABASE_ID = st.secrets["notiondb"] # Custom CSS to increase text size, beautify the app, and highlight the final message st.markdown(""" """, unsafe_allow_html=True) @st.cache_resource def load_model(): return SentenceTransformer('all-MiniLM-L6-v2') @st.cache_data def load_prompts(): try: with open('prompts.json', 'r') as file: return json.load(file) except json.JSONDecodeError as e: st.error(f"Error loading prompts.json: {e}") return {} @st.cache_data def load_categories(): try: with open('categories.json', 'r') as file: return json.load(file) except json.JSONDecodeError as e: st.error(f"Error loading categories.json: {e}") return {} def load_lottiefile(filepath: str): with open(filepath, "r") as f: return json.load(f) def find_prompt_types(user_input, num_types=3): model = load_model() prompt_types = load_prompts() user_embedding = model.encode(user_input, convert_to_tensor=True) similarities = {} for pt, data in prompt_types.items(): all_keywords = " ".join([" ".join(variation["keywords"]) for variation in data]) pt_embedding = model.encode(all_keywords, convert_to_tensor=True) similarity = torch.cosine_similarity(user_embedding, pt_embedding, dim=0).item() similarities[pt] = similarity return sorted(similarities, key=similarities.get, reverse=True)[:num_types] def generate_prompt(prompt_type, topic, category, complexity, topic2=None, constraint=None): model = load_model() prompt_types = load_prompts() categories_info = load_categories() template = prompt_types[prompt_type][0]["template"] complexity_instructions = { "low": "Use simple language and basic concepts. Avoid technical jargon and provide elementary explanations suitable for beginners.", "medium": "Include more detailed explanations and introduce some field-specific terminology. Provide a balance between depth and accessibility.", "high": "Delve into advanced concepts and use specialized terminology. Assume a high level of prior knowledge and provide in-depth analysis." } category_info = categories_info.get(category, {}) analogy = category_info.get("analogy", "analogous") expert_role = category_info.get("expert_role", "an expert") related_topics = ", ".join(category_info.get("related_topics", [])) return f""" # {prompt_type} Prompt: {topic} in {category} {template.format(topic=topic, category=category, topic2=topic2, constraint=constraint)} **Complexity: {complexity.capitalize()}** {complexity_instructions[complexity]} **Category Details:** - **Analogy:** {analogy} - **Expert Role:** {expert_role} - **Related Topics:** {related_topics} **Additional Instructions:** - Ensure your response is well-structured and easy to follow. - Use relevant examples to illustrate key points. - Tailor your language to the specified complexity level. """.strip() def save_feedback_to_notion(user_prompt, improvement_suggestion, timestamp): try: notion.pages.create( parent={"database_id": NOTION_DATABASE_ID}, properties={ "User Prompt": {"title": [{"text": {"content": user_prompt}}]}, "Improvement Suggestion": {"rich_text": [{"text": {"content": improvement_suggestion}}]} } ) return True except Exception as e: # Log the error to the console instead of displaying it in the Streamlit app print(f"Error saving to Notion: {str(e)}") return False def main(): # Display the title and animation st.markdown('