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

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