Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import openai
|
5 |
+
|
6 |
+
# ─── CONFIG ────────────────────────────────────────────────────────────────────
|
7 |
+
KEY_FILE = "openai_api_key.txt"
|
8 |
+
MODEL = "gpt-4o-2024-05-13"
|
9 |
+
|
10 |
+
# Try to load a cached key (global for all users)
|
11 |
+
if os.path.exists(KEY_FILE):
|
12 |
+
with open(KEY_FILE, "r") as f:
|
13 |
+
DEFAULT_KEY = f.read().strip()
|
14 |
+
else:
|
15 |
+
DEFAULT_KEY = ""
|
16 |
+
|
17 |
+
# ─── HELPERS ───────────────────────────────────────────────────────────────────
|
18 |
+
|
19 |
+
def save_api_key(api_key: str) -> str:
|
20 |
+
"""Save the key to disk so it's remembered for all future sessions."""
|
21 |
+
with open(KEY_FILE, "w") as f:
|
22 |
+
f.write(api_key.strip())
|
23 |
+
return "✅ API key saved and will be used for all users."
|
24 |
+
|
25 |
+
def chat_with_openai(api_key: str, user_message: str, history: list) -> list:
|
26 |
+
"""Invoke GPT-4o with the supplied API key and append to chat history."""
|
27 |
+
openai.api_key = api_key.strip()
|
28 |
+
# ensure history is a list of tuples [(user, bot), ...]
|
29 |
+
history = history or []
|
30 |
+
# Build messages in OpenAI format
|
31 |
+
messages = []
|
32 |
+
for u, a in history:
|
33 |
+
messages.append({"role":"user", "content": u})
|
34 |
+
messages.append({"role":"assistant","content": a})
|
35 |
+
messages.append({"role":"user", "content": user_message})
|
36 |
+
# Call the API
|
37 |
+
resp = openai.ChatCompletion.create(
|
38 |
+
model=MODEL,
|
39 |
+
messages=messages,
|
40 |
+
)
|
41 |
+
answer = resp.choices[0].message.content
|
42 |
+
history.append((user_message, answer))
|
43 |
+
return history
|
44 |
+
|
45 |
+
# ─── UI LAYOUT ────────────────────────────────────────────────────────────────
|
46 |
+
|
47 |
+
with gr.Blocks(title="🌐 GPT-4o Multimodal (Skeleton)") as demo:
|
48 |
+
|
49 |
+
gr.Markdown(
|
50 |
+
"""
|
51 |
+
# 🤖 GPT-4o Client
|
52 |
+
Enter your OpenAI API key once below.
|
53 |
+
It will be cached on the server for all future sessions.
|
54 |
+
"""
|
55 |
+
)
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
api_key_input = gr.Textbox(
|
59 |
+
label="🔑 OpenAI API Key",
|
60 |
+
placeholder="sk-…",
|
61 |
+
value=DEFAULT_KEY,
|
62 |
+
type="password",
|
63 |
+
)
|
64 |
+
save_button = gr.Button("💾 Save API Key")
|
65 |
+
save_status = gr.Textbox(
|
66 |
+
label="Status",
|
67 |
+
interactive=False,
|
68 |
+
placeholder="–"
|
69 |
+
)
|
70 |
+
|
71 |
+
# Wire the save button
|
72 |
+
save_button.click(
|
73 |
+
fn=save_api_key,
|
74 |
+
inputs=api_key_input,
|
75 |
+
outputs=save_status,
|
76 |
+
)
|
77 |
+
|
78 |
+
gr.Markdown("---
|
79 |
+
|
80 |
+
## 💬 Chat with GPT-4o")
|
81 |
+
chatbot = gr.Chatbot(label="Chat History")
|
82 |
+
msg = gr.Textbox(
|
83 |
+
label="Your message",
|
84 |
+
placeholder="Type something and press Enter…",
|
85 |
+
)
|
86 |
+
|
87 |
+
# When you hit Enter in the textbox, call the chat fn
|
88 |
+
msg.submit(
|
89 |
+
fn=chat_with_openai,
|
90 |
+
inputs=[api_key_input, msg, chatbot],
|
91 |
+
outputs=chatbot,
|
92 |
+
)
|
93 |
+
|
94 |
+
# ─── RUN ───────────────────────────────────────────────────────────────────────
|
95 |
+
|
96 |
+
if __name__ == "__main__":
|
97 |
+
# Use 0.0.0.0 if you want external access; port can be adjusted as needed
|
98 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|