Upload app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,10 @@ 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 |
# ===============================
|
|
@@ -167,11 +171,29 @@ Explain the trends in stress, mood, and sleep in simple, policymaker-friendly la
|
|
| 167 |
explanation = response.choices[0].message.content
|
| 168 |
|
| 169 |
# ---- PDF generation ----
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
pdf_path = f"{PDF_DIR}/weekly_report_user_{user_id}.pdf"
|
| 174 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
return explanation, chart_image, pdf_path
|
| 177 |
|
|
@@ -202,14 +224,19 @@ with gr.Blocks() as demo:
|
|
| 202 |
with gr.Tab("Medical QA"):
|
| 203 |
gr.Markdown("Ask questions about stress, mood, sleep, or general wellness.")
|
| 204 |
|
| 205 |
-
chatbot = gr.Chatbot()
|
| 206 |
msg = gr.Textbox(label="Your Question")
|
| 207 |
clear = gr.Button("Clear Chat")
|
| 208 |
|
| 209 |
def respond(message, history):
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
answer = rag_answer(message)
|
|
|
|
|
|
|
| 212 |
history.append((message, answer))
|
|
|
|
| 213 |
return "", history
|
| 214 |
|
| 215 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
| 21 |
from reportlab.pdfgen import canvas
|
| 22 |
from reportlab.lib.utils import ImageReader
|
| 23 |
from PIL import Image
|
| 24 |
+
|
| 25 |
+
REPORTS_DIR = "reports"
|
| 26 |
+
os.makedirs(REPORTS_DIR, exist_ok=True)
|
| 27 |
+
|
| 28 |
# ===============================
|
| 29 |
# 3️⃣ Groq Client
|
| 30 |
# ===============================
|
|
|
|
| 171 |
explanation = response.choices[0].message.content
|
| 172 |
|
| 173 |
# ---- PDF generation ----
|
| 174 |
+
import time
|
| 175 |
+
|
| 176 |
+
timestamp = int(time.time())
|
| 177 |
+
pdf_path = f"{REPORTS_DIR}/weekly_report_user_{user_id}_{timestamp}.pdf"
|
| 178 |
|
|
|
|
| 179 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 180 |
+
width, height = letter
|
| 181 |
+
|
| 182 |
+
c.setFont("Helvetica-Bold", 14)
|
| 183 |
+
c.drawString(40, height - 40, "Weekly Mental Health Trend Report")
|
| 184 |
+
|
| 185 |
+
c.setFont("Helvetica", 11)
|
| 186 |
+
y = height - 80
|
| 187 |
+
for line in explanation.split("\n"):
|
| 188 |
+
c.drawString(40, y, line)
|
| 189 |
+
y -= 14
|
| 190 |
+
if y < 100:
|
| 191 |
+
c.showPage()
|
| 192 |
+
y = height - 40
|
| 193 |
+
|
| 194 |
+
c.showPage()
|
| 195 |
+
c.drawImage(ImageReader(chart_buf), 50, 200, width=500, height=400)
|
| 196 |
+
c.save()
|
| 197 |
|
| 198 |
return explanation, chart_image, pdf_path
|
| 199 |
|
|
|
|
| 224 |
with gr.Tab("Medical QA"):
|
| 225 |
gr.Markdown("Ask questions about stress, mood, sleep, or general wellness.")
|
| 226 |
|
| 227 |
+
chatbot = gr.Chatbot(label="Medical QA")
|
| 228 |
msg = gr.Textbox(label="Your Question")
|
| 229 |
clear = gr.Button("Clear Chat")
|
| 230 |
|
| 231 |
def respond(message, history):
|
| 232 |
+
if history is None:
|
| 233 |
+
history = []
|
| 234 |
+
|
| 235 |
answer = rag_answer(message)
|
| 236 |
+
|
| 237 |
+
# MUST be tuple format
|
| 238 |
history.append((message, answer))
|
| 239 |
+
|
| 240 |
return "", history
|
| 241 |
|
| 242 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|