shee2205 commited on
Commit
272f768
Β·
verified Β·
1 Parent(s): 321cb55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -46
app.py CHANGED
@@ -13,6 +13,10 @@ def load_json(path, default):
13
  return json.load(f)
14
  return default
15
 
 
 
 
 
16
  profile = load_json(PROFILE_FILE, {})
17
  daily = load_json(DAILY_FILE, {})
18
 
@@ -73,53 +77,54 @@ def recent_logs():
73
  logs += f"**{d}**: {daily[d]['log']}\n"
74
  return logs
75
 
76
- SECRET_CODE = "1234" # Change this!
77
-
78
- def ui(request: gr.Request = None):
79
- admin_value = None
80
- if request is not None and hasattr(request, "query_params"):
81
- qp = request.query_params
82
- # In Spaces, query_params is a dict of lists (e.g. {'admin': ['1234']})
83
- if isinstance(qp, dict):
84
- admin_value = qp.get("admin", [None])[0]
85
- else:
86
- admin_value = qp.get("admin") if qp else None
87
-
88
- show_admin = (admin_value == SECRET_CODE)
89
-
90
- with gr.Blocks(title="Sheetal's Chatbot") as demo:
91
- gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
92
- gr.Markdown("Ask anything about Sheetal!")
93
-
94
- if show_admin:
95
- with gr.Tab("πŸ“ Admin (Sheetal)"):
96
- with gr.Row():
97
- name = gr.Textbox(label="Name", value=profile.get("name", "Sheetal"), max_lines=1)
98
- city = gr.Textbox(label="City", value=profile.get("city", ""), max_lines=1)
99
- job = gr.Textbox(label="Profession", value=profile.get("job", ""), max_lines=1)
100
- about = gr.Textbox(label="About You", value=profile.get("about", ""), lines=2, max_lines=3)
101
- today_log = gr.Textbox(label="What do you want to remember about today?", value=daily.get(str(date.today()), {}).get("log", ""), lines=2, max_lines=3)
102
- save_btn = gr.Button("πŸ’Ύ Save Profile & Today's Log")
103
- admin_status = gr.Markdown("")
104
- logs_output = gr.Markdown(recent_logs())
105
- save_btn.click(
106
- fn=save_profile_and_log,
107
- inputs=[name, city, job, about, today_log],
108
- outputs=[admin_status, logs_output]
109
- )
110
-
111
- with gr.Tab("πŸ’¬ Ask About Sheetal"):
112
- user_q = gr.Textbox(label="Type your question here:")
113
- ask_btn = gr.Button("Ask")
114
- answer_box = gr.Textbox(label="Bot answer", interactive=False, lines=2, max_lines=4)
115
- ask_btn.click(
116
- fn=chatbot_qa,
117
- inputs=user_q,
118
- outputs=answer_box
119
  )
120
- return demo
121
 
122
- # This is the official Gradio way to support context/request (2024+)
123
- demo = gr.Blocks(render_fn=ui)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  if __name__ == "__main__":
125
  demo.launch()
 
13
  return json.load(f)
14
  return default
15
 
16
+ def save_json(path, data):
17
+ with open(path, "w") as f:
18
+ json.dump(data, f, indent=2)
19
+
20
  profile = load_json(PROFILE_FILE, {})
21
  daily = load_json(DAILY_FILE, {})
22
 
 
77
  logs += f"**{d}**: {daily[d]['log']}\n"
78
  return logs
79
 
80
+ ADMIN_PASSWORD = "YourSecret123" # CHANGE THIS!
81
+
82
+ def check_password(password):
83
+ if password == ADMIN_PASSWORD:
84
+ return gr.update(visible=True), gr.update(value="", visible=False)
85
+ else:
86
+ return gr.update(visible=False), gr.update(value="❌ Access denied. Try again.", visible=True)
87
+
88
+ with gr.Blocks(title="Sheetal's Chatbot") as demo:
89
+ gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
90
+ gr.Markdown("Ask anything about Sheetal!")
91
+
92
+ with gr.Tab("πŸ“ Admin (Sheetal)"):
93
+ admin_password = gr.Textbox(label="Enter admin password", type="password", max_lines=1)
94
+ password_status = gr.Textbox(label="Password status", value="", interactive=False, visible=False)
95
+ unlock_panel = gr.Button("Unlock Admin Panel")
96
+
97
+ with gr.Column(visible=False) as admin_panel:
98
+ with gr.Row():
99
+ name = gr.Textbox(label="Name", value=profile.get("name", "Sheetal"), max_lines=1)
100
+ city = gr.Textbox(label="City", value=profile.get("city", ""), max_lines=1)
101
+ job = gr.Textbox(label="Profession", value=profile.get("job", ""), max_lines=1)
102
+ about = gr.Textbox(label="About You", value=profile.get("about", ""), lines=2, max_lines=3)
103
+ today_log = gr.Textbox(label="What do you want to remember about today?", value=daily.get(str(date.today()), {}).get("log", ""), lines=2, max_lines=3)
104
+ save_btn = gr.Button("πŸ’Ύ Save Profile & Today's Log")
105
+ admin_status = gr.Markdown("")
106
+ logs_output = gr.Markdown(recent_logs())
107
+ save_btn.click(
108
+ fn=save_profile_and_log,
109
+ inputs=[name, city, job, about, today_log],
110
+ outputs=[admin_status, logs_output]
 
 
 
 
 
 
 
 
 
 
 
 
111
  )
 
112
 
113
+ unlock_panel.click(
114
+ fn=check_password,
115
+ inputs=[admin_password],
116
+ outputs=[admin_panel, password_status]
117
+ )
118
+
119
+ with gr.Tab("πŸ’¬ Ask About Sheetal"):
120
+ user_q = gr.Textbox(label="Type your question here:")
121
+ ask_btn = gr.Button("Ask")
122
+ answer_box = gr.Textbox(label="Bot answer", interactive=False, lines=2, max_lines=4)
123
+ ask_btn.click(
124
+ fn=chatbot_qa,
125
+ inputs=user_q,
126
+ outputs=answer_box
127
+ )
128
+
129
  if __name__ == "__main__":
130
  demo.launch()