Upload app.py
Browse files
app.py
CHANGED
|
@@ -167,28 +167,13 @@ 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 |
-
width, height = letter
|
| 173 |
|
| 174 |
-
|
| 175 |
-
c.
|
| 176 |
|
| 177 |
-
|
| 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
|
|
@@ -216,13 +201,17 @@ with gr.Blocks() as demo:
|
|
| 216 |
|
| 217 |
with gr.Tab("Medical QA"):
|
| 218 |
gr.Markdown("Ask questions about stress, mood, sleep, or general wellness.")
|
| 219 |
-
chatbot = gr.Chatbot()
|
| 220 |
msg = gr.Textbox(label="Your Question")
|
| 221 |
clear = gr.Button("Clear Chat")
|
| 222 |
-
def respond(message,history):
|
| 223 |
answer = rag_answer(message)
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
msg.submit(respond,[msg,chatbot],[msg,chatbot])
|
| 227 |
clear.click(lambda: None,None,chatbot,queue=False)
|
| 228 |
|
|
|
|
| 167 |
explanation = response.choices[0].message.content
|
| 168 |
|
| 169 |
# ---- PDF generation ----
|
| 170 |
+
PDF_DIR = "reports"
|
| 171 |
+
os.makedirs(PDF_DIR, exist_ok=True)
|
|
|
|
| 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 |
|
| 178 |
# ===============================
|
| 179 |
# 🔟 Gradio interface
|
|
|
|
| 201 |
|
| 202 |
with gr.Tab("Medical QA"):
|
| 203 |
gr.Markdown("Ask questions about stress, mood, sleep, or general wellness.")
|
| 204 |
+
chatbot = gr.Chatbot(type="messages")
|
| 205 |
msg = gr.Textbox(label="Your Question")
|
| 206 |
clear = gr.Button("Clear Chat")
|
| 207 |
+
def respond(message, history):
|
| 208 |
answer = rag_answer(message)
|
| 209 |
+
|
| 210 |
+
history = history or []
|
| 211 |
+
history.append({"role": "user", "content": message})
|
| 212 |
+
history.append({"role": "assistant", "content": answer})
|
| 213 |
+
|
| 214 |
+
return "", history
|
| 215 |
msg.submit(respond,[msg,chatbot],[msg,chatbot])
|
| 216 |
clear.click(lambda: None,None,chatbot,queue=False)
|
| 217 |
|