Spaces:
Running on Zero
Running on Zero
File size: 7,630 Bytes
f7572a5 3e6ab4d 8e1f5bc 3e6ab4d b9db94b eab742e b9db94b 49dc69b fe43097 48fccfa f7572a5 3e6ab4d ad1140f 1db4430 8e1f5bc ad1140f 2f98dee 37f024b 2f98dee 37f024b ad1140f a77d0e8 1db4430 f1d5b31 1db4430 cd4d65a 1db4430 f7572a5 1db4430 45bab0c 2f98dee ad1140f 45bab0c ad1140f 2f98dee fe43097 1db4430 ad1140f 2f98dee 48fccfa ad1140f 2f98dee eab742e 49dc69b 1db4430 45bab0c f1d5b31 45bab0c f1d5b31 49dc69b f1d5b31 1db4430 f1d5b31 1db4430 f1d5b31 1db4430 f1d5b31 49dc69b 45bab0c 49dc69b 45bab0c 49dc69b 45bab0c 49dc69b 45bab0c 49dc69b 45bab0c 49dc69b 45bab0c 49dc69b f1d5b31 48fccfa fe43097 48fccfa f1d5b31 f7572a5 1db4430 45bab0c 1db4430 ad1140f 1db4430 eab742e ad1140f 1db4430 f1d5b31 eab742e f7572a5 1db4430 49dc69b f1d5b31 49dc69b 48fccfa 49dc69b f1d5b31 49dc69b f1d5b31 49dc69b f1d5b31 f7572a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | from __future__ import annotations
from typing import Any
import gradio as gr
from gradio.themes import Soft
from env.config import (
APP_DESCRIPTION,
APP_TITLE,
GITHUB_URL,
SPACE_URL,
)
from core.analyzer import (
add_user_message,
analyze_journal_ui,
chat_respond_ui,
reset_reflection_ui,
)
from ui.examples import render_examples
def get_theme() -> Any:
"""Returns the custom soft theme configured for dark slate violet styling."""
# Match the custom CSS with a dark violet Gradio theme.
theme = Soft(
primary_hue="violet",
secondary_hue="slate",
neutral_hue="slate",
)
return theme
def create_app() -> gr.Blocks:
"""Creates and lays out the Gradio interface for InnerSpace."""
with gr.Blocks(title=APP_TITLE) as demo:
# Store the analyzed journal for follow-up chat context.
journal_context_state = gr.State(value="")
# Header states the product purpose and guiding promise.
gr.Markdown(
f"# {APP_TITLE}\n{APP_DESCRIPTION}",
elem_id="nd-header",
)
gr.Markdown(
"Turn a journal entry into a grounded reframe, one tiny next step, and a calmer question to carry forward.",
elem_id="nd-kicker",
)
with gr.Row(elem_classes=["nd-main-grid"]):
# Left column collects journal text or file input.
with gr.Column(scale=1, elem_classes=["nd-input-panel"]):
gr.Markdown("## Journal Entry βοΈ")
notes_input = gr.Textbox(
label="Write your thoughts here...",
lines=8,
placeholder="Express your thoughts freely. What happened today? How are you feeling?",
elem_id="nd-journal-input",
)
distress_slider = gr.Slider(
minimum=1,
maximum=10,
value=5,
step=1,
label="Current distress level",
elem_classes=["nd-slider"],
)
with gr.Accordion("π Upload entry from file", open=False):
file_input = gr.File(
label="Upload text or markdown log (.txt, .md)",
file_types=[".txt", ".md"],
)
run_button = gr.Button(
"Analyze Thoughts",
variant="primary",
elem_classes=["nd-btn"],
)
# Right column displays the coaching chat panel.
with gr.Column(scale=1, elem_classes=["nd-output-panel"]):
gr.Markdown("## Mindful Coach π¨ββοΈ")
chatbot = gr.Chatbot(
label="Mindful CBT Coach",
elem_classes=["nd-chatbot"],
show_label=False,
)
with gr.Row(elem_classes=["nd-chat-row"]):
chat_input = gr.Textbox(
placeholder="Type your reply here...",
show_label=False,
lines=1,
max_lines=3,
scale=4,
elem_classes=["nd-chat-input"],
)
send_button = gr.Button(
"Send",
variant="secondary",
min_width=140,
elem_classes=["nd-send-btn"],
)
# Underneath both panels, display CBT Analysis report cards.
with gr.Column(elem_classes=["nd-analysis-section"]):
gr.Markdown("## Cognitive Analysis π§")
with gr.Row(elem_classes=["nd-card-grid"]):
sentiment_output = gr.Textbox(
label="Dominant Emotions π",
lines=5,
interactive=False,
elem_classes=["nd-output-card", "nd-emotions-card"],
)
areas_output = gr.Textbox(
label="Affected Life Areas π―",
lines=5,
interactive=False,
elem_classes=["nd-output-card", "nd-areas-card"],
)
distortions_output = gr.Textbox(
label="Cognitive Distortions π§ ",
lines=5,
interactive=False,
elem_classes=["nd-output-card", "nd-distortions-card"],
)
with gr.Row(elem_classes=["nd-card-grid"]):
reframe_output = gr.Textbox(
label="Balanced Reframe π¨βπ«",
lines=5,
interactive=False,
elem_classes=["nd-output-card", "nd-reframe-card"],
)
next_step_output = gr.Textbox(
label="Tiny Next Step π",
lines=5,
interactive=False,
elem_classes=["nd-output-card", "nd-next-step-card"],
)
# Example cards populate the form without running inference automatically.
render_examples(file_input, notes_input, distress_slider)
gr.Markdown(
f"[GitHub repo]({GITHUB_URL}) | [Hugging Face Space]({SPACE_URL})",
elem_id="nd-links",
)
# Diagnostics stay at the end and remain collapsed during normal use.
with gr.Accordion("βοΈ Diagnostics & System Execution Logs", open=False):
extracted_output = gr.Textbox(
label="Extracted Journal Text",
lines=3,
interactive=False,
elem_classes=["nd-log-box"],
)
model_output = gr.Textbox(
label="System execution logs",
lines=4,
interactive=False,
elem_classes=["nd-log-box"],
)
# Reset the coach before each new analysis run.
reset_event = run_button.click(
fn=reset_reflection_ui,
inputs=[],
outputs=[chatbot, chat_input, model_output],
)
# Analysis populates report cards, chat, and context state.
reset_event.then(
fn=analyze_journal_ui,
inputs=[file_input, notes_input, distress_slider],
outputs=[
extracted_output,
model_output,
sentiment_output,
areas_output,
distortions_output,
reframe_output,
next_step_output,
chatbot,
journal_context_state,
],
)
# Both enter key and button submit chat replies.
user_msg_event = chat_input.submit(
fn=add_user_message,
inputs=[chatbot, chat_input],
outputs=[chatbot, chat_input],
queue=False,
)
user_msg_event.then(
fn=chat_respond_ui,
inputs=[chatbot, journal_context_state],
outputs=[chatbot, model_output],
)
# Chat submission
send_event = send_button.click(
fn=add_user_message,
inputs=[chatbot, chat_input],
outputs=[chatbot, chat_input],
queue=False,
)
send_event.then(
fn=chat_respond_ui,
inputs=[chatbot, journal_context_state],
outputs=[chatbot, model_output],
)
return demo
|