Spaces:
Runtime error
Runtime error
| 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( | |
| """ | |
| <h1 style="text-align:center; color:#4f46e5;">π AI Mood Tracker</h1> | |
| <p style="text-align:center; font-size:18px;">Track your moods, get AI emotional insights, and visualize your day!</p> | |
| """ | |
| ) | |
| 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() | |