eaglelandsonce commited on
Commit
bc82d3a
Β·
verified Β·
1 Parent(s): 03436df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -4,17 +4,18 @@ from openai import OpenAI
4
  DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
5
  DEFAULT_MODEL = "gpt-5-chat-latest"
6
 
7
- def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, base_url):
8
  """
9
- Main chat function. Uses the API key typed into the interface.
10
  """
11
  if not api_key:
12
- return "⚠️ Please provide an OpenAI API key in the input box above."
13
 
14
- client = OpenAI(api_key=api_key, base_url=base_url or "https://api.openai.com/v1")
 
15
 
16
- # Build messages for chat completion
17
- messages = [{"role": "system", "content": system_prompt.strip() or DEFAULT_SYSTEM_PROMPT}]
18
  for user_msg, assistant_msg in history:
19
  if user_msg:
20
  messages.append({"role": "user", "content": user_msg})
@@ -24,40 +25,44 @@ def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, b
24
 
25
  try:
26
  resp = client.chat.completions.create(
27
- model=DEFAULT_MODEL,
28
  messages=messages,
29
- temperature=temperature,
30
- max_tokens=max_tokens if max_tokens and max_tokens > 0 else None,
31
  )
32
  return resp.choices[0].message.content
33
  except Exception as e:
34
- return f"❌ Error: {str(e)}"
 
35
 
36
  with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
37
- gr.Markdown("## πŸ”‘ OpenAI Chatbot\nEnter your API key and start chatting.")
38
 
39
  with gr.Row():
40
  api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
41
  base_url = gr.Textbox(label="Base URL", value="https://api.openai.com/v1", lines=1)
42
 
43
  with gr.Row():
44
- system_prompt = gr.Textbox(label="System prompt", value=DEFAULT_SYSTEM_PROMPT, lines=2)
 
 
45
 
46
  with gr.Row():
47
- temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.1, label="Temperature")
48
- max_tokens = gr.Slider(64, 4096, value=512, step=32, label="Max tokens (response)")
 
 
49
 
50
  chat = gr.ChatInterface(
51
- fn=lambda msg, hist: chat_fn(msg, hist, system_prompt.value,
52
- temperature.value, int(max_tokens.value),
53
- api_key.value, base_url.value),
54
- type="messages",
55
  title="OpenAI Chatbot",
56
  retry_btn="Retry",
57
  undo_btn="Remove last",
58
  clear_btn="Clear",
59
- submit_btn="Send"
 
60
  )
61
 
62
  if __name__ == "__main__":
 
63
  demo.launch()
 
4
  DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
5
  DEFAULT_MODEL = "gpt-5-chat-latest"
6
 
7
+ def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, base_url, model):
8
  """
9
+ Gradio passes (message, history, *additional_inputs).
10
  """
11
  if not api_key:
12
+ return "⚠️ Please provide an OpenAI API key (top of the page)."
13
 
14
+ # Build client with the provided key & base URL
15
+ client = OpenAI(api_key=api_key, base_url=(base_url or "https://api.openai.com/v1").strip())
16
 
17
+ # Build messages
18
+ messages = [{"role": "system", "content": (system_prompt or DEFAULT_SYSTEM_PROMPT).strip()}]
19
  for user_msg, assistant_msg in history:
20
  if user_msg:
21
  messages.append({"role": "user", "content": user_msg})
 
25
 
26
  try:
27
  resp = client.chat.completions.create(
28
+ model=(model or DEFAULT_MODEL).strip(),
29
  messages=messages,
30
+ temperature=float(temperature) if temperature is not None else 0.7,
31
+ max_tokens=int(max_tokens) if max_tokens else None,
32
  )
33
  return resp.choices[0].message.content
34
  except Exception as e:
35
+ # Surface API errors in the UI for easy debugging
36
+ return f"❌ Error: {e}"
37
 
38
  with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
39
+ gr.Markdown("## πŸ”‘ OpenAI Chatbot\nPaste your API key, pick a model, and chat.")
40
 
41
  with gr.Row():
42
  api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
43
  base_url = gr.Textbox(label="Base URL", value="https://api.openai.com/v1", lines=1)
44
 
45
  with gr.Row():
46
+ model = gr.Textbox(label="Model", value=DEFAULT_MODEL, lines=1)
47
+
48
+ system_prompt = gr.Textbox(label="System prompt", value=DEFAULT_SYSTEM_PROMPT, lines=2)
49
 
50
  with gr.Row():
51
+ temperature = gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature")
52
+ max_tokens = gr.Slider(minimum=64, maximum=4096, value=512, step=32, label="Max tokens (response)")
53
+
54
+ gr.Markdown("---")
55
 
56
  chat = gr.ChatInterface(
57
+ fn=chat_fn,
 
 
 
58
  title="OpenAI Chatbot",
59
  retry_btn="Retry",
60
  undo_btn="Remove last",
61
  clear_btn="Clear",
62
+ submit_btn="Send",
63
+ additional_inputs=[system_prompt, temperature, max_tokens, api_key, base_url, model],
64
  )
65
 
66
  if __name__ == "__main__":
67
+ # Set share=True if you need a public URL while testing
68
  demo.launch()