sheetal-chatbot / app.py
shee2205's picture
Update app.py
a4de076 verified
raw
history blame
4.14 kB
import gradio as gr
import json
import os
from datetime import date
from transformers import pipeline
PROFILE_FILE = "about_me.json"
DAILY_FILE = "daily_status.json"
def load_json(path, default):
if os.path.exists(path):
with open(path) as f:
return json.load(f)
return default
def save_json(path, data):
with open(path, "w") as f:
json.dump(data, f, indent=2)
profile = load_json(PROFILE_FILE, {})
daily = load_json(DAILY_FILE, {})
def build_context(profile, daily):
recent_days = sorted(daily.keys(), reverse=True)[:7]
daily_lines = "\n".join([f"{d}: {daily[d].get('log','')}" for d in recent_days])
context = (
f"Profile:\n{json.dumps(profile, indent=2)}\n"
f"Recent daily logs:\n{daily_lines}\n"
"You are a helpful assistant. Answer only using the provided information. "
"If you don't know the answer, reply: 'Sheetal hasn't shared that yet!'"
)
return context
def get_llm():
return pipeline(
"text-generation",
model="HuggingFaceH4/zephyr-7b-beta",
max_new_tokens=256,
do_sample=True,
temperature=0.7,
trust_remote_code=True,
)
llm = None
def chatbot_qa(user_q):
global llm
if llm is None:
llm = get_llm()
context = build_context(profile, daily)
prompt = f"System: {context}\nUser: {user_q}\nAssistant:"
outputs = llm(prompt, max_new_tokens=256)
answer = outputs[0]["generated_text"].split("Assistant:")[-1].strip()
return answer
def update_profile(name, city, job, about):
profile.update({
"name": name,
"city": city,
"job": job,
"about": about
})
save_json(PROFILE_FILE, profile)
return "Profile updated!"
def update_daily_log(freeform):
today = str(date.today())
daily[today] = {"log": freeform}
save_json(DAILY_FILE, daily)
return f"Today's log saved! ({today})"
def get_profile_defaults():
return (
profile.get("name", "Sheetal"),
profile.get("city", ""),
profile.get("job", ""),
profile.get("about", "")
)
def get_today_log():
today = str(date.today())
return daily.get(today, {}).get("log", "")
def recent_logs():
logs = ""
for d in sorted(daily.keys(), reverse=True)[:5]:
logs += f"**{d}**: {daily[d]['log']}\n"
return logs
with gr.Blocks(title="Sheetal's Personal Chatbot") as demo:
gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
with gr.Tab("πŸ“ Admin (Sheetal)"):
gr.Markdown("### Edit Your Profile")
with gr.Row():
name = gr.Textbox(label="Name", value=profile.get("name", "Sheetal"))
city = gr.Textbox(label="City", value=profile.get("city", ""))
job = gr.Textbox(label="Profession", value=profile.get("job", ""))
about = gr.Textbox(label="A few lines about you", value=profile.get("about", ""))
save_profile_btn = gr.Button("Save Profile")
profile_output = gr.Textbox(label="", interactive=False)
save_profile_btn.click(
fn=update_profile,
inputs=[name, city, job, about],
outputs=profile_output
)
gr.Markdown("---")
gr.Markdown("### 🌱 Add Your Daily Status (free text!)")
today_log = gr.Textbox(label="What do you want to remember about today?", value=get_today_log())
save_log_btn = gr.Button("Save Today's Log")
log_output = gr.Textbox(label="", interactive=False)
save_log_btn.click(
fn=update_daily_log,
inputs=today_log,
outputs=log_output
)
gr.Markdown("#### πŸ“… Recent Logs")
recent_logs_box = gr.Markdown(recent_logs())
with gr.Tab("πŸ’¬ Ask About Sheetal"):
gr.Markdown("### πŸ’¬ Ask Anything About Sheetal")
user_q = gr.Textbox(label="Type your question here:")
ask_btn = gr.Button("Ask")
answer_box = gr.Textbox(label="Bot answer", interactive=False)
ask_btn.click(
fn=chatbot_qa,
inputs=user_q,
outputs=answer_box
)
demo.launch()