eaglelandsonce commited on
Commit
352c49c
·
verified ·
1 Parent(s): 563e8bb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # --- Configuration ---
6
+ # Read from environment for safety. Set OPENAI_API_KEY before running.
7
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
8
+ if not OPENAI_API_KEY:
9
+ raise RuntimeError(
10
+ "Missing OPENAI_API_KEY. Set it in your environment: "
11
+ "export OPENAI_API_KEY='sk-...' (Linux/Mac) or "
12
+ "setx OPENAI_API_KEY \"sk-...\" (Windows)"
13
+ )
14
+
15
+ # Optional: override base URL if you use a proxy/self-hosted gateway.
16
+ # For the standard OpenAI endpoint you can omit base_url entirely.
17
+ client = OpenAI(
18
+ api_key=OPENAI_API_KEY,
19
+ base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
20
+ )
21
+
22
+ # Default system prompt and model; adjust as desired.
23
+ DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
24
+ DEFAULT_MODEL = os.getenv("OPENAI_MODEL", "gpt-5-chat-latest") # use a current chat model
25
+
26
+ def chat_fn(message, history, system_prompt, temperature, max_tokens):
27
+ """
28
+ Gradio ChatInterface passes:
29
+ - message: current user message (str)
30
+ - history: list[tuple[str,str]] of (user, assistant) turns
31
+ We convert to OpenAI's chat message format and call the Chat Completions API.
32
+ """
33
+ # Build messages: system + alternating user/assistant + latest user
34
+ messages = [{"role": "system", "content": system_prompt.strip() or DEFAULT_SYSTEM_PROMPT}]
35
+
36
+ for user_msg, assistant_msg in history:
37
+ if user_msg:
38
+ messages.append({"role": "user", "content": user_msg})
39
+ if assistant_msg:
40
+ messages.append({"role": "assistant", "content": assistant_msg})
41
+
42
+ messages.append({"role": "user", "content": message})
43
+
44
+ resp = client.chat.completions.create(
45
+ model=DEFAULT_MODEL,
46
+ messages=messages,
47
+ temperature=temperature,
48
+ max_tokens=max_tokens if max_tokens and max_tokens > 0 else None,
49
+ )
50
+
51
+ reply = resp.choices[0].message.content
52
+ return reply
53
+
54
+ with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
55
+ gr.Markdown("## OpenAI Chatbot\nEnter a message below to chat.")
56
+
57
+ with gr.Row():
58
+ system_prompt = gr.Textbox(
59
+ label="System prompt",
60
+ value=DEFAULT_SYSTEM_PROMPT,
61
+ lines=2
62
+ )
63
+
64
+ with gr.Row():
65
+ temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.1, label="Temperature")
66
+ max_tokens = gr.Slider(64, 4096, value=512, step=32, label="Max tokens (response)")
67
+
68
+ chat = gr.ChatInterface(
69
+ fn=lambda msg, hist: chat_fn(msg, hist, system_prompt.value, temperature.value, int(max_tokens.value)),
70
+ type="messages", # keeps history as list of tuples
71
+ title="OpenAI Chatbot",
72
+ retry_btn="Retry",
73
+ undo_btn="Remove last",
74
+ clear_btn="Clear",
75
+ submit_btn="Send"
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ # You can set server_name="0.0.0.0" if deploying in a container.
80
+ demo.launch()