File size: 1,988 Bytes
c5edfc1
a4de076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c770626
 
a4de076
 
 
 
 
 
 
 
 
 
 
 
c770626
a4de076
 
 
 
 
c770626
a4de076
 
 
 
 
c770626
a4de076
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
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
65
66
67
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

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="google/flan-t5-small",  # Fast!
        max_new_tokens=128,
        do_sample=True,
        temperature=0.7,
    )

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=128)
    answer = outputs[0]["generated_text"].split("Assistant:")[-1].strip()
    return answer

with gr.Blocks(title="Sheetal's Personal Chatbot") as demo:
    gr.Markdown("# 🌸 Sheetal's Personal Chatbot")
    gr.Markdown("Ask anything about Sheetal!")

    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, lines=2, max_lines=4)
        ask_btn.click(
            fn=chatbot_qa,
            inputs=user_q,
            outputs=answer_box
        )

demo.launch()