File size: 2,887 Bytes
352c49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
import os
import gradio as gr
from openai import OpenAI

# --- Configuration ---
# Read from environment for safety. Set OPENAI_API_KEY before running.
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
    raise RuntimeError(
        "Missing OPENAI_API_KEY. Set it in your environment: "
        "export OPENAI_API_KEY='sk-...' (Linux/Mac) or "
        "setx OPENAI_API_KEY \"sk-...\" (Windows)"
    )

# Optional: override base URL if you use a proxy/self-hosted gateway.
# For the standard OpenAI endpoint you can omit base_url entirely.
client = OpenAI(
    api_key=OPENAI_API_KEY,
    base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
)

# Default system prompt and model; adjust as desired.
DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
DEFAULT_MODEL = os.getenv("OPENAI_MODEL", "gpt-5-chat-latest")  # use a current chat model

def chat_fn(message, history, system_prompt, temperature, max_tokens):
    """
    Gradio ChatInterface passes:
      - message: current user message (str)
      - history: list[tuple[str,str]] of (user, assistant) turns
    We convert to OpenAI's chat message format and call the Chat Completions API.
    """
    # Build messages: system + alternating user/assistant + latest user
    messages = [{"role": "system", "content": system_prompt.strip() or DEFAULT_SYSTEM_PROMPT}]

    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})

    messages.append({"role": "user", "content": message})

    resp = client.chat.completions.create(
        model=DEFAULT_MODEL,
        messages=messages,
        temperature=temperature,
        max_tokens=max_tokens if max_tokens and max_tokens > 0 else None,
    )

    reply = resp.choices[0].message.content
    return reply

with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
    gr.Markdown("## OpenAI Chatbot\nEnter a message below to chat.")

    with gr.Row():
        system_prompt = gr.Textbox(
            label="System prompt",
            value=DEFAULT_SYSTEM_PROMPT,
            lines=2
        )

    with gr.Row():
        temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.1, label="Temperature")
        max_tokens = gr.Slider(64, 4096, value=512, step=32, label="Max tokens (response)")

    chat = gr.ChatInterface(
        fn=lambda msg, hist: chat_fn(msg, hist, system_prompt.value, temperature.value, int(max_tokens.value)),
        type="messages",    # keeps history as list of tuples
        title="OpenAI Chatbot",
        retry_btn="Retry",
        undo_btn="Remove last",
        clear_btn="Clear",
        submit_btn="Send"
    )

if __name__ == "__main__":
    # You can set server_name="0.0.0.0" if deploying in a container.
    demo.launch()