import gradio as gr import pandas as pd import plotly.express as px from groq import Groq import os # ---------------- CONFIG ---------------- client = Groq(api_key=os.getenv("GROQ_API_KEY")) # ---------------- CORE FUNCTION ---------------- def analyze_health_data(file): if file is None: return "Please upload a CSV file.", None, None # Read CSV file try: df = pd.read_csv(file.name) except Exception as e: return f"Error reading CSV: {e}", None, None # Drop completely empty columns df = df.dropna(axis=1, how="all") # Detect numeric columns for analysis numeric_cols = df.select_dtypes(include=["number"]).columns.tolist() if len(numeric_cols) < 2: return "Please upload a dataset with at least two numeric columns.", None, None # Try to find a date/time column date_cols = [c for c in df.columns if "date" in c.lower() or "time" in c.lower()] x_col = date_cols[0] if date_cols else df.columns[0] # Convert potential date column try: df[x_col] = pd.to_datetime(df[x_col], errors="ignore") except Exception: pass # ---------------- VISUALIZATIONS ---------------- # First chart — all numeric columns line plot fig1 = px.line( df, x=x_col, y=numeric_cols, title="Health Metrics Over Time", markers=True, labels={"value": "Metric Value", "variable": "Health Metric"}, ) # Second chart — correlation heatmap (if >2 numeric cols) if len(numeric_cols) >= 2: corr = df[numeric_cols].corr() fig2 = px.imshow( corr, text_auto=True, title="Correlation Between Health Metrics", color_continuous_scale="Blues", ) else: fig2 = None # ---------------- SUMMARY STATS ---------------- summary = df.describe(include="all").to_string() # ---------------- GENERATE AI INSIGHTS ---------------- prompt = f""" You are an expert digital health assistant. The user uploaded the following dataset summary: {summary} Identify 3 interesting insights or patterns (e.g., trends, correlations), and 2 practical recommendations that could help improve their health behavior, based solely on the numerical data provided. """ try: response = client.chat.completions.create( model="deepseek-r1-distill-llama-70b", messages=[{"role": "user", "content": prompt}], temperature=0.7, ) insights = response.choices[0].message.content.strip() except Exception as e: insights = f"Error generating AI insights: {e}" return insights, fig1, fig2 # ---------------- GRADIO INTERFACE ---------------- title = "🏥 HealthMind AI — Universal Health Data Insight Dashboard" desc = ( "Upload any health-related CSV file. " "The app will automatically detect numeric columns, visualize trends, " "and generate AI-driven insights about your data." ) demo = gr.Interface( fn=analyze_health_data, inputs=[gr.File(label="Upload Health Data (CSV)", file_types=[".csv"])], outputs=[ gr.Textbox(label="AI Health Insights", lines=10), gr.Plot(label="Metric Trends"), gr.Plot(label="Correlation Heatmap"), ], title=title, description=desc ) if __name__ == "__main__": demo.launch()