File size: 10,731 Bytes
34648ae
0f2a79c
 
34648ae
0f2a79c
34648ae
0f2a79c
 
 
 
 
 
34648ae
 
 
 
0f2a79c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34648ae
0f2a79c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2a6d0c
0f2a79c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import gradio as gr
import requests
import os

API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct"

SYSTEM_PROMPT = (
    "You are a wise, slightly witty, and deeply philosophical cosmic guide. "
    "The user will share a minor daily human anxiety. Reframe their problem entirely from the "
    "perspective of their chosen cosmic scale. Keep your response strictly under 4 sentences. "
    "Make it comforting but funny, proving how wonderfully small their problem is in the grand timeline of reality."
)

def generate_cosmic_response(worry, scale):
    if not worry.strip():
        return "Please type a worry so the universe can handle it!"

    formatted_prompt = (
        f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
        f"<|im_start|>user\nMy worry is: '{worry}'. Reframe it from this perspective: {scale}.<|im_end|>\n"
        f"<|im_start|>assistant\n"
    )

    payload = {
        "inputs": formatted_prompt,
        "parameters": {
            "max_new_tokens": 200,
            "temperature": 0.7,
            "return_full_text": False
        }
    }

    # Automatically use Space token if available to bypass strict rate limits
    headers = {}
    if os.getenv("HF_TOKEN"):
        headers["Authorization"] = f"Bearer {os.getenv('HF_TOKEN')}"

    try:
        # INCREASED TIMEOUT to 60 seconds to allow the model time to wake up
        response = requests.post(API_URL, json=payload, headers=headers, timeout=60)
        
        # Safely handle overloaded Hugging Face servers without crashing
        if response.status_code != 200:
            try:
                error_data = response.json()
                if "error" in error_data:
                    return f"The universe is booting up: {error_data['error']}. Wait 20 seconds and try again!"
            except:
                pass
            return f"The universe is experiencing high traffic (Status {response.status_code}). Give it a minute!"

        output = response.json()
        if isinstance(output, list) and len(output) > 0:
            generated_text = output[0].get('generated_text', '').strip()
            return generated_text.replace("<|im_end|>", "").strip()
        elif isinstance(output, dict) and "error" in output:
            return f"The universe is booting up: {output['error']}. Give it a brief moment and try again!"
            
        return "The stars are misaligned. Try pressing the button once more."
        
    except requests.exceptions.Timeout:
        return "The stars are taking a while to align! The model is still waking up. Please click the button again."
    except Exception as e:
        return f"Cosmic connection error: {str(e)}. Take a deep breath and try again!"


head = """
<link href="https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@400;600;700;800;900&display=swap" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
<style>
  /* ── Reset & base ── */
  *, *::before, *::after { box-sizing: border-box; }

  body, .gradio-container {
    background-color: #131313 !important;
    color: #e5e2e1 !important;
    font-family: 'Hanken Grotesk', sans-serif !important;
    -webkit-font-smoothing: antialiased;
  }

  /* Hide default Gradio chrome we don't need */
  .gradio-container > .main > .wrap { padding: 0 !important; }
  footer { display: none !important; }

  /* ── All text visible ── */
  p, span, label, div, h1, h2, h3, h4, h5, h6,
  .prose, .prose p,
  [data-testid="markdown"], [data-testid="markdown"] p {
    color: #e5e2e1 !important;
    font-family: 'Hanken Grotesk', sans-serif !important;
    overflow: visible !important;
    max-height: none !important;
  }

  /* ── Hero title ── */
  .cosmic-title [data-testid="markdown"] p,
  .cosmic-title p {
    font-size: clamp(36px, 6vw, 64px) !important;
    font-weight: 900 !important;
    line-height: 1.1 !important;
    letter-spacing: -0.02em !important;
    color: #e5e2e1 !important;
    text-align: center !important;
  }

  /* ── Subtitle ── */
  .cosmic-subtitle [data-testid="markdown"] p,
  .cosmic-subtitle p {
    font-size: 18px !important;
    font-weight: 400 !important;
    line-height: 1.6 !important;
    color: #A1A1A1 !important;
    text-align: center !important;
  }

  /* ── Section labels ── */
  .cosmic-label [data-testid="markdown"] p,
  .cosmic-label p {
    font-size: 14px !important;
    font-weight: 700 !important;
    letter-spacing: 0.15em !important;
    text-transform: uppercase !important;
    color: #b4c5ff !important;
  }

  /* ── Textbox ── */
  .gradio-container textarea {
    background-color: #0e0e0e !important;
    color: #e5e2e1 !important;
    border: 1px solid #2D2D2D !important;
    border-radius: 12px !important;
    font-family: 'Hanken Grotesk', sans-serif !important;
    font-size: 18px !important;
    padding: 20px 24px !important;
  }
  .gradio-container textarea:focus {
    border-color: #b4c5ff !important;
    outline: none !important;
    box-shadow: none !important;
  }
  .gradio-container .label-wrap { display: none !important; }

  /* ── Tighten Gradio's default gaps ── */
  .gradio-container .gap,
  .gradio-container .flex-col {
    gap: 4px !important;
  }
  .gradio-container .block {
    padding: 4px 0 !important;
  }

  /* ── Kill ALL white/light backgrounds Gradio injects ── */
  .gradio-container .block,
  .gradio-container .form,
  .gradio-container fieldset,
  .gradio-container .wrap,
  .gradio-container .gap,
  .gradio-container > div,
  .gradio-container .container,
  .gradio-container .panel,
  .gradio-container .tabs,
  .gradio-container .tabitem,
  .gradio-container .row,
  .gradio-container .col,
  .gradio-container .box {
    background: transparent !important;
    background-color: transparent !important;
    border-color: transparent !important;
    box-shadow: none !important;
  }

  /* Style each radio item as a card */
  .gradio-container .block.svelte-1p9xokt label,
  .gradio-container .wrap label,
  .gradio-container input[type="radio"] + span {
    background-color: #1c1b1b !important;
    border: 1px solid #2D2D2D !important;
    border-radius: 12px !important;
    padding: 20px !important;
    color: #e5e2e1 !important;
    font-size: 14px !important;
    font-weight: 600 !important;
    transition: border-color 0.2s, background-color 0.2s !important;
    cursor: pointer !important;
  }
  .gradio-container input[type="radio"]:checked + span,
  .gradio-container .selected label {
    border-color: #b4c5ff !important;
    background-color: #1a1a1a !important;
  }

  /* ── Primary button ── */
  .gradio-container button.primary,
  #component-submit button {
    background: #e5e2e1 !important;
    color: #131313 !important;
    font-family: 'Hanken Grotesk', sans-serif !important;
    font-size: 14px !important;
    font-weight: 700 !important;
    letter-spacing: 0.02em !important;
    border: none !important;
    border-radius: 9999px !important;
    padding: 14px 40px !important;
    width: auto !important;
    min-width: 280px !important;
    max-width: 400px !important;
    white-space: nowrap !important;
    cursor: pointer !important;
    transition: opacity 0.2s !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  .gradio-container button.primary:hover { opacity: 0.88 !important; }

  /* Wrapper row β€” kill excess padding and stop it squishing the button into a circle */
  .gradio-container .row {
    justify-content: center !important;
    padding: 4px 0 !important;
    gap: 0 !important;
  }

  /* ── Output card ── */
  #reality-check-box {
    background-color: #131313 !important;
    border: 1px solid #2D2D2D !important;
    border-radius: 12px !important;
    padding: 32px !important;
    position: relative !important;
    overflow: hidden !important;
  }
  #reality-check-box::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at center, rgba(180,197,255,0.08) 0%, transparent 70%);
    pointer-events: none;
  }
  #reality-check-box [data-testid="markdown"] p,
  #reality-check-box p {
    font-size: 24px !important;
    font-weight: 700 !important;
    line-height: 1.3 !important;
    color: #c3c6d7 !important;
    font-style: italic !important;
    text-align: center !important;
  }

  /* ── Reality Check label above output ── */
  .reality-label [data-testid="markdown"] p,
  .reality-label p {
    font-size: 12px !important;
    font-weight: 700 !important;
    letter-spacing: 0.2em !important;
    text-transform: uppercase !important;
    color: #b4c5ff !important;
    text-align: center !important;
  }
</style>
"""

SCALES = [
    "A colony of ants living in your wall",
    "The 4.5 billion year geological history of the Earth",
    "A distant, indifferent supernova dying in another galaxy",
]

with gr.Blocks(theme=gr.themes.Monochrome(), head=head, css="") as demo:

    # ── Hero ──
    with gr.Column(elem_classes=["cosmic-title"]):
        gr.Markdown("# πŸͺ The Cosmic Scale-Out")
    with gr.Column(elem_classes=["cosmic-subtitle"]):
        gr.Markdown(
            "Anxiety grounding for busy minds. "
            "Reframe your tiny daily struggles against the grand architecture of time and space."
        )

    with gr.Column():
        # Worry input
        with gr.Column(elem_classes=["cosmic-label"]):
            gr.Markdown("What are you spiraling about right now?")
        worry_input = gr.Textbox(
            placeholder="e.g., I'm stressed about a presentation or a flight delay...",
            lines=4,
            show_label=False,
        )

        # Scale selector
        with gr.Column(elem_classes=["cosmic-label"]):
            gr.Markdown("Choose your cosmic scale factor:")
        perspective_input = gr.Radio(
            choices=SCALES,
            value=SCALES[0],
            show_label=False,
        )

        gr.HTML("<div style='height:8px'></div>")

        # Submit β€” centred
        with gr.Row():
            submit_btn = gr.Button("Hand Worry to the Universe ✨", variant="primary", elem_id="component-submit")

        gr.HTML("<div style='height:24px'></div>")

        # Output
        with gr.Column(elem_classes=["reality-label"]):
            gr.Markdown("πŸͺ Reality Check")
        output_text = gr.Markdown(elem_id="reality-check-box")

    submit_btn.click(
        fn=generate_cosmic_response,
        inputs=[worry_input, perspective_input],
        outputs=output_text,
    )

demo.launch()