Upload app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ from io import BytesIO
|
|
| 20 |
from reportlab.lib.pagesizes import letter
|
| 21 |
from reportlab.pdfgen import canvas
|
| 22 |
from reportlab.lib.utils import ImageReader
|
| 23 |
-
|
| 24 |
# ===============================
|
| 25 |
# 3️⃣ Groq Client
|
| 26 |
# ===============================
|
|
@@ -116,66 +116,79 @@ def add_daily_entry(user_id, stress, mood, sleep_hours):
|
|
| 116 |
def generate_weekly_report(user_id):
|
| 117 |
global df
|
| 118 |
df['date'] = pd.to_datetime(df['date'])
|
| 119 |
-
user_df = df[df['user_id']==user_id]
|
|
|
|
| 120 |
if user_df.empty:
|
| 121 |
return "No data available yet.", None, None
|
|
|
|
| 122 |
user_df['week'] = user_df['date'].dt.isocalendar().week
|
| 123 |
|
| 124 |
weekly_summary = user_df.groupby('week').agg({
|
| 125 |
-
"stress":["mean","max"],
|
| 126 |
-
"mood":["mean","min"],
|
| 127 |
-
"sleep_hours":["mean","min"]
|
| 128 |
})
|
|
|
|
| 129 |
weekly_summary['stress_change'] = weekly_summary['stress']['mean'].diff()
|
| 130 |
weekly_summary['mood_change'] = weekly_summary['mood']['mean'].diff()
|
| 131 |
weekly_summary['sleep_change'] = weekly_summary['sleep_hours']['mean'].diff()
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
fig, ax = plt.subplots(3,1,figsize=(8,10))
|
| 135 |
-
|
| 136 |
-
weekly_summary['
|
| 137 |
-
weekly_summary['
|
|
|
|
|
|
|
| 138 |
plt.tight_layout()
|
|
|
|
| 139 |
chart_buf = BytesIO()
|
| 140 |
plt.savefig(chart_buf, format="png")
|
|
|
|
| 141 |
chart_buf.seek(0)
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
trend_prompt = f"""
|
| 145 |
You are a wellness data analyst AI.
|
| 146 |
-
|
|
|
|
| 147 |
{weekly_summary.tail(4)}
|
| 148 |
|
| 149 |
-
Explain
|
| 150 |
"""
|
|
|
|
| 151 |
response = client.chat.completions.create(
|
| 152 |
model="llama-3.3-70b-versatile",
|
| 153 |
-
messages=[{"role":"user","content":trend_prompt}]
|
| 154 |
)
|
|
|
|
| 155 |
explanation = response.choices[0].message.content
|
| 156 |
|
| 157 |
-
#
|
| 158 |
pdf_buf = BytesIO()
|
| 159 |
c = canvas.Canvas(pdf_buf, pagesize=letter)
|
| 160 |
width, height = letter
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
c.drawString(
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
for line in explanation.split("\n"):
|
| 166 |
-
c.drawString(
|
| 167 |
-
y -=
|
| 168 |
if y < 100:
|
| 169 |
c.showPage()
|
| 170 |
y = height - 40
|
| 171 |
-
|
| 172 |
-
img = ImageReader(chart_buf)
|
| 173 |
c.showPage()
|
| 174 |
-
c.drawImage(
|
| 175 |
c.save()
|
| 176 |
pdf_buf.seek(0)
|
| 177 |
|
| 178 |
-
return explanation,
|
| 179 |
|
| 180 |
# ===============================
|
| 181 |
# 🔟 Gradio interface
|
|
|
|
| 20 |
from reportlab.lib.pagesizes import letter
|
| 21 |
from reportlab.pdfgen import canvas
|
| 22 |
from reportlab.lib.utils import ImageReader
|
| 23 |
+
from PIL import Image
|
| 24 |
# ===============================
|
| 25 |
# 3️⃣ Groq Client
|
| 26 |
# ===============================
|
|
|
|
| 116 |
def generate_weekly_report(user_id):
|
| 117 |
global df
|
| 118 |
df['date'] = pd.to_datetime(df['date'])
|
| 119 |
+
user_df = df[df['user_id'] == user_id]
|
| 120 |
+
|
| 121 |
if user_df.empty:
|
| 122 |
return "No data available yet.", None, None
|
| 123 |
+
|
| 124 |
user_df['week'] = user_df['date'].dt.isocalendar().week
|
| 125 |
|
| 126 |
weekly_summary = user_df.groupby('week').agg({
|
| 127 |
+
"stress": ["mean", "max"],
|
| 128 |
+
"mood": ["mean", "min"],
|
| 129 |
+
"sleep_hours": ["mean", "min"]
|
| 130 |
})
|
| 131 |
+
|
| 132 |
weekly_summary['stress_change'] = weekly_summary['stress']['mean'].diff()
|
| 133 |
weekly_summary['mood_change'] = weekly_summary['mood']['mean'].diff()
|
| 134 |
weekly_summary['sleep_change'] = weekly_summary['sleep_hours']['mean'].diff()
|
| 135 |
|
| 136 |
+
# ---- Create chart ----
|
| 137 |
+
fig, ax = plt.subplots(3, 1, figsize=(8, 10))
|
| 138 |
+
|
| 139 |
+
weekly_summary['stress']['mean'].plot(ax=ax[0], title="Weekly Avg Stress", marker="o")
|
| 140 |
+
weekly_summary['mood']['mean'].plot(ax=ax[1], title="Weekly Avg Mood", marker="o")
|
| 141 |
+
weekly_summary['sleep_hours']['mean'].plot(ax=ax[2], title="Weekly Avg Sleep Hours", marker="o")
|
| 142 |
+
|
| 143 |
plt.tight_layout()
|
| 144 |
+
|
| 145 |
chart_buf = BytesIO()
|
| 146 |
plt.savefig(chart_buf, format="png")
|
| 147 |
+
plt.close()
|
| 148 |
chart_buf.seek(0)
|
| 149 |
|
| 150 |
+
chart_image = Image.open(chart_buf)
|
| 151 |
+
|
| 152 |
+
# ---- LLaMA explanation ----
|
| 153 |
trend_prompt = f"""
|
| 154 |
You are a wellness data analyst AI.
|
| 155 |
+
|
| 156 |
+
Here is the weekly summary:
|
| 157 |
{weekly_summary.tail(4)}
|
| 158 |
|
| 159 |
+
Explain the trends in stress, mood, and sleep in simple, policymaker-friendly language.
|
| 160 |
"""
|
| 161 |
+
|
| 162 |
response = client.chat.completions.create(
|
| 163 |
model="llama-3.3-70b-versatile",
|
| 164 |
+
messages=[{"role": "user", "content": trend_prompt}]
|
| 165 |
)
|
| 166 |
+
|
| 167 |
explanation = response.choices[0].message.content
|
| 168 |
|
| 169 |
+
# ---- PDF generation ----
|
| 170 |
pdf_buf = BytesIO()
|
| 171 |
c = canvas.Canvas(pdf_buf, pagesize=letter)
|
| 172 |
width, height = letter
|
| 173 |
+
|
| 174 |
+
c.setFont("Helvetica-Bold", 14)
|
| 175 |
+
c.drawString(40, height - 40, "Weekly Mental Health Trend Report")
|
| 176 |
+
|
| 177 |
+
c.setFont("Helvetica", 11)
|
| 178 |
+
y = height - 80
|
| 179 |
for line in explanation.split("\n"):
|
| 180 |
+
c.drawString(40, y, line)
|
| 181 |
+
y -= 14
|
| 182 |
if y < 100:
|
| 183 |
c.showPage()
|
| 184 |
y = height - 40
|
| 185 |
+
|
|
|
|
| 186 |
c.showPage()
|
| 187 |
+
c.drawImage(ImageReader(chart_buf), 50, 200, width=500, height=400)
|
| 188 |
c.save()
|
| 189 |
pdf_buf.seek(0)
|
| 190 |
|
| 191 |
+
return explanation, chart_image, pdf_buf
|
| 192 |
|
| 193 |
# ===============================
|
| 194 |
# 🔟 Gradio interface
|