import gradio as gr import datetime import matplotlib.pyplot as plt from openai import OpenAI import io from PIL import Image # -------- GROQ API Client -------- client = OpenAI( api_key="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", # <-- replace with your key base_url="https://api.groq.com/openai/v1", ) # -------- Mood Log -------- mood_log = [] def log_mood(mood, description): now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') mood_log.append({ "datetime": now, "mood": mood, "description": description }) return f"✔ Logged: {now} - {mood}" def show_entries(): if not mood_log: return "No entries yet today." text = "" for entry in mood_log: text += f"{entry['datetime']} — {entry['mood']} — {entry['description']}\n" return text def analyze_mood(): if not mood_log: return "Please log at least one mood first." prompt = """ You are an emotional AI assistant. Analyze the user's mood logs: - Rate each mood from 1–10 (1 = very negative, 10 = very positive) - Analyze emotional tone - Identify patterns like stress, fatigue, sadness, happiness, or anxiety - Provide an overall summary and advice Mood entries: """ for entry in mood_log: prompt += f"- {entry['datetime']}: {entry['mood']} — {entry['description']}\n" prompt += "\nProvide a detailed analysis:" try: response = client.responses.create( model="llama-3.3-70b-versatile", input=prompt, temperature=0.7, top_p=0.9 ) return response.output_text except Exception as e: return f"API Error: {e}" def mood_chart(): if not mood_log: return Image.new("RGB", (600, 400), color="white") moods = [entry["mood"] for entry in mood_log] counts = {} for m in moods: counts[m] = counts.get(m, 0) + 1 plt.figure(figsize=(6,4)) plt.bar(counts.keys(), counts.values(), color="#4f46e5") plt.title("Mood Trend Today", fontsize=16) plt.xlabel("Mood") plt.ylabel("Count") buf = io.BytesIO() plt.savefig(buf, format="png", bbox_inches='tight') buf.seek(0) plt.close() return Image.open(buf) # -------- Gradio Gen-AI Style UI -------- with gr.Blocks(theme=gr.themes.Base()) as app: gr.Markdown( """

🌈 AI Mood Tracker

Track your moods, get AI emotional insights, and visualize your day!

""" ) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 📥 Log Your Mood") mood = gr.Dropdown( ["😊 Happy","😔 Sad","😩 Tired","😰 Anxious","😡 Angry","😐 Neutral"], label="Select Mood" ) description = gr.Textbox( label="Describe your feeling", placeholder="Write a few words about how you feel...", lines=4 ) log_button = gr.Button("Log Mood") log_output = gr.Textbox(label="Status", lines=2) entries_button = gr.Button("Show Entries") entries_output = gr.Textbox(label="Today's Logs", lines=8) with gr.Column(scale=1): gr.Markdown("### 🤖 AI Mood Analysis") analyze_button = gr.Button("Analyze My Mood") analyze_output = gr.Textbox( label="AI Analysis", placeholder="Your mood analysis will appear here...", lines=20, interactive=False ) gr.Markdown("### 📊 Mood Trend Chart") chart_button = gr.Button("Show Chart") chart_output = gr.Image(label="Mood Chart") log_button.click(log_mood, [mood, description], log_output) entries_button.click(show_entries, None, entries_output) analyze_button.click(analyze_mood, None, analyze_output) chart_button.click(mood_chart, None, chart_output) app.launch()