Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,10 +13,6 @@ def load_json(path, default):
|
|
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 |
|
@@ -34,11 +30,10 @@ def build_context(profile, daily):
|
|
34 |
def get_llm():
|
35 |
return pipeline(
|
36 |
"text-generation",
|
37 |
-
model="
|
38 |
-
max_new_tokens=
|
39 |
do_sample=True,
|
40 |
temperature=0.7,
|
41 |
-
trust_remote_code=True,
|
42 |
)
|
43 |
|
44 |
llm = None
|
@@ -49,84 +44,19 @@ def chatbot_qa(user_q):
|
|
49 |
llm = get_llm()
|
50 |
context = build_context(profile, daily)
|
51 |
prompt = f"System: {context}\nUser: {user_q}\nAssistant:"
|
52 |
-
outputs = llm(prompt, max_new_tokens=
|
53 |
answer = outputs[0]["generated_text"].split("Assistant:")[-1].strip()
|
54 |
return answer
|
55 |
|
56 |
-
def update_profile(name, city, job, about):
|
57 |
-
profile.update({
|
58 |
-
"name": name,
|
59 |
-
"city": city,
|
60 |
-
"job": job,
|
61 |
-
"about": about
|
62 |
-
})
|
63 |
-
save_json(PROFILE_FILE, profile)
|
64 |
-
return "Profile updated!"
|
65 |
-
|
66 |
-
def update_daily_log(freeform):
|
67 |
-
today = str(date.today())
|
68 |
-
daily[today] = {"log": freeform}
|
69 |
-
save_json(DAILY_FILE, daily)
|
70 |
-
return f"Today's log saved! ({today})"
|
71 |
-
|
72 |
-
def get_profile_defaults():
|
73 |
-
return (
|
74 |
-
profile.get("name", "Sheetal"),
|
75 |
-
profile.get("city", ""),
|
76 |
-
profile.get("job", ""),
|
77 |
-
profile.get("about", "")
|
78 |
-
)
|
79 |
-
|
80 |
-
def get_today_log():
|
81 |
-
today = str(date.today())
|
82 |
-
return daily.get(today, {}).get("log", "")
|
83 |
-
|
84 |
-
def recent_logs():
|
85 |
-
logs = ""
|
86 |
-
for d in sorted(daily.keys(), reverse=True)[:5]:
|
87 |
-
logs += f"**{d}**: {daily[d]['log']}\n"
|
88 |
-
return logs
|
89 |
-
|
90 |
with gr.Blocks(title="Sheetal's Personal Chatbot") as demo:
|
91 |
gr.Markdown("# πΈ Sheetal's Personal Chatbot")
|
92 |
-
|
93 |
-
with gr.Tab("π Admin (Sheetal)"):
|
94 |
-
gr.Markdown("### Edit Your Profile")
|
95 |
-
with gr.Row():
|
96 |
-
name = gr.Textbox(label="Name", value=profile.get("name", "Sheetal"))
|
97 |
-
city = gr.Textbox(label="City", value=profile.get("city", ""))
|
98 |
-
job = gr.Textbox(label="Profession", value=profile.get("job", ""))
|
99 |
-
about = gr.Textbox(label="A few lines about you", value=profile.get("about", ""))
|
100 |
-
save_profile_btn = gr.Button("Save Profile")
|
101 |
-
profile_output = gr.Textbox(label="", interactive=False)
|
102 |
-
|
103 |
-
save_profile_btn.click(
|
104 |
-
fn=update_profile,
|
105 |
-
inputs=[name, city, job, about],
|
106 |
-
outputs=profile_output
|
107 |
-
)
|
108 |
-
|
109 |
-
gr.Markdown("---")
|
110 |
-
gr.Markdown("### π± Add Your Daily Status (free text!)")
|
111 |
-
today_log = gr.Textbox(label="What do you want to remember about today?", value=get_today_log())
|
112 |
-
save_log_btn = gr.Button("Save Today's Log")
|
113 |
-
log_output = gr.Textbox(label="", interactive=False)
|
114 |
-
|
115 |
-
save_log_btn.click(
|
116 |
-
fn=update_daily_log,
|
117 |
-
inputs=today_log,
|
118 |
-
outputs=log_output
|
119 |
-
)
|
120 |
-
|
121 |
-
gr.Markdown("#### π
Recent Logs")
|
122 |
-
recent_logs_box = gr.Markdown(recent_logs())
|
123 |
|
124 |
with gr.Tab("π¬ Ask About Sheetal"):
|
125 |
gr.Markdown("### π¬ Ask Anything About Sheetal")
|
126 |
user_q = gr.Textbox(label="Type your question here:")
|
127 |
ask_btn = gr.Button("Ask")
|
128 |
-
answer_box = gr.Textbox(label="Bot answer", interactive=False)
|
129 |
-
|
130 |
ask_btn.click(
|
131 |
fn=chatbot_qa,
|
132 |
inputs=user_q,
|
|
|
13 |
return json.load(f)
|
14 |
return default
|
15 |
|
|
|
|
|
|
|
|
|
16 |
profile = load_json(PROFILE_FILE, {})
|
17 |
daily = load_json(DAILY_FILE, {})
|
18 |
|
|
|
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,
|
|
|
37 |
)
|
38 |
|
39 |
llm = None
|
|
|
44 |
llm = get_llm()
|
45 |
context = build_context(profile, daily)
|
46 |
prompt = f"System: {context}\nUser: {user_q}\nAssistant:"
|
47 |
+
outputs = llm(prompt, max_new_tokens=128)
|
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,
|