shee2205 commited on
Commit
abf5905
Β·
verified Β·
1 Parent(s): c770626

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -17
app.py CHANGED
@@ -30,7 +30,7 @@ def build_context(profile, daily):
30
  def get_llm():
31
  return pipeline(
32
  "text-generation",
33
- model="google/flan-t5-small", # Fast!
34
  max_new_tokens=128,
35
  do_sample=True,
36
  temperature=0.7,
@@ -48,19 +48,73 @@ def chatbot_qa(user_q):
48
  answer = outputs[0]["generated_text"].split("Assistant:")[-1].strip()
49
  return answer
50
 
51
- with gr.Blocks(title="Sheetal's Personal Chatbot") as demo:
52
- gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
53
- gr.Markdown("Ask anything about Sheetal!")
54
-
55
- with gr.Tab("πŸ’¬ Ask About Sheetal"):
56
- gr.Markdown("### πŸ’¬ Ask Anything About Sheetal")
57
- user_q = gr.Textbox(label="Type your question here:")
58
- ask_btn = gr.Button("Ask")
59
- answer_box = gr.Textbox(label="Bot answer", interactive=False, lines=2, max_lines=4)
60
- ask_btn.click(
61
- fn=chatbot_qa,
62
- inputs=user_q,
63
- outputs=answer_box
64
- )
65
-
66
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def get_llm():
31
  return pipeline(
32
  "text-generation",
33
+ model="google/flan-t5-small",
34
  max_new_tokens=128,
35
  do_sample=True,
36
  temperature=0.7,
 
48
  answer = outputs[0]["generated_text"].split("Assistant:")[-1].strip()
49
  return answer
50
 
51
+ def save_profile_and_log(name, city, job, about, freeform):
52
+ today = str(date.today())
53
+ profile.update({
54
+ "name": name,
55
+ "city": city,
56
+ "job": job,
57
+ "about": about
58
+ })
59
+ save_json(PROFILE_FILE, profile)
60
+ daily[today] = {"log": freeform}
61
+ save_json(DAILY_FILE, daily)
62
+ logs = ""
63
+ for d in sorted(daily.keys(), reverse=True)[:5]:
64
+ logs += f"**{d}**: {daily[d]['log']}\n"
65
+ return (
66
+ "βœ… Profile & log updated!",
67
+ logs
68
+ )
69
+
70
+ def recent_logs():
71
+ logs = ""
72
+ for d in sorted(daily.keys(), reverse=True)[:5]:
73
+ logs += f"**{d}**: {daily[d]['log']}\n"
74
+ return logs
75
+
76
+ SECRET_CODE = "1234" # Change this to your secret
77
+
78
+ def build_ui(request: gr.Request):
79
+ # Check for secret in URL, e.g., ?admin=1234
80
+ query_params = dict(request.query_params)
81
+ show_admin = query_params.get("admin", [None])[0] == SECRET_CODE
82
+
83
+ with gr.Blocks(title="Sheetal's Chatbot") as demo:
84
+ gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
85
+ gr.Markdown("Ask anything about Sheetal!")
86
+
87
+ if show_admin:
88
+ with gr.Tab("πŸ“ Admin (Sheetal)"):
89
+ with gr.Row():
90
+ name = gr.Textbox(label="Name", value=profile.get("name", "Sheetal"), max_lines=1)
91
+ city = gr.Textbox(label="City", value=profile.get("city", ""), max_lines=1)
92
+ job = gr.Textbox(label="Profession", value=profile.get("job", ""), max_lines=1)
93
+ about = gr.Textbox(label="About You", value=profile.get("about", ""), lines=2, max_lines=3)
94
+ 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)
95
+ save_btn = gr.Button("πŸ’Ύ Save Profile & Today's Log")
96
+ admin_status = gr.Markdown("")
97
+ logs_output = gr.Markdown(recent_logs())
98
+ save_btn.click(
99
+ fn=save_profile_and_log,
100
+ inputs=[name, city, job, about, today_log],
101
+ outputs=[admin_status, logs_output]
102
+ )
103
+
104
+ with gr.Tab("πŸ’¬ Ask About Sheetal"):
105
+ user_q = gr.Textbox(label="Type your question here:")
106
+ ask_btn = gr.Button("Ask")
107
+ answer_box = gr.Textbox(label="Bot answer", interactive=False, lines=2, max_lines=4)
108
+ ask_btn.click(
109
+ fn=chatbot_qa,
110
+ inputs=user_q,
111
+ outputs=answer_box
112
+ )
113
+ return demo
114
+
115
+ gr.mount_gradio_app(app=build_ui, path="/", root_path="")
116
+
117
+ # For Hugging Face Spaces, use this launch line:
118
+ demo = build_ui
119
+ if __name__ == "__main__":
120
+ demo.launch()