File size: 4,177 Bytes
7725b42
3fd1be3
4bef18d
3fd1be3
4bef18d
3fd1be3
 
 
4bef18d
3fd1be3
 
4bef18d
3fd1be3
c48ba74
b48f0ec
 
 
497d9a2
2a605cf
b48f0ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bef18d
3fd1be3
 
4bef18d
3fd1be3
 
 
 
 
4bef18d
 
3fd1be3
 
 
 
 
c48ba74
 
 
3fd1be3
 
 
 
b48f0ec
3fd1be3
 
 
 
b48f0ec
3fd1be3
 
 
 
 
 
 
 
 
 
b48f0ec
 
 
 
 
 
 
3fd1be3
 
 
 
 
b48f0ec
3fd1be3
41e9e77
3fd1be3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c48ba74
 
3fd1be3
 
c48ba74
66acbac
3fd1be3
 
c48ba74
3fd1be3
 
 
 
4bef18d
 
3fd1be3
 
 
 
 
 
 
4bef18d
3fd1be3
4bef18d
3fd1be3
 
 
4bef18d
3fd1be3
4bef18d
7725b42
3fd1be3
 
 
 
7725b42
3fd1be3
c48ba74
3fd1be3
 
 
 
c48ba74
 
 
4bef18d
0b060ae
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
import os
import subprocess
import gradio as gr
import spaces

MODEL_REPO = "HauhauCS/Gemma-4-E4B-Uncensored-HauhauCS-Aggressive"
MODEL_FILE = "Gemma-4-E4B-Uncensored-HauhauCS-Aggressive-Q5_K_P.gguf"
MMPROJ_FILE = "mmproj-Gemma-4-E4B-Uncensored-HauhauCS-Aggressive-f16.gguf"

MODEL_PATH = os.path.join("/tmp", MODEL_FILE)
MMPROJ_PATH = os.path.join("/tmp", MMPROJ_FILE)

server_process = None

LLAMA_DIR = "/tmp/llama"
LLAMA_SERVER = os.path.join(LLAMA_DIR, "llama-server")
LLAMA_RELEASE = "b10182"
LLAMA_ARCHIVE = f"llama.cpp-{LLAMA_RELEASE}-cuda-12.8-amd64.tar.gz"
LLAMA_URL = f"https://github.com/ai-dock/llama.cpp-cuda/releases/download/{LLAMA_RELEASE}/{LLAMA_ARCHIVE}"


def download_llama():
    if os.path.exists(LLAMA_SERVER):
        return

    os.makedirs(LLAMA_DIR, exist_ok=True)

    archive = os.path.join("/tmp", LLAMA_ARCHIVE)

    subprocess.run(
        ["wget", "-q", "--show-progress", "-O", archive, LLAMA_URL],
        check=True,
    )

    subprocess.run(
        ["tar", "-xzf", archive, "-C", LLAMA_DIR, "--strip-components=1"],
        check=True,
    )

    if not os.path.exists(LLAMA_SERVER):
        raise RuntimeError(
            f"llama-server was not found after extraction: {LLAMA_SERVER}"
        )

    os.chmod(LLAMA_SERVER, 0o755)


def download_models():
    from huggingface_hub import hf_hub_download

    if not os.path.exists(MODEL_PATH):
        hf_hub_download(
            repo_id=MODEL_REPO,
            filename=MODEL_FILE,
            local_dir="/tmp",
        )

    if not os.path.exists(MMPROJ_PATH):
        hf_hub_download(
            repo_id=MODEL_REPO,
            filename=MMPROJ_FILE,
            local_dir="/tmp",
        )


@spaces.GPU
def generate(prompt, max_tokens, temperature):
    global server_process

    download_llama()
    download_models()

    if server_process is None or server_process.poll() is not None:
        cmd = [
            LLAMA_SERVER,
            "-m", MODEL_PATH,
            "--mmproj", MMPROJ_PATH,
            "--host", "127.0.0.1",
            "--port", "8080",
            "-c", "32768",
            "-n", "4096",
            "--n-gpu-layers", "99",
            "-t", "2",
        ]

        server_env = os.environ.copy()
        server_env["LD_LIBRARY_PATH"] = (
            LLAMA_DIR
            + ":"
            + server_env.get("LD_LIBRARY_PATH", "")
        )

        server_process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
            env=server_env,
        )

    import time
    import requests

    for _ in range(120):
        try:
            r = requests.get("http://127.0.0.1:8080/health", timeout=1)
            if r.ok:
                break
        except Exception:
            pass
        time.sleep(1)
    else:
        raise RuntimeError("llama-server did not become ready.")

    response = requests.post(
        "http://127.0.0.1:8080/completion",
        json={
            "prompt": prompt,
            "n_predict": int(max_tokens),
            "temperature": float(temperature),
            "stream": False,
        },
        timeout=600,
    )

    response.raise_for_status()
    return response.json().get("content", "")


with gr.Blocks(title="Gemma-4-E4B Uncensored") as demo:
    gr.Markdown("# Gemma-4-E4B-Uncensored-HauhauCS-Aggressive")

    prompt = gr.Textbox(
        label="Prompt",
        placeholder="Enter your prompt...",
        lines=8,
    )

    with gr.Row():
        max_tokens = gr.Slider(
            minimum=64,
            maximum=4096,
            value=1024,
            step=64,
            label="Max tokens",
        )

        temperature = gr.Slider(
            minimum=0.0,
            maximum=2.0,
            value=0.7,
            step=0.05,
            label="Temperature",
        )

    output = gr.Textbox(
        label="Response",
        lines=20,
    )

    generate_button = gr.Button("Generate", variant="primary")

    generate_button.click(
        generate,
        inputs=[prompt, max_tokens, temperature],
        outputs=output,
    )


if __name__ == "__main__":
    demo.launch(ssr_mode=False)