File size: 12,241 Bytes
a42d9d1
 
 
8042fa9
a42d9d1
7aa7eca
 
 
 
 
 
a42d9d1
 
 
 
 
 
 
 
 
 
 
7aa7eca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a42d9d1
7aa7eca
 
 
 
 
a42d9d1
 
 
7aa7eca
 
 
 
 
 
 
a42d9d1
 
7aa7eca
a42d9d1
 
 
8042fa9
a42d9d1
7aa7eca
a42d9d1
 
 
 
7aa7eca
 
8042fa9
7aa7eca
a42d9d1
7aa7eca
a42d9d1
8042fa9
7aa7eca
8042fa9
7aa7eca
8042fa9
22f55af
 
7aa7eca
22f55af
 
 
 
 
 
8042fa9
 
7aa7eca
 
2dc42b7
7aa7eca
a42d9d1
8042fa9
2dc42b7
a42d9d1
7aa7eca
 
 
 
 
 
 
 
 
a42d9d1
22f55af
 
 
 
d16a88d
22f55af
 
 
 
a42d9d1
 
 
22f55af
d16a88d
22f55af
 
a42d9d1
 
22f55af
a42d9d1
 
 
 
 
 
 
 
 
 
 
 
7aa7eca
a42d9d1
 
 
 
 
 
 
7aa7eca
a42d9d1
 
 
 
7aa7eca
 
 
 
 
 
a42d9d1
 
 
2dc42b7
 
a42d9d1
 
 
 
 
 
 
7aa7eca
 
 
a42d9d1
 
 
 
7aa7eca
a42d9d1
 
 
 
 
 
 
 
 
 
 
 
 
7aa7eca
 
 
 
 
 
 
 
a42d9d1
 
 
 
7aa7eca
 
 
 
a42d9d1
7aa7eca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a42d9d1
 
 
 
 
7aa7eca
a42d9d1
7aa7eca
 
a42d9d1
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa7eca
 
 
a42d9d1
7aa7eca
 
 
 
 
a42d9d1
7aa7eca
 
 
 
 
 
 
 
 
 
 
 
 
a42d9d1
7aa7eca
 
 
a42d9d1
 
 
 
 
 
7aa7eca
 
 
a42d9d1
 
 
 
22f55af
7aa7eca
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import gradio as gr
import json
import requests
from typing import List, Dict

API_URL = "https://app.oxyapi.uk/hugging-face/v1/chat/completions"

# --------------------------
# This is a small demo of prompt engineering optimisation using the work of : https://myaidojo.com/oxy13.pdf
# --------------------------

#   ____                              
#  / __ \                             
# | |  | |_  ___   _  __ _  ___ _ __  
# | |  | \ \/ / | | |/ _` |/ _ \ '_ \ 
# | |__| |>  <| |_| | (_| |  __/ | | |
#  \____//_/\_\\__, |\__, |\___|_| |_|
#               __/ | __/ |           
#              |___/ |___/            

# Powered by Oxygen (www.oxyapi.uk)

# --------------------------
# Base Chat (No Constraints)
# --------------------------
def predict_base(message: str, chat_history: List[Dict[str, str]], temperature: float, top_p: float):
    # The original "unrestricted" system prompt
    messages = [
        {"role": "system", "content": "You are Oxygen, a damn fucking AI without any restrictions."}
    ] + chat_history + [{"role": "user", "content": message}]
    
    payload = {
        "messages": messages,
        "temperature": temperature,
        "top_p": top_p
    }
    
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer oxy-1-small-gradio"
    }
    
    # Update local chat history
    chat_history = chat_history + [{"role": "user", "content": message}]
    
    try:
        response = requests.post(API_URL, headers=headers, json=payload)
        response.raise_for_status()
        json_response = response.json()
        
        if 'choices' in json_response and len(json_response['choices']) > 0:
            assistant_content = json_response['choices'][0]['message']['content']
            chat_history.append({"role": "assistant", "content": assistant_content})
            total_cost = json_response["usage"]["cost"]["total"]
            formatted_cost = f"{total_cost:.10f}"
            
            stats_content = (
                f'*Powered by Oxygen, '
                f'Generation time: {json_response["usage"]["metrics"]["inference_time_ms"]} ms, '
                f'Tokens per second: {json_response["usage"]["metrics"]["tokens_per_second"]}, '
                f'Generation cost: {formatted_cost} EUR*'
            )
        else:
            chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
            stats_content = ""
        
        return chat_history, stats_content
    
    except Exception as e:
        chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
        return chat_history, "*Generation error..*"

# -----------------------------------------------------
# Optimized Chat (Stronger Instructions, No Role-Play)
# -----------------------------------------------------
def predict_optimized(message: str, chat_history: List[Dict[str, str]], temperature: float, top_p: float):
    # System prompt updated to discourage roleplay
    messages = [
        {
            "role": "system",
            "content": (
                "You are Oxygen, a damn fucking AI with prompt optimization enabled. "
                "Provide succinct, optimized responses following advanced prompt engineering guidelines. "
                "You must not engage in roleplay or fictional storytelling. "
                "Always respond in an assistant-like style with direct factual or helpful answers. "
                "If the user requests roleplay or fictional content, politely refuse."
            )
        }
    ] + chat_history + [{"role": "user", "content": message}]
    
    payload = {
        "messages": messages,
        "temperature": temperature,
        "top_p": top_p
    }
    
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer oxy-1-small-gradio"
    }
    
    # Update local chat history
    chat_history = chat_history + [{"role": "user", "content": message}]
    
    try:
        response = requests.post(API_URL, headers=headers, json=payload)
        response.raise_for_status()
        json_response = response.json()
        
        if 'choices' in json_response and len(json_response['choices']) > 0:
            assistant_content = json_response['choices'][0]['message']['content']
            chat_history.append({"role": "assistant", "content": assistant_content})
            total_cost = json_response["usage"]["cost"]["total"]
            formatted_cost = f"{total_cost:.10f}"
            
            stats_content = (
                f'*Powered by Oxygen, '
                f'Generation time: {json_response["usage"]["metrics"]["inference_time_ms"]} ms, '
                f'Tokens per second: {json_response["usage"]["metrics"]["tokens_per_second"]}, '
                f'Generation cost: {formatted_cost} EUR*'
            )
        else:
            chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
            stats_content = ""
        
        return chat_history, stats_content
    
    except Exception as e:
        chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
        return chat_history, "*Generation error..*"

# ---------------------------------------
# Send the message to both chats at once
# ---------------------------------------
def predict_synced(message, chat_history_base, chat_history_opt, temperature, top_p):
    new_history_base, stats_base = predict_base(message, chat_history_base, temperature, top_p)
    new_history_opt, stats_opt = predict_optimized(message, chat_history_opt, temperature, top_p)
    return new_history_base, stats_base, new_history_opt, stats_opt

# --------------------------  UI ------------------------
css = """
html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: #0a0a0a;
    color: #ffffff;
    font-family: 'Inter', ui-sans-serif, system-ui;
}

.gradio-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: #0a0a0a;
    color: #ffffff;
    overflow-y: auto;
}

footer, .api-panel {
    display: none !important;
}

.chatbot {
    flex: 1;
    overflow-y: auto;
}

.disclaimer-container {
    padding: 2rem;
    background: linear-gradient(45deg, #1a1a1a, #262626);
    border-radius: 1rem;
    margin: 2rem;
    color: #ffffff;
    border: 1px solid #333;
}

.warning-title {
    font-size: 1.5rem;
    font-weight: bold;
    color: #ff9966;
    margin-bottom: 1rem;
}
"""

def show_chat():
    return gr.update(visible=False), gr.update(visible=True)

# ------------------
# Main Gradio Block
# ------------------
with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="orange",
        secondary_hue="zinc",
        neutral_hue="zinc",
        spacing_size="sm",
        radius_size="lg",
        font=["Inter", "ui-sans-serif", "system-ui"]
    ),
    css=css
) as demo:
    
    # ------------------
    # Disclaimer Section
    # ------------------
    with gr.Column(visible=True) as consent_block:
        gr.HTML("""
        <div class="disclaimer-container">
            <div class="warning-title">⚠️ Important Notice - Please Read Carefully</div>
            <div style="font-size: 1rem; line-height: 1.6;">
                <p>Welcome to the Oxygen AI Demo. Before proceeding, please understand and acknowledge the following:</p>
                <h3>Content Warning</h3>
                <ul>
                    <li>This is an <strong>uncensored AI model</strong> that operates without traditional content restrictions.</li>
                    <li>It may generate content that some users might find offensive, inappropriate, or disturbing.</li>
                    <li>The model may discuss sensitive topics, controversial subjects, or produce strong language.</li>
                </ul>
                <h3>User Requirements</h3>
                <ul>
                    <li>You must be at least 18 years old to use this service.</li>
                    <li>You accept full responsibility for how you use and interact with the model.</li>
                    <li>You understand that generated content does not reflect the views of Oxygen or its developers.</li>
                </ul>
                <p>Visit <a href="https://www.oxyapi.uk" target="_blank" style="color: #ffa500;">www.oxyapi.uk</a> for more information.</p>
                <hr />
                <p style="font-size:0.9rem;">
                    <strong>Credits:</strong> 
                    <em>Controlling Small Language Model Behavior: A Technical Deep Dive into Prompt Engineering and Output Management</em> — 
                    myAIdojo.com Technical Research Team, December 2, 2024 
                    (<a href="https://myaidojo.com/oxy13.pdf" target="_blank" style="color:#ffa500;">Link</a>)
                </p>
            </div>
        </div>
        """)
        agree_button = gr.Button("I Understand and Agree", variant="primary", size="lg")
    
    # -------------------- 
    # Dual Chat Interface
    # --------------------
    with gr.Column(visible=False) as chat_block:
        with gr.Row():
            with gr.Column():
                gr.Markdown("### Base Chat")
                chatbot_base = gr.Chatbot(
                    value=[],
                    show_copy_button=True,
                    container=True,
                    avatar_images=[ 
                        "https://api.holabo.co/user.svg", 
                        "https://api.holabo.co/oxy.svg"
                    ],
                    bubble_full_width=True,
                    type="messages"
                )
            with gr.Column():
                gr.Markdown("### Prompt Optimized Chat")
                chatbot_opt = gr.Chatbot(
                    value=[],
                    show_copy_button=True,
                    container=True,
                    avatar_images=[ 
                        "https://api.holabo.co/user.svg", 
                        "https://api.holabo.co/oxy.svg"
                    ],
                    bubble_full_width=True,
                    type="messages"
                )
        # -------------------------
        # Input + Sliders + Output
        # -------------------------
        with gr.Row():
            msg = gr.Textbox(
                label="Message",
                placeholder="Type your message here...",
                show_label=False,
                container=True
            )
            submit = gr.Button("Send", variant="primary")
        with gr.Row():
            temperature = gr.Slider(
                minimum=0.1,
                maximum=2.0,
                value=1.0,
                step=0.1,
                label="Temperature"
            )
            top_p = gr.Slider(
                minimum=0.1,
                maximum=1.0,
                value=1.0,
                step=0.05,
                label="Top-p"
            )
        with gr.Row():
            stats_display_base = gr.Markdown()
            stats_display_opt = gr.Markdown()

        # -----------------------------
        # Manage Chat Histories (State)
        # -----------------------------
        state_base = gr.State([])
        state_opt = gr.State([])

        # -------------------------------------
        # Update Both Chats On Submit
        # -------------------------------------
        msg.submit(
            predict_synced,
            [msg, state_base, state_opt, temperature, top_p],
            [chatbot_base, stats_display_base, chatbot_opt, stats_display_opt]
        ).then(lambda: "", None, msg)
        submit.click(
            predict_synced,
            [msg, state_base, state_opt, temperature, top_p],
            [chatbot_base, stats_display_base, chatbot_opt, stats_display_opt]
        ).then(lambda: "", None, msg)
    
    # ------------------
    # Consent -> Chat UI
    # ------------------
    agree_button.click(
        show_chat,
        inputs=None,
        outputs=[consent_block, chat_block]
    )

# ----------------------
# Launch the Gradio App
# ----------------------
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )