Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer | |
import random | |
from datetime import datetime | |
# Initialize models (same as before) | |
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") | |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name) | |
text_generator = pipeline( | |
"text-generation", | |
model=model, | |
tokenizer=tokenizer, | |
max_new_tokens=50, | |
temperature=0.7, | |
top_p=0.9, | |
pad_token_id=tokenizer.eos_token_id | |
) | |
# JournalCompanion class remains the same as in previous example | |
# ... (insert the entire JournalCompanion class here) | |
def create_journal_interface(): | |
journal = JournalCompanion() | |
# Custom CSS for better styling | |
custom_css = """ | |
/* Global styles */ | |
.container { | |
max-width: 1200px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
/* Header styles */ | |
.header { | |
text-align: center; | |
margin-bottom: 2rem; | |
background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%); | |
padding: 2rem; | |
border-radius: 15px; | |
color: white; | |
} | |
/* Input area styles */ | |
.input-container { | |
background: white; | |
border-radius: 15px; | |
padding: 20px; | |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
margin-bottom: 20px; | |
} | |
/* Output area styles */ | |
.output-container { | |
background: #f8f9fa; | |
border-radius: 15px; | |
padding: 20px; | |
margin-top: 20px; | |
} | |
/* Button styles */ | |
.custom-button { | |
background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%); | |
border: none; | |
padding: 10px 20px; | |
border-radius: 8px; | |
color: white; | |
font-weight: bold; | |
cursor: pointer; | |
transition: transform 0.2s; | |
} | |
.custom-button:hover { | |
transform: translateY(-2px); | |
} | |
/* Card styles */ | |
.card { | |
background: white; | |
border-radius: 10px; | |
padding: 15px; | |
margin: 10px 0; | |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); | |
transition: transform 0.2s; | |
} | |
.card:hover { | |
transform: translateY(-2px); | |
} | |
/* Animation for results */ | |
@keyframes fadeIn { | |
from { opacity: 0; transform: translateY(10px); } | |
to { opacity: 1; transform: translateY(0); } | |
} | |
.result-animation { | |
animation: fadeIn 0.5s ease-out; | |
} | |
/* Responsive design */ | |
@media (max-width: 768px) { | |
.container { | |
padding: 10px; | |
} | |
.header { | |
padding: 1rem; | |
} | |
} | |
""" | |
with gr.Blocks(css=custom_css, title="AI Journal Companion") as interface: | |
with gr.Column(elem_classes="container"): | |
# Header | |
with gr.Column(elem_classes="header"): | |
gr.Markdown("# π AI Journal Companion") | |
gr.Markdown( | |
"Transform your thoughts into insights with AI-powered journaling", | |
elem_classes="subtitle" | |
) | |
# Main content | |
with gr.Row(): | |
# Input Column | |
with gr.Column(scale=1, elem_classes="input-container"): | |
entry_input = gr.Textbox( | |
label="Write Your Thoughts", | |
placeholder="Share what's on your mind...", | |
lines=8, | |
elem_classes="journal-input" | |
) | |
submit_btn = gr.Button( | |
"β¨ Analyze Entry", | |
variant="primary", | |
elem_classes="custom-button" | |
) | |
# Output Column | |
with gr.Column(scale=1, elem_classes="output-container"): | |
with gr.Column(elem_classes="card result-animation"): | |
result_message = gr.Markdown(label="Analysis") | |
sentiment_output = gr.Textbox( | |
label="Emotional Tone", | |
elem_classes="sentiment-output" | |
) | |
with gr.Column(elem_classes="card result-animation"): | |
prompt_output = gr.Markdown( | |
label="Reflection Prompts", | |
elem_classes="prompts-output" | |
) | |
with gr.Column(elem_classes="card result-animation"): | |
affirmation_output = gr.Textbox( | |
label="Your Daily Affirmation", | |
elem_classes="affirmation-output" | |
) | |
# Insights Section | |
with gr.Row(elem_classes="insights-section"): | |
with gr.Column(scale=1): | |
insights_btn = gr.Button( | |
"π View Monthly Insights", | |
elem_classes="custom-button" | |
) | |
insights_output = gr.Markdown( | |
elem_classes="card insights-card" | |
) | |
# Event handlers | |
submit_btn.click( | |
fn=journal.analyze_entry, | |
inputs=[entry_input], | |
outputs=[ | |
result_message, | |
sentiment_output, | |
prompt_output, | |
affirmation_output | |
] | |
) | |
insights_btn.click( | |
fn=journal.get_monthly_insights, | |
inputs=[], | |
outputs=[insights_output] | |
) | |
return interface | |
if __name__ == "__main__": | |
interface = create_journal_interface() | |
interface.launch() |