File size: 2,268 Bytes
d45701f
60e794c
 
d45701f
 
 
 
 
 
60e794c
d45701f
60e794c
 
d45701f
 
60e794c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d45701f
60e794c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
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()