""" Gradio Space: ask OpenAI Chat Completions in two boring ways — raw HTTP (`requests`) and Hugging Face (`huggingface_hub.InferenceClient`). Put OPENAI_API_KEY in Space secrets. """ import os import gradio as gr import requests from huggingface_hub import InferenceClient # Pick any model name your OpenAI key is allowed to call. MODEL = "gpt-5-nano" def ask_with_requests(question: str) -> str: """ Path 1: build the HTTP request yourself. - URL + JSON body match OpenAI's REST docs for Chat Completions. - `Authorization: Bearer …` is how OpenAI expects the API key. """ key = os.environ["OPENAI_API_KEY"] response = requests.post( "https://api.openai.com/v1/chat/completions", headers={ "Authorization": f"Bearer {key}", "Content-Type": "application/json", }, json={ "model": MODEL, "messages": [{"role": "user", "content": question}], }, timeout=120, ) response.raise_for_status() data = response.json() return data["choices"][0]["message"]["content"] def ask_with_huggingface_hub(question: str) -> str: """ Path 2: let `huggingface_hub` speak OpenAI-style Chat Completions. `InferenceClient.chat.completions.create(...)` is documented to follow the same idea as OpenAI's SDK; here we aim it at OpenAI by setting `base_url` to OpenAI's `/v1` root. """ client = InferenceClient( base_url="https://api.openai.com/v1", api_key=os.environ["OPENAI_API_KEY"], ) completion = client.chat.completions.create( model=MODEL, messages=[{"role": "user", "content": question}], ) return completion.choices[0].message.content demo = gr.TabbedInterface( [ gr.Interface( fn=ask_with_requests, inputs=gr.Textbox(label="Question", lines=3), outputs=gr.Textbox(label="Answer", lines=12), ), gr.Interface( fn=ask_with_huggingface_hub, inputs=gr.Textbox(label="Question", lines=3), outputs=gr.Textbox(label="Answer", lines=12), ), ], tab_names=["requests", "huggingface_hub"], title="Ask OpenAI (two simple clients)", ) demo.launch()