Suporte commited on
Commit
0524d04
·
1 Parent(s): d613c86

Switch Space to remote Qwen image edit uncensored setup for CPU Basic

Browse files
Files changed (3) hide show
  1. README.md +16 -18
  2. app.py +53 -85
  3. requirements.txt +1 -1
README.md CHANGED
@@ -1,27 +1,25 @@
1
  ---
2
- title: Qwen3.6-35B-A3B HauhauCS CPU Basic
3
- emoji:
4
- colorFrom: blue
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 5.29.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Space Gradio preparado para CPU Basic usando o modelo:
13
- `HauhauCS/Qwen3.6-35B-A3B-Uncensored-HauhauCS-Aggressive`
14
 
15
- Configuração rápida:
16
- 1. Em **Settings > Hardware**, ativa **CPU Basic**.
17
- 2. Faz push destes ficheiros para o teu Space.
18
- 3. (Opcional) Em **Variables**, troca `MODEL_FILE` para outro GGUF do repositório.
19
- 4. Se ficar lento ou sem memória, usa quantizações mais leves e reduz `N_CTX`.
20
 
21
- Variáveis opcionais:
22
- - `MODEL_FILE` (default: `Qwen3.6-35B-A3B-Uncensored-HauhauCS-Aggressive-IQ2_M.gguf`)
23
- - `N_CTX` (default: `4096`)
24
- - `MAX_NEW_TOKENS` (default: `256`)
25
- - `TEMPERATURE` (default: `0.7`)
26
- - `TOP_P` (default: `0.9`)
27
- - `N_GPU_LAYERS` (default: `0`)
 
 
 
 
1
  ---
2
+ title: Qwen Image Edit Uncensored CPU Basic
3
+ colorFrom: gray
4
+ colorTo: blue
 
5
  sdk: gradio
6
  sdk_version: 5.29.1
7
  app_file: app.py
8
  pinned: false
9
  ---
10
 
11
+ Space configurado para CPU Basic com edicao de imagem via endpoint remoto.
 
12
 
13
+ Modelo padrao:
14
+ `ScottzillaSystems/qwen-image-edit-plus-nsfw-lora`
 
 
 
15
 
16
+ Como funciona:
17
+ - O Space roda so a interface em CPU Basic.
18
+ - A inferencia de image edit roda no provider remoto do Hugging Face.
19
+
20
+ Secrets obrigatorias:
21
+ - `HF_TOKEN` com permissao de leitura (idealmente write tambem).
22
+
23
+ Variaveis opcionais:
24
+ - `MODEL_ID` (default: `ScottzillaSystems/qwen-image-edit-plus-nsfw-lora`)
25
+ - `MAX_SIDE` (default: `1024`)
app.py CHANGED
@@ -1,94 +1,62 @@
 
1
  import os
2
- import threading
3
-
4
  import gradio as gr
5
- from huggingface_hub import hf_hub_download
6
- from llama_cpp import Llama
7
-
8
- REPO_ID = "HauhauCS/Qwen3.6-35B-A3B-Uncensored-HauhauCS-Aggressive"
9
- MODEL_FILE = os.getenv(
10
- "MODEL_FILE",
11
- "Qwen3.6-35B-A3B-Uncensored-HauhauCS-Aggressive-IQ2_M.gguf",
12
- )
13
- SYSTEM_PROMPT = os.getenv(
14
- "SYSTEM_PROMPT",
15
- "You are a direct, concise assistant."
16
- )
17
- N_CTX = int(os.getenv("N_CTX", "512"))
18
- MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", "256"))
19
- TEMPERATURE = float(os.getenv("TEMPERATURE", "0.7"))
20
- TOP_P = float(os.getenv("TOP_P", "0.9"))
21
- N_GPU_LAYERS = int(os.getenv("N_GPU_LAYERS", "0"))
22
-
23
- _llm = None
24
- _llm_error = None
25
- _model_lock = threading.Lock()
26
-
27
-
28
- def get_llm() -> Llama:
29
- global _llm, _llm_error
30
- if _llm_error is not None:
31
- raise RuntimeError(_llm_error)
32
- if _llm is None:
33
- with _model_lock:
34
- if _llm is None:
35
- try:
36
- model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE)
37
- _llm = Llama(
38
- model_path=model_path,
39
- n_ctx=N_CTX,
40
- n_gpu_layers=N_GPU_LAYERS,
41
- n_threads=max((os.cpu_count() or 4) // 2, 1),
42
- n_batch=128,
43
- verbose=False,
44
- chat_format="chatml",
45
- n_ubatch=32,
46
- )
47
- except Exception as e:
48
- _llm_error = str(e)
49
- raise
50
- return _llm
51
-
52
-
53
- def chat(message: str, history):
54
- try:
55
- llm = get_llm()
56
- except Exception:
57
- return "Olá! Não consegui carregar este modelo no hardware CPU Basic atual."
58
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
59
-
60
- for user_msg, assistant_msg in history:
61
- if user_msg:
62
- messages.append({"role": "user", "content": user_msg})
63
- if assistant_msg:
64
- messages.append({"role": "assistant", "content": assistant_msg})
65
 
66
- messages.append({"role": "user", "content": message})
 
 
 
67
 
68
- try:
69
- response = llm.create_chat_completion(
70
- messages=messages,
71
- max_tokens=MAX_NEW_TOKENS,
72
- temperature=TEMPERATURE,
73
- top_p=TOP_P,
74
- )
75
- return response["choices"][0]["message"]["content"]
76
- except Exception:
77
- return "Olá! O modelo não conseguiu gerar resposta neste hardware."
78
 
79
 
80
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
81
- gr.Markdown("# Qwen3.6-35B-A3B (HauhauCS) on CPU Basic")
82
- gr.Markdown(
83
- "Primeira resposta pode demorar (download + load do GGUF). Usa Enter ou o botão Enviar."
84
- )
85
- gr.ChatInterface(
86
- fn=chat,
87
- textbox=gr.Textbox(placeholder="Escreve a tua mensagem...", lines=3),
88
- submit_btn="Enviar",
89
- stop_btn="Parar",
90
- )
91
 
92
 
93
  if __name__ == "__main__":
94
- demo.queue(default_concurrency_limit=1).launch(show_error=True)
 
1
+ import io
2
  import os
 
 
3
  import gradio as gr
4
+ from PIL import Image
5
+ from huggingface_hub import InferenceClient
6
+
7
+ MODEL_ID = os.getenv("MODEL_ID", "ScottzillaSystems/qwen-image-edit-plus-nsfw-lora")
8
+ MAX_SIDE = int(os.getenv("MAX_SIDE", "1024"))
9
+
10
+
11
+ def _resize_for_api(img: Image.Image) -> Image.Image:
12
+ img = img.convert("RGB")
13
+ w, h = img.size
14
+ m = max(w, h)
15
+ if m <= MAX_SIDE:
16
+ return img
17
+ scale = MAX_SIDE / m
18
+ nw = max(64, int(w * scale))
19
+ nh = max(64, int(h * scale))
20
+ return img.resize((nw, nh), Image.Resampling.LANCZOS)
21
+
22
+
23
+ def edit_image(image: Image.Image, prompt: str):
24
+ if image is None:
25
+ raise gr.Error("Carrega uma imagem primeiro.")
26
+ if not prompt or not prompt.strip():
27
+ raise gr.Error("Escreve a instrução de edição.")
28
+
29
+ token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
30
+ if not token:
31
+ raise gr.Error("Falta HF_TOKEN nas Secrets do Space.")
32
+
33
+ client = InferenceClient(token=token)
34
+ base = _resize_for_api(image)
35
+
36
+ out = client.image_to_image(
37
+ image=base,
38
+ prompt=prompt.strip(),
39
+ model=MODEL_ID,
40
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ if isinstance(out, Image.Image):
43
+ return out
44
+ if isinstance(out, bytes):
45
+ return Image.open(io.BytesIO(out)).convert("RGB")
46
 
47
+ raise gr.Error("Resposta inesperada do endpoint de image edit.")
 
 
 
 
 
 
 
 
 
48
 
49
 
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("# Qwen Image Edit (CPU Basic frontend)")
52
+ gr.Markdown("Este Space corre em CPU Basic e usa inferência remota para editar imagens.")
53
+ with gr.Row():
54
+ input_img = gr.Image(type="pil", label="Imagem")
55
+ output_img = gr.Image(type="pil", label="Resultado")
56
+ prompt = gr.Textbox(lines=3, label="Instrução")
57
+ run = gr.Button("Editar")
58
+ run.click(edit_image, inputs=[input_img, prompt], outputs=output_img)
 
 
59
 
60
 
61
  if __name__ == "__main__":
62
+ demo.launch(show_error=True)
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  gradio==5.29.1
2
  huggingface_hub>=0.31.0
3
- llama-cpp-python==0.3.9
 
1
  gradio==5.29.1
2
  huggingface_hub>=0.31.0
3
+ Pillow>=10.0.0