ChatBot / app.py
eaglelandsonce's picture
Update app.py
76b1144 verified
raw
history blame
2.25 kB
# app.py
import gradio as gr
from openai import OpenAI
DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
DEFAULT_MODEL = "gpt-5-chat-latest"
def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, base_url, model):
if not api_key:
return "⚠️ Please provide an OpenAI API key (top of the page)."
client = OpenAI(api_key=api_key, base_url=(base_url or "https://api.openai.com/v1").strip())
messages = [{"role": "system", "content": (system_prompt or DEFAULT_SYSTEM_PROMPT).strip()}]
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})
try:
resp = client.chat.completions.create(
model=(model or DEFAULT_MODEL).strip(),
messages=messages,
temperature=float(temperature) if temperature is not None else 0.7,
max_tokens=int(max_tokens) if max_tokens else None,
)
return resp.choices[0].message.content
except Exception as e:
return f"❌ Error: {e}"
with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
gr.Markdown("## πŸ”‘ OpenAI Chatbot\nPaste your API key, pick a model, and chat.")
with gr.Row():
api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
base_url = gr.Textbox(label="Base URL", value="https://api.openai.com/v1", lines=1)
with gr.Row():
model = gr.Textbox(label="Model", value=DEFAULT_MODEL, lines=1)
system_prompt = gr.Textbox(label="System prompt", value=DEFAULT_SYSTEM_PROMPT, lines=2)
with gr.Row():
temperature = gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature")
max_tokens = gr.Slider(minimum=64, maximum=4096, value=512, step=32, label="Max tokens (response)")
gr.Markdown("---")
gr.ChatInterface(
fn=chat_fn,
title="OpenAI Chatbot",
additional_inputs=[system_prompt, temperature, max_tokens, api_key, base_url, model],
)
if __name__ == "__main__":
demo.launch()