Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,136 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from datetime import date
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
PROFILE_FILE = "about_me.json"
|
8 |
+
DAILY_FILE = "daily_status.json"
|
9 |
+
|
10 |
+
def load_json(path, default):
|
11 |
+
if os.path.exists(path):
|
12 |
+
with open(path) as f:
|
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 |
+
|
23 |
+
def build_context(profile, daily):
|
24 |
+
recent_days = sorted(daily.keys(), reverse=True)[:7]
|
25 |
+
daily_lines = "\n".join([f"{d}: {daily[d].get('log','')}" for d in recent_days])
|
26 |
+
context = (
|
27 |
+
f"Profile:\n{json.dumps(profile, indent=2)}\n"
|
28 |
+
f"Recent daily logs:\n{daily_lines}\n"
|
29 |
+
"You are a helpful assistant. Answer only using the provided information. "
|
30 |
+
"If you don't know the answer, reply: 'Sheetal hasn't shared that yet!'"
|
31 |
+
)
|
32 |
+
return context
|
33 |
+
|
34 |
+
def get_llm():
|
35 |
+
return pipeline(
|
36 |
+
"text-generation",
|
37 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
38 |
+
max_new_tokens=256,
|
39 |
+
do_sample=True,
|
40 |
+
temperature=0.7,
|
41 |
+
trust_remote_code=True,
|
42 |
+
)
|
43 |
+
|
44 |
+
llm = None
|
45 |
+
|
46 |
+
def chatbot_qa(user_q):
|
47 |
+
global llm
|
48 |
+
if llm is None:
|
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=256)
|
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,
|
133 |
+
outputs=answer_box
|
134 |
+
)
|
135 |
+
|
136 |
+
demo.launch()
|