Spaces:
Running
Running
Suporte commited on
Commit ·
0524d04
1
Parent(s): d613c86
Switch Space to remote Qwen image edit uncensored setup for CPU Basic
Browse files- README.md +16 -18
- app.py +53 -85
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -1,27 +1,25 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.29.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
Space
|
| 13 |
-
`HauhauCS/Qwen3.6-35B-A3B-Uncensored-HauhauCS-Aggressive`
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
- `
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 6 |
-
from
|
| 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 |
-
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 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 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(
|
| 81 |
-
gr.Markdown("#
|
| 82 |
-
gr.Markdown(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
stop_btn="Parar",
|
| 90 |
-
)
|
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
demo.
|
|
|
|
| 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 |
-
|
|
|
|
| 1 |
gradio==5.29.1
|
| 2 |
huggingface_hub>=0.31.0
|
| 3 |
+
Pillow>=10.0.0
|