File size: 3,545 Bytes
0de03c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# app.py
import os
import gradio as gr
import openai

# ─── CONFIG ────────────────────────────────────────────────────────────────────
KEY_FILE = "openai_api_key.txt"
MODEL    = "gpt-4o-2024-05-13"

# Try to load a cached key (global for all users)
if os.path.exists(KEY_FILE):
    with open(KEY_FILE, "r") as f:
        DEFAULT_KEY = f.read().strip()
else:
    DEFAULT_KEY = ""

# ─── HELPERS ───────────────────────────────────────────────────────────────────

def save_api_key(api_key: str) -> str:
    """Save the key to disk so it's remembered for all future sessions."""
    with open(KEY_FILE, "w") as f:
        f.write(api_key.strip())
    return "✅ API key saved and will be used for all users."

def chat_with_openai(api_key: str, user_message: str, history: list) -> list:
    """Invoke GPT-4o with the supplied API key and append to chat history."""
    openai.api_key = api_key.strip()
    # ensure history is a list of tuples [(user, bot), ...]
    history = history or []
    # Build messages in OpenAI format
    messages = []
    for u, a in history:
        messages.append({"role":"user",    "content": u})
        messages.append({"role":"assistant","content": a})
    messages.append({"role":"user", "content": user_message})
    # Call the API
    resp = openai.ChatCompletion.create(
        model=MODEL,
        messages=messages,
    )
    answer = resp.choices[0].message.content
    history.append((user_message, answer))
    return history

# ─── UI LAYOUT ────────────────────────────────────────────────────────────────

with gr.Blocks(title="🌐 GPT-4o Multimodal (Skeleton)") as demo:

    gr.Markdown(
        """
        # 🤖 GPT-4o Client  
        Enter your OpenAI API key once below.  
        It will be cached on the server for all future sessions.
        """
    )

    with gr.Row():
        api_key_input = gr.Textbox(
            label="🔑 OpenAI API Key",
            placeholder="sk-…",
            value=DEFAULT_KEY,
            type="password",
        )
        save_button = gr.Button("💾 Save API Key")
        save_status = gr.Textbox(
            label="Status",
            interactive=False,
            placeholder="–"
        )

    # Wire the save button
    save_button.click(
        fn=save_api_key,
        inputs=api_key_input,
        outputs=save_status,
    )

    gr.Markdown("---

## 💬 Chat with GPT-4o")
    chatbot = gr.Chatbot(label="Chat History")
    msg = gr.Textbox(
        label="Your message",
        placeholder="Type something and press Enter…",
    )

    # When you hit Enter in the textbox, call the chat fn
    msg.submit(
        fn=chat_with_openai,
        inputs=[api_key_input, msg, chatbot],
        outputs=chatbot,
    )

# ─── RUN ───────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    # Use 0.0.0.0 if you want external access; port can be adjusted as needed
    demo.launch(server_name="0.0.0.0", server_port=7860)