Switch to transformers with Qwen2.5-7B-Instruct
Browse filesFiles changed (3)
- README.md+3-10
- app.py+46-55
- requirements.txt+3-2
9 unmodified lines10111213141516171819202122232425262728299 unmodified lineslicense: apache-2.0tags:- qwen- uncensored- llama-cpp- zerogpu---# Qwen3.5-9B Uncensored APIAPI interface for [HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive](https://huggingface.co/HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive).## Features- 9B parameters, fully uncensored (0/465 refusals)- Q4_K_M quantization via llama.cpp- Running on ZeroGPU## API Usage9 unmodified lines101112131415161718192021229 unmodified lineslicense: apache-2.0tags:- qwen- transformers- zerogpu---# Qwen2.5-7B-Instruct APIAPI interface for [Qwen/Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) on ZeroGPU.## API Usage
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748496 unmodified lines56575859606162636465666768697071727331 unmodified lines1051061071081091101111121131141151161171181191201211221231241251261271281291302 unmodified lines13313413513613713813915 unmodified lines155156157158159160161import osimport gradio as grimport spacesfrom huggingface_hub import hf_hub_downloadMODEL_REPO = "HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive"MODEL_FILE = "Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-Q4_K_M.gguf"model_path = Nonellm = Nonedef download_model():global model_pathif model_path is None:print("Downloading model...")model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)print(f"Model downloaded: {model_path}")return model_pathdef get_llm():global llmif llm is None:from llama_cpp import Llamapath = download_model()print("Loading model into GPU...")llm = Llama(model_path=path,n_ctx=8192,n_gpu_layers=-1,verbose=False,)print("Model loaded!")return llmdef format_messages(message: str, history: list, system_prompt: str = "") -> str:formatted = ""if system_prompt.strip():formatted += f"<|im_start|>system\n{system_prompt}<|im_end|>\n"for user_msg, assistant_msg in history:if user_msg:formatted += f"<|im_start|>user\n{user_msg}<|im_end|>\n"if assistant_msg:formatted += f"<|im_start|>assistant\n{assistant_msg}<|im_end|>\n"formatted += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"return formatted@spaces.GPU(duration=120)6 unmodified linestop_k: int = 20,max_tokens: int = 1024,) -> str:model = get_llm()prompt = format_messages(message, history, system_prompt)output = model(prompt,max_tokens=max_tokens,temperature=temperature,top_p=top_p,top_k=top_k,stop=["<|im_end|>", "<|im_start|>"],)return output["choices"][0]["text"].strip()@spaces.GPU(duration=120)31 unmodified linesreturn {"response": None, "status": "error", "error": str(e)}with gr.Blocks(title="Qwen3.5-9B Uncensored API", theme=gr.themes.Soft()) as demo:gr.Markdown("""# 🤖 Qwen3.5-9B Uncensored APIPowered by [HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive](https://huggingface.co/HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive)- 9B parameters, fully uncensored (0/465 refusals)- Q4_K_M quantization via llama.cpp on ZeroGPU""")with gr.Tab("💬 Chat"):chatbot = gr.Chatbot(height=450, label="Conversation")with gr.Row():msg = gr.Textbox(label="Message", placeholder="Type here...", scale=4, lines=2)submit_btn = gr.Button("Send", variant="primary", scale=1)with gr.Accordion("⚙️ Settings", open=False):system_prompt = gr.Textbox(label="System Prompt", placeholder="Optional", lines=2)with gr.Row():temperature = gr.Slider(0.0, 2.0, 0.7, step=0.1, label="Temperature")2 unmodified linestop_k = gr.Slider(1, 100, 20, step=1, label="Top K")max_tokens = gr.Slider(64, 2048, 1024, step=64, label="Max Tokens")clear_btn = gr.Button("🗑️ Clear")def user_submit(message, history):return "", history + [[message, None]]15 unmodified lines)clear_btn.click(lambda: [], None, chatbot)with gr.Tab("🔌 API"):gr.Markdown("""## API Usage123456789101112131415161718192021222324252627286 unmodified lines35363738394041424344454647484950515253545556575859606162636465666731 unmodified lines991001011021031041051061071081091101111121131141151161171181191201212 unmodified lines12412512612712812913015 unmodified lines146147148149150151152import osimport torchimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, AutoTokenizerMODEL_ID = "Qwen/Qwen2.5-7B-Instruct"tokenizer = Nonemodel = Nonedef load_model():global tokenizer, modelif model is None:print("Loading tokenizer...")tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)print("Loading model...")model = AutoModelForCausalLM.from_pretrained(MODEL_ID,torch_dtype=torch.bfloat16,device_map="auto",trust_remote_code=True,)print("Model loaded!")return tokenizer, model@spaces.GPU(duration=120)6 unmodified linestop_k: int = 20,max_tokens: int = 1024,) -> str:tok, mdl = load_model()messages = []if system_prompt.strip():messages.append({"role": "system", "content": system_prompt})for user_msg, assistant_msg in history:if user_msg:messages.append({"role": "user", "content": user_msg})if assistant_msg:messages.append({"role": "assistant", "content": assistant_msg})messages.append({"role": "user", "content": message})text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tok([text], return_tensors="pt").to(mdl.device)outputs = mdl.generate(**inputs,max_new_tokens=max_tokens,temperature=temperature,top_p=top_p,top_k=top_k,do_sample=True,pad_token_id=tok.eos_token_id,)generated = outputs[0][inputs['input_ids'].shape[-1]:]return tok.decode(generated, skip_special_tokens=True)@spaces.GPU(duration=120)31 unmodified linesreturn {"response": None, "status": "error", "error": str(e)}with gr.Blocks(title="Qwen API", theme=gr.themes.Soft()) as demo:gr.Markdown("""# Qwen2.5-7B-Instruct APIPowered by [Qwen/Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) on ZeroGPU""")with gr.Tab("Chat"):chatbot = gr.Chatbot(height=450, label="Conversation")with gr.Row():msg = gr.Textbox(label="Message", placeholder="Type here...", scale=4, lines=2)submit_btn = gr.Button("Send", variant="primary", scale=1)with gr.Accordion("Settings", open=False):system_prompt = gr.Textbox(label="System Prompt", placeholder="Optional", lines=2)with gr.Row():temperature = gr.Slider(0.0, 2.0, 0.7, step=0.1, label="Temperature")2 unmodified linestop_k = gr.Slider(1, 100, 20, step=1, label="Top K")max_tokens = gr.Slider(64, 2048, 1024, step=64, label="Max Tokens")clear_btn = gr.Button("Clear")def user_submit(message, history):return "", history + [[message, None]]15 unmodified lines)clear_btn.click(lambda: [], None, chatbot)with gr.Tab("API"):gr.Markdown("""## API Usage
12345gradio>=4.0.0huggingface_hub>=0.20.0spaces--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124llama-cpp-python123456gradio>=4.0.0huggingface_hub>=0.20.0spacestorchtransformersaccelerate
README.md+3-10
app.py+46-55
requirements.txt+3-2