Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -21,36 +21,28 @@ profile = load_json(PROFILE_FILE, {})
|
|
21 |
daily = load_json(DAILY_FILE, {})
|
22 |
|
23 |
def build_context(profile, daily):
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
f"
|
28 |
-
|
29 |
-
"
|
30 |
-
|
31 |
-
)
|
32 |
-
return context
|
33 |
-
|
34 |
-
def get_llm():
|
35 |
-
return pipeline(
|
36 |
-
"text-generation",
|
37 |
-
model="gpt2",
|
38 |
-
max_new_tokens=128,
|
39 |
-
do_sample=True,
|
40 |
-
temperature=0.7,
|
41 |
-
)
|
42 |
|
43 |
-
|
|
|
44 |
|
45 |
def chatbot_qa(user_q):
|
46 |
-
global llm
|
47 |
-
if llm is None:
|
48 |
-
llm = get_llm()
|
49 |
context = build_context(profile, daily)
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def save_profile_and_log(name, city, job, about, freeform):
|
56 |
today = str(date.today())
|
@@ -85,14 +77,17 @@ def check_password(password):
|
|
85 |
else:
|
86 |
return gr.update(visible=False), gr.update(value="β Access denied. Try again.", visible=True)
|
87 |
|
|
|
|
|
|
|
88 |
with gr.Blocks(title="Sheetal's Chatbot") as demo:
|
89 |
gr.Markdown("# πΈ Sheetal's Personal Chatbot")
|
90 |
gr.Markdown("Ask anything about Sheetal!")
|
91 |
|
92 |
with gr.Tab("π Admin (Sheetal)"):
|
93 |
admin_password = gr.Textbox(label="Enter admin password", type="password", max_lines=1)
|
94 |
-
password_status = gr.Textbox(label="Password status", value="", interactive=False, visible=False)
|
95 |
unlock_panel = gr.Button("Unlock Admin Panel")
|
|
|
96 |
|
97 |
with gr.Column(visible=False) as admin_panel:
|
98 |
with gr.Row():
|
@@ -109,6 +104,11 @@ with gr.Blocks(title="Sheetal's Chatbot") as demo:
|
|
109 |
inputs=[name, city, job, about, today_log],
|
110 |
outputs=[admin_status, logs_output]
|
111 |
)
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
unlock_panel.click(
|
114 |
fn=check_password,
|
|
|
21 |
daily = load_json(DAILY_FILE, {})
|
22 |
|
23 |
def build_context(profile, daily):
|
24 |
+
# Combine profile info and last 7 days logs
|
25 |
+
context = []
|
26 |
+
for k, v in profile.items():
|
27 |
+
context.append(f"{k.capitalize()}: {v}")
|
28 |
+
for d in sorted(daily.keys(), reverse=True)[:7]:
|
29 |
+
context.append(f"On {d}: {daily[d].get('log','')}")
|
30 |
+
return "\n".join(context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# QA Pipeline - loads once and is reused
|
33 |
+
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
34 |
|
35 |
def chatbot_qa(user_q):
|
|
|
|
|
|
|
36 |
context = build_context(profile, daily)
|
37 |
+
try:
|
38 |
+
result = qa_pipeline(question=user_q, context=context)
|
39 |
+
answer = result["answer"].strip()
|
40 |
+
# If the model cannot answer, it often returns '' or 'empty'
|
41 |
+
if not answer or answer.lower() in ["empty", ""]:
|
42 |
+
return "Sheetal hasn't shared that yet!"
|
43 |
+
return answer
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error: {e}"
|
46 |
|
47 |
def save_profile_and_log(name, city, job, about, freeform):
|
48 |
today = str(date.today())
|
|
|
77 |
else:
|
78 |
return gr.update(visible=False), gr.update(value="β Access denied. Try again.", visible=True)
|
79 |
|
80 |
+
def show_profile():
|
81 |
+
return json.dumps(profile, indent=2), json.dumps(daily, indent=2)
|
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 |
with gr.Tab("π Admin (Sheetal)"):
|
88 |
admin_password = gr.Textbox(label="Enter admin password", type="password", max_lines=1)
|
|
|
89 |
unlock_panel = gr.Button("Unlock Admin Panel")
|
90 |
+
password_status = gr.Textbox(label="Password status", value="", interactive=False, visible=False)
|
91 |
|
92 |
with gr.Column(visible=False) as admin_panel:
|
93 |
with gr.Row():
|
|
|
104 |
inputs=[name, city, job, about, today_log],
|
105 |
outputs=[admin_status, logs_output]
|
106 |
)
|
107 |
+
# Debug: show current data
|
108 |
+
show_btn = gr.Button("Show Current Data")
|
109 |
+
profile_out = gr.Textbox(label="Profile JSON")
|
110 |
+
daily_out = gr.Textbox(label="Daily JSON")
|
111 |
+
show_btn.click(fn=show_profile, outputs=[profile_out, daily_out])
|
112 |
|
113 |
unlock_panel.click(
|
114 |
fn=check_password,
|