File size: 7,965 Bytes
22e78a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a568f
9f4b1e8
 
 
 
22e78a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a568f
 
22e78a2
 
c1a568f
22e78a2
 
 
c1a568f
 
 
22e78a2
c1a568f
22e78a2
 
 
 
c1a568f
 
 
 
 
 
 
22e78a2
c1a568f
22e78a2
 
c1a568f
22e78a2
 
c1a568f
 
 
22e78a2
 
c1a568f
 
22e78a2
 
c1a568f
22e78a2
c1a568f
22e78a2
 
c1a568f
22e78a2
c1a568f
22e78a2
 
c1a568f
9f4b1e8
 
 
 
c1a568f
c5eac77
9f4b1e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a568f
c5eac77
22e78a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0af5fb3
ba7ceaf
22e78a2
 
c5eac77
0af5fb3
53f3bf5
c62b9ee
0af5fb3
c62b9ee
 
 
 
 
 
 
 
 
 
c5eac77
0af5fb3
 
 
22e78a2
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# ===============================
# 1️⃣ Install dependencies (only in Colab, HF Space installs from requirements.txt)
# ===============================
# !pip install -q groq datasets sentence-transformers faiss-cpu gradio matplotlib pandas tqdm reportlab

# ===============================
# 2️⃣ Imports
# ===============================
import os
import faiss
import numpy as np
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
from groq import Groq
import datetime
from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from PIL import Image

REPORTS_DIR = "reports"
os.makedirs(REPORTS_DIR, exist_ok=True)

# ===============================
# 3️⃣ Groq Client
# ===============================
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

# ===============================
# 4️⃣ Load datasets for RAG
# ===============================
medical_ds = load_dataset("lavita/medical-qa-datasets", "all-processed", split="train[:1000]")
stress_ds = load_dataset("Amod/mental_health_counseling_conversations", split="train[:500]")

# ===============================
# 5️⃣ Prepare documents
# ===============================
documents = []
for row in medical_ds:
    instr = row.get("instruction","") or ""
    inp = row.get("input","") or ""
    out = row.get("output","") or ""
    text = instr.strip()
    if inp.strip(): text += " " + inp.strip()
    text += " " + out.strip()
    documents.append(text)
for row in stress_ds:
    context = row.get("Context","") or ""
    response = row.get("Response","") or ""
    documents.append(context + " " + response)

# ===============================
# 6️⃣ Embeddings + FAISS
# ===============================
embedder = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = embedder.encode(documents, convert_to_numpy=True, show_progress_bar=True)
dimension = embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(embeddings)

# ===============================
# 7️⃣ RAG functions
# ===============================
def retrieve_docs(query,k=5):
    query_embedding = embedder.encode([query])
    distances, indices = index.search(query_embedding,k)
    return [documents[i] for i in indices[0]]

def rag_answer(query):
    retrieved = retrieve_docs(query)
    context = "\n\n".join(retrieved)
    prompt = f"""
You are a medical assistant.
Use ONLY the context below to answer.
Do NOT diagnose anyone.
Provide supportive and informative responses.

Context:
{context}

Question:
{query}
"""
    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[{"role":"user","content":prompt}],
    )
    return response.choices[0].message.content

# ===============================
# 8️⃣ CSV persistence
# ===============================
CSV_FILE = "daily_entries.csv"
if os.path.exists(CSV_FILE):
    df = pd.read_csv(CSV_FILE, parse_dates=["date"])
else:
    df = pd.DataFrame(columns=["date","user_id","stress","mood","sleep_hours"])

def add_daily_entry(user_id, stress, mood, sleep_hours):
    global df
    today = datetime.date.today()
    new_row = pd.DataFrame([{
        "date": today,
        "user_id": user_id,
        "stress": stress,
        "mood": mood,
        "sleep_hours": sleep_hours
    }])
    df = pd.concat([df,new_row], ignore_index=True)
    df.to_csv(CSV_FILE,index=False)
    return f"Entry for {today} saved!"

# ===============================
# 9️⃣ Weekly report + LLaMA + chart
# ===============================
def generate_weekly_report(user_id):
    global df
    df['date'] = pd.to_datetime(df['date'])
    user_df = df[df['user_id'] == user_id]

    if user_df.empty:
        return "No data available yet.", None, None

    user_df['week'] = user_df['date'].dt.isocalendar().week

    weekly_summary = user_df.groupby('week').agg({
        "stress": ["mean", "max"],
        "mood": ["mean", "min"],
        "sleep_hours": ["mean", "min"]
    })

    weekly_summary['stress_change'] = weekly_summary['stress']['mean'].diff()
    weekly_summary['mood_change'] = weekly_summary['mood']['mean'].diff()
    weekly_summary['sleep_change'] = weekly_summary['sleep_hours']['mean'].diff()

    # ---- Create chart ----
    fig, ax = plt.subplots(3, 1, figsize=(8, 10))

    weekly_summary['stress']['mean'].plot(ax=ax[0], title="Weekly Avg Stress", marker="o")
    weekly_summary['mood']['mean'].plot(ax=ax[1], title="Weekly Avg Mood", marker="o")
    weekly_summary['sleep_hours']['mean'].plot(ax=ax[2], title="Weekly Avg Sleep Hours", marker="o")

    plt.tight_layout()

    chart_buf = BytesIO()
    plt.savefig(chart_buf, format="png")
    plt.close()
    chart_buf.seek(0)

    chart_image = Image.open(chart_buf)

    # ---- LLaMA explanation ----
    trend_prompt = f"""
You are a wellness data analyst AI.

Here is the weekly summary:
{weekly_summary.tail(4)}

Explain the trends in stress, mood, and sleep in simple, policymaker-friendly language.
"""

    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[{"role": "user", "content": trend_prompt}]
    )

    explanation = response.choices[0].message.content

    # ---- PDF generation ----
    import time

    timestamp = int(time.time())
    pdf_path = f"{REPORTS_DIR}/weekly_report_user_{user_id}_{timestamp}.pdf"

    c = canvas.Canvas(pdf_path, pagesize=letter)
    width, height = letter

    c.setFont("Helvetica-Bold", 14)
    c.drawString(40, height - 40, "Weekly Mental Health Trend Report")

    c.setFont("Helvetica", 11)
    y = height - 80
    for line in explanation.split("\n"):
        c.drawString(40, y, line)
        y -= 14
        if y < 100:
            c.showPage()
            y = height - 40

    c.showPage()
    c.drawImage(ImageReader(chart_buf), 50, 200, width=500, height=400)
    c.save()

    return explanation, chart_image, pdf_path

# ===============================
# 🔟 Gradio interface
# ===============================
with gr.Blocks() as demo:
    gr.Markdown("# 🧠 Medical & Stress RAG Assistant with Persistent Reports and PDF Export")

    with gr.Tab("Daily Entry"):
        gr.Markdown("Enter daily stress, mood, and sleep hours.")
        stress = gr.Slider(0,10,label="Stress Level")
        mood = gr.Slider(0,10,label="Mood Level")
        sleep = gr.Number(label="Sleep Hours")
        submit = gr.Button("Save Entry")
        output_entry = gr.Textbox(label="Status")
        submit.click(add_daily_entry,[gr.Number(value=1,label="User ID"),stress,mood,sleep],output_entry)

    with gr.Tab("Weekly Trend Report"):
        gr.Markdown("View weekly summary, trends, and export PDF.")
        user_id_input = gr.Number(value=1,label="User ID")
        report_output = gr.Textbox(label="Weekly Trend Explanation")
        chart_output = gr.Image(label="Trend Chart")
        pdf_output = gr.File(label="Download PDF")
        generate = gr.Button("Generate Report")
        generate.click(generate_weekly_report,[user_id_input],[report_output,chart_output,pdf_output])

    with gr.Tab("Medical QA"):
        gr.Markdown("Ask questions about stress, mood, sleep, or general wellness.")

        chatbot = gr.Chatbot(label="Medical QA")
        msg = gr.Textbox(label="Your Question")
        clear = gr.Button("Clear Chat")

        def respond(message, history):
            history = history or []

            answer = rag_answer(message)

            history.append({
                "role": "user",
                "content": message
            })
            history.append({
                "role": "assistant",
                "content": answer
            })

            return "", history

        msg.submit(respond, [msg, chatbot], [msg, chatbot])
        clear.click(lambda: [], None, chatbot)

demo.launch()