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.
1 Like

I honestly don’t know what you mean by “install,” at least what I know of, like downloading a file and click the install button afterwards. If you meant by “install”, do mean by just copy-paste it to Grok?

1 Like

If you meant by “install”, do mean by just copy-paste it to Grok?

No, copy-paste it to your terminal (e.g. On Windows, CMD or PowerShell) and press enter.
You must install Python beforehand. Personally, I recommend Python 3.11 or Python 3.12.


Install once into your Windows Python, verify it works, then start writing code in any project folder. “Install” here means “run a terminal command so pip downloads libraries and adds them to Python.” pip is the Python package installer. (pip.pypa.io)

Step 1. Install Python and confirm it is reachable

  1. Install Python 3.10+ (Gradio needs Python 3.10 or higher). (Gradio)
  2. Open PowerShell (Start menu → type “PowerShell”).

Now run:

py --version
py -m pip --version

If those print versions, continue.

If py is not found, reinstall Python and enable “Add Python to PATH”, or use python instead of py everywhere.

Step 2. Install the packages (global install)

Run:

py -m pip install --upgrade pip
py -m pip install gradio openai

This is the “install.” It downloads packages and registers them so import gradio and import openai work.

Gradio’s docs explicitly recommend installing with pip. (Gradio)
OpenAI’s docs show pip install openai. (OpenAI Platform)

Step 3. If you hit permission errors, use per-user install

If you see errors like “Access is denied” or “not writeable”, install to your Windows user account:

py -m pip install --user --upgrade pip
py -m pip install --user gradio openai

This --user pattern is documented for Windows. (Python Packaging)

Step 4. Verify installation

Run:

py -c "import gradio, openai; print('installed OK')"

If you see installed OK, installation is done.

Step 5. Create any project folder and start coding

Packages are not installed “into” each folder. They live in your Python environment. Your folders just hold code.

Example:

mkdir my_project
cd my_project
notepad app.py

Put this in app.py (simple Gradio test):

import gradio as gr

def greet(name):
    return f"Hello, {name}!"

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()

Run it:

py app.py

Gradio’s quickstart shows this workflow (write app.py, run it). (Gradio)

Step 6. If gradio command is “not recognized”

If you installed with --user, Windows may not have the user “Scripts” directory on PATH. The Packaging guide explains how to find it: run py -m site --user-site and replace site-packages with Scripts, then add that folder to PATH. (Python Packaging)


Two important warnings if you skip virtual environments

  1. Version conflicts are real. Installing for Project B can break Project A later.
  2. Keep a requirements.txt per project anyway (for repeatability), even if you install globally. The Packaging guide documents requirements files and the Windows command pattern. (Python Packaging)

Minimal checklist

  • PowerShell → py --version
  • Install → py -m pip install gradio openai (Gradio)
  • If permissions fail → add --user (Python Packaging)
  • Verify → py -c "import gradio, openai; print('installed OK')"
  • Make any folder → write app.pypy app.py (Gradio)

I’m using the Command Prompt (CMD). At first it worked, using the

pip install gradio openai

Then run it.

The after that, I’ve used

setx XAI_API_KEY "your_real_xai_key_here"

Run it again, and it was a success.

But once I’m about to create the app with the python you’ve provided me, it immediately couldn’t able to recognised the command. Here’s what it showed me.

C:\Users\rubai>import os
'import' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>import gradio as gr
'import' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>from openai import OpenAI
'from' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>
C:\Users\rubai># 1. Connect the OpenAI client to xAI’s API
'#' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>client = OpenAI(
'client' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    api_key=os.environ.get("XAI_API_KEY"),
'api_key' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    base_url="https://api.x.ai/v1",
'base_url' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>)
C:\Users\rubai>
C:\Users\rubai># 2. System prompt controls style, but stays inside xAI rules
'#' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>SYSTEM_PROMPT = """
'SYSTEM_PROMPT' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>You are a political news explainer.
'You' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>You can lean slightly conservative and tabloid-style if asked,
'You' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>but you stay factual and avoid slurs, hate, or calls for violence.
'but' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>Always follow xAI's safety rules.
'Always' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>Explain things clearly for non-technical readers.
'Explain' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>""".strip()
'""".strip()' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>
C:\Users\rubai># 3. Gradio chat function: ignore history for now (simpler)
'#' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>def chat(message, history):
'def' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    response = client.chat.completions.create(
'response' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>        model="grok-4",  # or another Grok model enabled on your account
'model' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>        messages=[
'messages' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>            {"role": "system", "content": SYSTEM_PROMPT},
'{"role":' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>            {"role": "user", "content": message},
'{"role":' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>        ],
']' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>        temperature=0.7,
'temperature' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    )
C:\Users\rubai>    return response.choices[0].message.content
'return' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>
C:\Users\rubai># 4. Wrap it in a Gradio ChatInterface
'#' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>demo = gr.ChatInterface(
'demo' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    fn=chat,
'fn' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>    title="My Grok chat (xAI API)",

C:\Users\rubai>    description="Uses Grok via xAI's OpenAI-compatible API.",
'description' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\rubai>)
C:\Users\rubai>
C:\Users\rubai>if __name__ == "__main__":
The syntax of the command is incorrect.
C:\Users\rubai>    demo.launch()
1 Like

Python is one of the easiest programming languages to get started with, but since it is a programming language, it can be a tough journey if you don’t have some prior knowledge…
Personally, I recommend actively seeking help from Grok. If you just want to get it running, you should be able to manage it as long as you follow the steps correctly…


Your installs look fine. The issue is where the Python code is being executed.


1) What’s actually happening

In your screenshot/log you are at a prompt like:

C:\Users\rubai>

That is Command Prompt (CMD). CMD can only run Windows commands. It does not understand Python syntax.

So when you type Python statements like:

  • import os
  • from openai import OpenAI
  • def chat(...):
  • # comments

CMD tries to interpret them as Windows commands and replies with errors like:

'import' is not recognized as an internal or external command
'#' is not recognized as an internal or external command
The syntax of the command is incorrect. (Hugging Face Discussion)

This means the code itself is not “broken”. It just isn’t being run by Python yet.


2) The fix: put the Python code in a file, then run the file

Think of it like this:

  • In CMD you type commands (example: py app.py)
  • In a .py file you write Python code (example: import os)

Python on Windows is commonly run via py (the Python Launcher). The official docs show using py to run scripts and modules. (Python documentation)

Step A. Confirm Python is runnable

In CMD:

py --version

If that prints a version, continue.

(Reference: “Using Python on Windows”)
https://docs.python.org/3/using/windows.html (Python documentation)

Step B. Create a clean folder and (recommended) a virtual environment

In CMD:

mkdir grok_local
cd grok_local

py -m venv .venv
.venv\Scripts\activate

Virtual environments are part of Python’s standard workflow for isolating dependencies. (Python documentation)
https://docs.python.org/3/tutorial/venv.html (Python documentation)

Step C. Install the packages into that environment

py -m pip install --upgrade pip
py -m pip install gradio openai

Using py -m pip ensures you install into the same Python you run. Packaging docs recommend this pattern on Windows. (Python Packaging)
https://packaging.python.org/tutorials/installing-packages/ (Python Packaging)

Step D. Set your xAI key (important Windows detail)

You used:

setx XAI_API_KEY "..."

Important note: Microsoft’s docs state that variables set with setx are available in future command windows only, not the current one. (Microsoft Learn)

So you have two correct options:

Option 1: Set it for the current CMD window (simple and immediate)

set XAI_API_KEY=your_real_xai_key_here

set is the built-in command to display/set/remove cmd.exe environment variables. (Microsoft Learn)

Option 2: Use setx, then open a NEW CMD window

setx XAI_API_KEY "your_real_xai_key_here"

Then close CMD and open a new one before running the app. (Microsoft Learn)

Security note: API keys are secrets. Do not paste them into public posts. This is a standard practice across API providers. (OpenAI Platform)


3) Create app.py (this is where the Python code goes)

In the same folder (grok_local), create a file:

notepad app.py

Paste this code into app.py, save, close Notepad:

import os
import gradio as gr
from openai import OpenAI

def reply(message, history):
    api_key = os.getenv("XAI_API_KEY")
    if not api_key:
        return (
            "XAI_API_KEY is missing.\n\n"
            "If you used setx, open a NEW CMD window.\n"
            "Or set it for this window with:\n"
            "  set XAI_API_KEY=your_key_here"
        )

    # xAI is OpenAI-SDK compatible when you set base_url to https://api.x.ai/v1
    client = OpenAI(
        api_key=api_key,
        base_url="https://api.x.ai/v1",
    )

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

gr.ChatInterface(fn=reply, title="Local Grok chat (xAI API)").launch()

Why these lines matter:

  • base_url="https://api.x.ai/v1" is required to point the OpenAI-compatible client at xAI. xAI’s migration guide documents this directly. (xAI)
  • Gradio ChatInterface is the high-level class for creating a chatbot UI around a function. (Gradio)
  • The OpenAI Python library supports custom base_url in the client constructor, which is the same mechanism used for non-OpenAI backends. (GitHub)

References:


4) Run it (this is the only thing you run in CMD)

Back in CMD, run:

py app.py

If it starts, you should see a local URL printed (often http://127.0.0.1:7860). Open that in your browser.

Extra: Gradio also documents a local dev workflow where you can run with gradio app.py for hot reload, but py app.py is perfectly fine as a baseline. (Gradio)


5) Quick troubleshooting (copy/paste outputs, no secrets)

If it still fails, please paste the full text output of these commands (remove any secrets):

py --version
py -m pip --version
py -c "import gradio, openai; print('imports ok')"
set XAI_API_KEY
dir

Common causes:

  • Key not visible: you used setx but didn’t open a new CMD window. (Microsoft Learn)
  • Wrong environment: packages installed in one Python, running a different Python. Using py -m pip ... helps avoid that. (Python Packaging)
  • Code pasted into CMD again: all Python code must stay in app.py (or another .py file). Your original log shows CMD interpreting each Python line as a command. (Hugging Face Discussion)

6) How to get better answers from Grok (or any assistant) and from search engines

A) Use a “minimal reproducible” problem report

When asking for help, provide a minimal, complete, reproducible setup. Stack Overflow’s help center explains this clearly. (Stack Overflow)
https://stackoverflow.com/help/minimal-reproducible-example (Stack Overflow)

A good help request includes:

  • OS: Windows 10/11
  • Terminal prompt line (first line) e.g. C:\Users\...>
  • Exact commands you ran
  • Full error text (not a screenshot)
  • What you expected to happen
  • What happened instead
  • Confirmation that you did not paste secrets

B) Search using the exact error text

Copy one full error line, put it in quotes, add context words:

  • "import' is not recognized as an internal or external command" cmd python
  • "setx variables are available in future command windows only" Windows
  • "ModuleNotFoundError: gradio" venv windows

A classic guide on asking technical questions specifically recommends searching the text of the error message. (Catb)
https://www.catb.org/~esr/faqs/smart-questions.html (Catb)


7) Core references (organized)

Running Python on Windows

Virtual environments and packages

Environment variables on Windows CMD

Gradio chat UI

xAI Grok API connection (OpenAI-compatible)


If you follow the “write code in app.py → run py app.py” workflow, the exact CMD errors in your log disappear because the Python code is finally executed by Python (not by CMD). (Hugging Face Discussion)