--- title: P3 Myeloma Follow-up Chatbot emoji: 🩺 colorFrom: red colorTo: gray sdk: gradio sdk_version: 5.49.1 app_file: app.py pinned: false --- # P3 Myeloma – AI Follow-up Chatbot An adaptive follow-up chatbot for the AI-Enhanced Personalized Patient Profile (P3) Myeloma intake. The participant answers the 16 Shared Decision Making questions, and the app uses Google Gemini to ask tailored follow-up questions, gently probe apparent contradictions in their preferences, and end with a priorities summary they can bring to their care team. It does **not** give medical advice, diagnoses, or treatment recommendations. It only helps the patient articulate their own values and concerns. ## Setup on Hugging Face Spaces 1. Create a new Space, choose **Gradio** as the SDK. 2. Upload `app.py`, `knowledge_base.py`, `supabase_store.py`, `Archive_2.zip`, `requirements.txt`, `packages.txt`, and this `README.md`. 3. Add your Gemini key as a secret: - Go to **Settings → Variables and secrets → New secret** - Name: `GEMINI_API_KEY` - Value: your key from https://aistudio.google.com/apikey 4. Add the Supabase secrets described below. 5. The Space will build and launch automatically. ## Saving and resuming sessions (Supabase) Participants sign in with a **User ID** and an **email address**. Everything they do is saved against that email, so closing the page and returning later — even on another device — picks up exactly where they left off. The User ID is checked on every return visit, so entering someone else's email does not open their session. ### One-time setup 1. **Create the tables.** In Supabase, open **SQL Editor → New query**, paste the whole of `supabase_schema.sql`, and run it. It is idempotent, so re-running it after an update is safe. 2. **Copy your credentials.** In Supabase, go to **Project Settings → API** and copy the **Project URL** and the **`service_role`** key. 3. **Add them as Space secrets** (Settings → Variables and secrets): | Name | Value | |---|---| | `SUPABASE_URL` | `https://YOUR-PROJECT-REF.supabase.co` | | `SUPABASE_KEY` | your **service_role** key | Use the `service_role` key, not the `anon` key. Row Level Security is enabled with no policies, so the `anon` key can read and write nothing — which is the point. Gradio runs your Python on the server, so the key is never sent to a browser. 4. **Check it.** Locally, or in the Space terminal: ```bash SUPABASE_URL="https://YOUR-PROJECT-REF.supabase.co" SUPABASE_KEY="YOUR-SERVICE-ROLE-KEY" python3 verify_supabase.py ``` It writes a throwaway row, reads it back, and deletes it. It never touches real participant data. If the Supabase secrets are missing or the service is unreachable, the chatbot still runs — it just tells the participant that this visit will not be saved, and it will not overwrite anything already stored for them. ### What gets stored `p3_sessions` — one resumable row per email: the participant's User ID, the 16 answers, the conversation they see, the full model-facing conversation, and the follow-up counter. `p3_messages` — an append-only log of every turn. Session rows are replaced when a participant starts over; this log never is, so no transcript is lost. For a quick look at what has been collected, open the `p3_session_overview` view in the Table Editor. ### What participants see - They are asked for their **User ID first, then their email address**, before anything else. Both are stored in Supabase. - Their User ID is shown in the **top right of the page** for as long as they are signed in, so they can note it for next time. - If a returning participant enters the wrong User ID for an email, they are refused. The **second** wrong attempt tells them the User ID is displayed at the top right of the page. - The conversation **opens with all 16 questions and their own answers**, so the profile the follow-up is built on is visible without leaving the chat. - The questionnaire auto-saves as it is filled in, so a half-finished intake survives a closed tab. - **Start Over** first shows a warning that it erases the current chat history and starts a new session, and only erases anything once confirmed. The transcript log in `p3_messages` is kept either way. **Use a different email** just signs out; nothing is deleted. - If the same session is open in two places, the older one is told to reload rather than being allowed to overwrite the newer conversation. ### Changing a participant's User ID ```sql update public.p3_sessions set user_id = 'P3-NEW' where email = 'someone@example.com'; ``` A row created before User IDs existed has none stored; the first sign-in after that adopts whatever User ID is entered, and it is enforced from then on. ### Deleting a participant's data ```sql delete from public.p3_sessions where email = 'someone@example.com'; delete from public.p3_messages where email = 'someone@example.com'; ``` ## Model Uses `gemini-2.5-flash`. To change it, edit the `MODEL` variable in `app.py`. ## How it works - The 16 yes/no answers are turned into a structured profile. - A rule-based `detect_tensions()` step flags likely contradictions (for example, wanting an aggressive approach while prioritizing quality of life, or living alone with no caregiver but preferring home medications). - Those flags are passed to Gemini, which asks one follow-up question per turn. - After enough exchanges (capped at 8), the model returns a `<>` of the patient's priorities, which the app formats and displays. ## Disclaimer This tool is for informational and preparation purposes only and is not a substitute for professional medical advice. Always consult a qualified healthcare provider.