chkla/AnnotationPromptCards
0
1import streamlit as st2import os3import frontmatter4import json5# import pyperclip6 7def load_prompts():8 prompts = []9 prompt_dir = "prompts"10 11 for filename in os.listdir(prompt_dir):12 if filename.endswith(".md"):13 with open(os.path.join(prompt_dir, filename), "r") as file:14 prompt = frontmatter.load(file)15 prompts.append({16 "id": prompt.get("id"),17 "title": prompt.get("title"),18 "author": prompt.get("author"),19 "paper": prompt.get("paperlink"),20 "date": prompt.get("date"),21 "language": prompt.get("language"),22 "task": prompt.get("task"),23 "version": prompt.get("version"),24 "addedby": prompt.get("addedby"),25 "keywords": prompt.get("keywords"),26 "content": prompt.content27 })28 29 return prompts30 31def render_prompt_details(prompt):32 st.markdown(f"# Card: {prompt['title']}")33 st.write("---")34 st.write(f"**Author:** {prompt['author']}")35 st.write(f"**Paper:** {prompt['paper']}")36 st.write(f"**Date:** {prompt['date']}")37 st.write(f"**Language:** {prompt['language']}")38 st.write(f"**Task:** {prompt['task']}")39 st.write(f"**Keywords:** {prompt['keywords']}")40 st.write(f"**Version:** {prompt['version']}")41 st.write(f"_Added By: {prompt['addedby']}_")42 st.write("---")43 st.write(prompt["content"])44 45def render_prompt_card(prompt):46 card_style = """47 <style>48 .card {49 background-color: #f8f9fa;50 border: 1px solid #d9d9d9;51 border-radius: 5px;52 padding: 15px;53 margin-bottom: 10px;54 box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);55 display: flex;56 justify-content: space-between;57 align-items: center;58 }59 .card-content {60 display: flex;61 flex-direction: column;62 }63 .button-style {64 background-color: #FFD700;65 border: none;66 color: white;67 text-align: center;68 text-decoration: none;69 display: inline-block;70 font-size: 16px;71 margin: 4px 2px;72 cursor: pointer;73 border-radius: 4px;74 }75 </style>76 """77 78 st.markdown(card_style, unsafe_allow_html=True)79 80 card_container = st.container()81 82 with card_container:83 with st.container():84 col1, col2 = st.columns([4, 1])85 86 with col1:87 st.write(f"### **Card:** {prompt['title']}")88 st.write(f"**Author:** {prompt['author']}")89 st.write(f"**Paper:** {prompt['paper']}")90 st.write(f"**Date:** {prompt['date']}")91 st.write(f"**Prompt Language:** {prompt['language']}")92 st.write(f"**Annotation Task:** {prompt['task']}")93 st.write(f"**Keywords:** {prompt['keywords']}")94 st.write(f"_Added by: {prompt['addedby']}_")95 96 with col2:97 if st.button("View details", key=f"view-details-{prompt['id']}"):98 st.session_state.selected_prompt_id = prompt["id"]99 st.experimental_rerun()100 101 st.markdown("<style>.button-style {background-color: #FFD700;}</style>", unsafe_allow_html=True)102 103 card_container.container().write("---")104 105def main():106 if "selected_prompt_id" not in st.session_state:107 st.session_state.selected_prompt_id = None108 109 st.title("Annotation PromptCards ๐ท๏ธ ๐ ๐ค")110 st.write("Welcome to the Prompt Archive! Click on the 'View details' button on each prompt card to see more information about the annotation prompt. Check out our [website](https://flask-prompt-annotations.vercel.app). Happy searching ๐ง๐ฝโ๐!")111 st.write("---")112 113 prompts = load_prompts()114 language_list = list(set([prompt['language'] for prompt in prompts]))115 task_list = list(set([prompt['task'] for prompt in prompts]))116 117 st.sidebar.header("**Annotation PromptCards**")118 119 st.sidebar.write("A collection of prompts for annotation tasks in NLP. This is a work in progress. Please contribute your prompts via GitHub [[Upload]](https://github.com/chkla/PromptCards) ๐๐ฝ.")120 121 # add a link to the GitHub repository122 st.sidebar.write("---")123 st.sidebar.write(f"**Total number of prompts:** {len(prompts)}")124 st.sidebar.write("---")125 126 st.sidebar.header("๐ง๐ฝโ๐ Explore:")127 search = st.sidebar.text_input("Search by title")128 language_filter = st.sidebar.selectbox("Filter by Language", ["All"] + language_list)129 task_filter = st.sidebar.selectbox("Filter by Task", ["All"] + task_list)130 131 if st.sidebar.button("Back to list"):132 st.session_state.selected_prompt_id = None133 st.experimental_rerun()134 135 if st.session_state.selected_prompt_id is None:136 filtered_prompts = [137 prompt for prompt in prompts138 if (search.lower() in prompt['title'].lower() or not search)139 and (prompt['language'] == language_filter or language_filter == "All")140 and (prompt['task'] == task_filter or task_filter == "All")141 ]142 143 for prompt in filtered_prompts:144 render_prompt_card(prompt)145 else:146 prompt = next((p for p in prompts if p["id"] == st.session_state.selected_prompt_id), None)147 if prompt:148 render_prompt_details(prompt)149 150 st.sidebar.write("---")151 st.sidebar.write("Made with โค๏ธ and ๐ค by [chkla](klamm.ai).")152 153if __name__ == "__main__":154 main()155 