File size: 3,736 Bytes
eda4739
 
 
 
 
733e010
eda4739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733e010
 
 
 
 
 
eda4739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00679d8
 
 
 
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
"""Gradio app for the Cree1865 remote Tinker sampler."""

from __future__ import annotations

import os
import json

try:
    from dotenv import load_dotenv

    load_dotenv()
except Exception:
    pass

import gradio as gr

try:
    from .tinker_remote import (
        DEFAULT_MODEL_PATH,
        DEFAULT_SYSTEM_PROMPT,
        EXAMPLE_PROMPTS,
        generate_for_ui,
    )
except ImportError:
    from tinker_remote import (  # type: ignore
        DEFAULT_MODEL_PATH,
        DEFAULT_SYSTEM_PROMPT,
        EXAMPLE_PROMPTS,
        generate_for_ui,
    )


def infer(
    prompt: str,
    system_prompt: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    seed: int,
    num_samples: int,
    enable_thinking: bool,
):
    return generate_for_ui(
        prompt=prompt,
        system_prompt=system_prompt,
        max_tokens=int(max_tokens),
        temperature=float(temperature),
        top_p=float(top_p),
        seed=int(seed),
        num_samples=int(num_samples),
        enable_thinking=bool(enable_thinking),
    )


def endpoint_status() -> dict[str, object]:
    return {
        "endpoint": DEFAULT_MODEL_PATH,
        "tinker_key_configured": bool(os.getenv("TINKER_API_KEY")),
    }


with gr.Blocks(title="Cree1865 Tinker Endpoint") as demo:
    gr.Markdown("# Cree1865 Tinker Endpoint")
    with gr.Accordion("Run context", open=False):
        gr.Markdown(
            "This Space calls the final 800-step Tinker sampler remotely. "
            "It is an experimental endpoint for inspection, not a validated fluent Cree model."
        )
        gr.Textbox(
            value=json.dumps(endpoint_status(), indent=2),
            label="Endpoint status",
            lines=4,
            interactive=False,
        )

    with gr.Row():
        with gr.Column(scale=3):
            prompt = gr.Textbox(
                lines=6,
                label="Prompt",
                placeholder="Ask for a Cree dictionary lookup or translation.",
            )
            system_prompt = gr.Textbox(
                value=DEFAULT_SYSTEM_PROMPT,
                lines=3,
                label="System prompt",
            )
            run = gr.Button("Run", variant="primary")
        with gr.Column(scale=2):
            max_tokens = gr.Slider(16, 256, value=96, step=8, label="Max tokens")
            temperature = gr.Slider(0.0, 1.2, value=0.3, step=0.05, label="Temperature")
            top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
            seed = gr.Number(value=42, precision=0, label="Seed")
            num_samples = gr.Slider(1, 4, value=1, step=1, label="Samples")
            enable_thinking = gr.Checkbox(value=False, label="Enable thinking")

    output = gr.Textbox(lines=10, label="Model output")
    metadata = gr.JSON(label="Run metadata")

    gr.Examples(
        examples=[[example] for example in EXAMPLE_PROMPTS],
        inputs=[prompt],
    )

    run.click(
        fn=infer,
        inputs=[
            prompt,
            system_prompt,
            max_tokens,
            temperature,
            top_p,
            seed,
            num_samples,
            enable_thinking,
        ],
        outputs=[output, metadata],
        api_name="infer",
    )
    prompt.submit(
        fn=infer,
        inputs=[
            prompt,
            system_prompt,
            max_tokens,
            temperature,
            top_p,
            seed,
            num_samples,
            enable_thinking,
        ],
        outputs=[output, metadata],
        api_name=False,
    )


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=2).launch(
        server_name="0.0.0.0",
        server_port=int(os.getenv("PORT", "7860")),
    )