Spaces:
Sleeping
Sleeping
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() | |