import gradio as gr import os from datetime import datetime NOTES_FILE = "notes_storage.txt" # Ensure notes file exists if not os.path.exists(NOTES_FILE): with open(NOTES_FILE, "w", encoding="utf-8") as f: pass # -------- Core Functions -------- # def add_note(title, content): if not title.strip() or not content.strip(): return "⚠️ Title and content cannot be empty!", get_notes() timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") formatted_note = f""" 🗂 Title: {title} 🕒 Time: {timestamp} 📝 Note: {content} {'-'*50} """ with open(NOTES_FILE, "a", encoding="utf-8") as f: f.write(formatted_note) return "✅ Note Added Successfully!", get_notes() def get_notes(): if not os.path.exists(NOTES_FILE): return "✨ No notes yet." with open(NOTES_FILE, "r", encoding="utf-8") as f: data = f.read() return data if data else "✨ No notes yet. Start writing something amazing!" def clear_notes(): with open(NOTES_FILE, "w", encoding="utf-8") as f: f.write("") return "🗑️ All notes cleared!", "✨ No notes yet." def download_notes(): return NOTES_FILE # -------- UI Design -------- # with gr.Blocks( theme=gr.themes.Soft( primary_hue="violet", secondary_hue="blue", ) ) as demo: gr.Markdown( """
Beautiful • Organized • Simple • Powerful
""" ) with gr.Row(): with gr.Column(): gr.Markdown("### ✍️ Create a New Note") title_input = gr.Textbox( label="Title", placeholder="Enter note title..." ) content_input = gr.Textbox( label="Content", placeholder="Write your note here...", lines=8 ) add_btn = gr.Button("➕ Add Note", variant="primary") clear_btn = gr.Button("🗑 Clear All Notes", variant="stop") status_output = gr.Textbox( label="Status", interactive=False ) with gr.Column(): gr.Markdown("### 📚 Your Notes") notes_display = gr.Textbox( lines=20, interactive=False ) refresh_btn = gr.Button("🔄 Refresh Notes") download_btn = gr.DownloadButton("📥 Download Notes") # Button Actions add_btn.click( add_note, inputs=[title_input, content_input], outputs=[status_output, notes_display] ) clear_btn.click( clear_notes, outputs=[status_output, notes_display] ) refresh_btn.click( get_notes, outputs=notes_display ) download_btn.click( download_notes, outputs=download_btn ) demo.load(get_notes, outputs=notes_display) # 🔥 IMPORTANT FIX FOR HUGGING FACE if __name__ == "__main__": demo.launch()