Spaces:
Running
Running
Update app.py
Browse files
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",
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|