Use llama.cpp server with OpenAI-compatible API
Browse filesFiles changed (4)
- Dockerfile+19-25
- README.md+59-23
- app.pyDeleted-280
- requirements.txtDeleted-2
12345678910111213141516171819202122232425262728FROM nvidia/cuda:12.4.0-devel-ubuntu22.04ENV DEBIAN_FRONTEND=noninteractiveENV CMAKE_ARGS="-DGGML_CUDA=on"ENV FORCE_CMAKE=1RUN apt-get update && apt-get install -y \python3 \python3-pip \git \cmake \build-essential \&& rm -rf /var/lib/apt/lists/*WORKDIR /appRUN pip3 install --no-cache-dir --upgrade pipRUN pip3 install --no-cache-dir llama-cpp-pythonCOPY requirements.txt .RUN pip3 install --no-cache-dir -r requirements.txtCOPY app.py .EXPOSE 7860CMD ["python3", "app.py"]12345678910111213141516171819202122FROM ghcr.io/ggml-org/llama.cpp:fullWORKDIR /appRUN apt update && apt install -y python3-pipRUN pip install -U huggingface_hubRUN python3 -c 'from huggingface_hub import hf_hub_download; \repo="HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive"; \hf_hub_download(repo_id=repo, filename="Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-Q4_K_M.gguf", local_dir="/app"); \hf_hub_download(repo_id=repo, filename="mmproj-Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-BF16.gguf", local_dir="/app")'CMD ["--server", \"-m", "/app/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-Q4_K_M.gguf", \"--mmproj", "/app/mmproj-Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-BF16.gguf", \"--host", "0.0.0.0", \"--port", "7860", \"-t", "2", \"--cache-type-k", "q8_0", \"--cache-type-v", "iq4_nl", \"-c", "32768", \"-n", "8192"]
3 unmodified lines456789101 unmodified line1213141516171819202122232 unmodified lines262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273743 unmodified linescolorFrom: bluecolorTo: purplesdk: dockerapp_file: app.pypinned: falselicense: apache-2.0tags:1 unmodified line- uncensored- llama-cpp- ggufsuggested_hardware: a10g-small---# Qwen3.5-9B Uncensored API InterfaceAPI interface for [HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive](https://huggingface.co/HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive).## Features2 unmodified lines- Multimodal capable (text, image, video)- Supports 201 languages- Q4_K_M quantization via llama.cpp## API Usage### Python```pythonfrom gradio_client import Clientclient = Client("Ngixdev/qwen-api")result = client.predict(prompt="Your question here",system_prompt="You are a helpful assistant",temperature=0.7,top_p=0.8,max_tokens=1024,api_name="/api_generate")print(result)```### cURL```bashcurl -X POST https://ngixdev-qwen-api.hf.space/api/api_generate \-H "Content-Type: application/json" \-d '{"data": ["Your question here","You are a helpful assistant",0.7,0.8,1024]}'```## Parameters| Parameter | Type | Default | Description ||-----------|------|---------|-------------|| prompt | string | required | User prompt/question || system_prompt | string | "" | System instruction || temperature | float | 0.7 | Sampling temperature (0.0-2.0) || top_p | float | 0.8 | Nucleus sampling (0.0-1.0) || max_tokens | int | 1024 | Maximum tokens to generate |3 unmodified lines4567891 unmodified line111213141516171819202122232 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091103 unmodified linescolorFrom: bluecolorTo: purplesdk: dockerpinned: falselicense: apache-2.0tags:1 unmodified line- uncensored- llama-cpp- gguf- openai-compatiblesuggested_hardware: a10g-small---# Qwen3.5-9B Uncensored APIOpenAI-compatible API for [HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive](https://huggingface.co/HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive).## Features2 unmodified lines- Multimodal capable (text, image, video)- Supports 201 languages- Q4_K_M quantization via llama.cpp- OpenAI-compatible API## API Usage### Python (OpenAI SDK)```pythonfrom openai import OpenAIclient = OpenAI(base_url="https://ngixdev-qwen-api.hf.space/v1",api_key="not-needed")response = client.chat.completions.create(model="qwen",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello, who are you?"}],temperature=0.7,max_tokens=1024)print(response.choices[0].message.content)```### cURL```bashcurl https://ngixdev-qwen-api.hf.space/v1/chat/completions \-H "Content-Type: application/json" \-d '{"model": "qwen","messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello!"}],"temperature": 0.7,"max_tokens": 1024}'```### Streaming```pythonfrom openai import OpenAIclient = OpenAI(base_url="https://ngixdev-qwen-api.hf.space/v1",api_key="not-needed")stream = client.chat.completions.create(model="qwen",messages=[{"role": "user", "content": "Tell me a story"}],stream=True)for chunk in stream:if chunk.choices[0].delta.content:print(chunk.choices[0].delta.content, end="")```## Endpoints| Endpoint | Description ||----------|-------------|| `/v1/chat/completions` | Chat completions (OpenAI-compatible) || `/v1/completions` | Text completions || `/v1/models` | List available models || `/health` | Health check |## Parameters| Parameter | Type | Default | Description ||-----------|------|---------|-------------|| messages | array | required | Chat messages || temperature | float | 0.7 | Sampling temperature (0.0-2.0) || top_p | float | 0.8 | Nucleus sampling (0.0-1.0) || max_tokens | int | 1024 | Maximum tokens to generate || stream | bool | false | Enable streaming response |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280import osimport gradio as grfrom huggingface_hub import hf_hub_downloadfrom llama_cpp import Llama
MODEL_REPO = "HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive"MODEL_FILE = "Qwen3.5-9B-Uncensored-HauhauCS-Aggressive-Q4_K_M.gguf"
print("Downloading model...")model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)print(f"Model downloaded to: {model_path}")
print("Loading model...")llm = Llama( model_path=model_path, n_ctx=8192, n_gpu_layers=-1, verbose=False,)print("Model loaded!")
def 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
def generate_response( message: str, history: list, system_prompt: str = "", temperature: float = 0.7, top_p: float = 0.8, top_k: int = 20, max_tokens: int = 2048,) -> str: prompt = format_messages(message, history, system_prompt) output = llm( 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()
def api_generate( prompt: str, system_prompt: str = "", temperature: float = 0.7, top_p: float = 0.8, max_tokens: int = 2048,) -> dict: """ API endpoint for text generation. Args: prompt: The user prompt/question system_prompt: Optional system instruction temperature: Sampling temperature (0.0-2.0) top_p: Nucleus sampling parameter (0.0-1.0) max_tokens: Maximum tokens to generate Returns: Dictionary with 'response' key containing generated text """ try: response = generate_response( message=prompt, history=[], system_prompt=system_prompt, temperature=temperature, top_p=top_p, max_tokens=max_tokens, ) return {"response": response, "status": "success"} except Exception as e: return {"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 API Interface Powered by [HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive](https://huggingface.co/HauhauCS/Qwen3.5-9B-Uncensored-HauhauCS-Aggressive) **Features:** - 9B parameters with 262K context window - Fully uncensored (0/465 refusals) - Multimodal capable (text, image, video) - Supports 201 languages - Running with Q4_K_M quantization via llama.cpp Use the chat interface below or access via API. """ ) with gr.Tab("💬 Chat"): chatbot = gr.Chatbot(height=500, label="Conversation") with gr.Row(): msg = gr.Textbox( label="Message", placeholder="Type your message 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: Set behavior/personality for the model", lines=3, ) with gr.Row(): temperature = gr.Slider( minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature", ) top_p = gr.Slider( minimum=0.0, maximum=1.0, value=0.8, step=0.05, label="Top P", ) with gr.Row(): top_k = gr.Slider( minimum=1, maximum=100, value=20, step=1, label="Top K", ) max_tokens = gr.Slider( minimum=64, maximum=4096, value=1024, step=64, label="Max Tokens", ) clear_btn = gr.Button("🗑️ Clear Chat") def user_submit(message, history): return "", history + [[message, None]] def bot_response(history, system_prompt, temperature, top_p, top_k, max_tokens): if not history: return history message = history[-1][0] history_without_last = history[:-1] response = generate_response( message, history_without_last, system_prompt, temperature, top_p, top_k, max_tokens ) history[-1][1] = response return history msg.submit( user_submit, [msg, chatbot], [msg, chatbot] ).then( bot_response, [chatbot, system_prompt, temperature, top_p, top_k, max_tokens], chatbot, ) submit_btn.click( user_submit, [msg, chatbot], [msg, chatbot] ).then( bot_response, [chatbot, system_prompt, temperature, top_p, top_k, max_tokens], chatbot, ) clear_btn.click(lambda: [], None, chatbot) with gr.Tab("🔌 API"): gr.Markdown( """ ## API Usage This Space provides a REST API for programmatic access. ### Python Example ```python from gradio_client import Client client = Client("Ngixdev/qwen-api") result = client.predict( prompt="Explain quantum computing in simple terms", system_prompt="You are a helpful assistant", temperature=0.7, top_p=0.8, max_tokens=1024, api_name="/api_generate" ) print(result) ``` ### cURL Example ```bash curl -X POST https://ngixdev-qwen-api.hf.space/api/api_generate \\ -H "Content-Type: application/json" \\ -d '{ "data": [ "Explain quantum computing", "You are a helpful assistant", 0.7, 0.8, 1024 ] }' ``` """ ) with gr.Row(): with gr.Column(): api_prompt = gr.Textbox( label="Prompt", placeholder="Enter your prompt here...", lines=4, ) api_system = gr.Textbox( label="System Prompt (Optional)", placeholder="Set behavior/personality...", lines=2, ) with gr.Row(): api_temp = gr.Slider(0.0, 2.0, 0.7, step=0.1, label="Temperature") api_top_p = gr.Slider(0.0, 1.0, 0.8, step=0.05, label="Top P") api_max_tokens = gr.Slider(64, 4096, 1024, step=64, label="Max Tokens") api_submit = gr.Button("Generate", variant="primary") with gr.Column(): api_output = gr.JSON(label="API Response") api_submit.click( api_generate, [api_prompt, api_system, api_temp, api_top_p, api_max_tokens], api_output, api_name="api_generate", )
demo.launch(server_name="0.0.0.0", server_port=7860)12gradio>=4.0.0huggingface_hub>=0.20.0Dockerfile+19-25
README.md+59-23
app.pyDeleted-280
requirements.txtDeleted-2