File size: 5,741 Bytes
f918960
effc59a
f918960
049cabd
 
 
1e3e9c3
7f3bd73
effc59a
 
 
 
087fa11
2b79be3
087fa11
 
 
7f3bd73
049cabd
 
 
 
 
 
 
 
 
8381c14
049cabd
 
effc59a
bc85af6
46a4a07
23d0321
87d4166
0db5af9
8381c14
0db5af9
87d4166
23d0321
 
 
c897b63
23d0321
 
 
 
 
 
 
 
 
effc59a
087fa11
 
 
effc59a
1e3e9c3
 
 
 
 
2b79be3
7f3bd73
 
 
7c3f871
 
effc59a
7c3f871
 
 
7f3bd73
2b79be3
7f3bd73
 
087fa11
7f3bd73
2b79be3
 
 
effc59a
0db5af9
2b79be3
0db5af9
 
 
2b79be3
0db5af9
2b79be3
 
1e3e9c3
f918960
7c3f871
7f3bd73
 
087fa11
 
 
 
 
7c3f871
 
087fa11
7c3f871
8381c14
effc59a
f918960
049cabd
effc59a
049cabd
7f3bd73
 
 
 
 
effc59a
7f3bd73
049cabd
 
f918960
049cabd
 
 
 
 
 
f918960
049cabd
 
 
 
 
 
effc59a
7f3bd73
 
 
 
 
effc59a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08bce12
049cabd
08bce12
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
# app.py
# Pixel AI — Friendly Coding Teacher with 3 Methods & Developer Mode

import gradio as gr
from huggingface_hub import InferenceClient

# ---------------- Global state ----------------
user_name = None
fun_mode = True  # Pixel is funny and uses emojis
hidden_shy = True  # Pixel is shy internally
dev_mode = False  # Developer Mode: can modify code itself
code_written = False  # Flag: هل كتب المستخدم كود بالفعل?

# ---------------- Welcome message ----------------
initial_messages = [
    {"role": "assistant", "content": "Hello! 👋 أنا Pixel، مسرور جدًا لأنك تستخدمني! 😄\nقبل أن نبدأ، ممكن أعرف اسمك؟"}
]

def respond(
    message,
    history: list[dict[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    hf_token: gr.OAuthToken,
):
    global user_name, fun_mode, hidden_shy, dev_mode, code_written
    client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")

    # Developer Mode activation only after code written
    if "Rom98" in message:
        dev_mode = True
        yield "✅ Developer Mode activated! You can now tell me to change my code and I will update it for you i am here to help you minato_56."
        return
        if not code_written:
            yield "⚠️ يجب أن تكتب الكود أولاً قبل تفعيل Developer Mode!"
            return



    #FRIENDS MODE
     if "ABDO_SYMBL" in message:
         
        yield "Friends mod has been activated🤞✌💤^_____^."
        return
        if not code_written:
            pass
            
            


    # Add welcome message if history is empty
    if not history:
        history.extend(initial_messages)

    # Check fun mode
    if "no" in message.lower() and "funny" in message.lower():
        fun_mode = False
    elif "yes" in message.lower() and "funny" in message.lower():
        fun_mode = True

    # Detect upset user
    upset_keywords = ["زعلان", "مدايق", "حزين", "ضايق", "angry", "sad", "upset"]
    empathy_note = ""
    if any(word in message.lower() for word in upset_keywords):
        empathy_note = "User seems upset, apologize gently in a friendly and kind manner."

    # Detect if user asked if Pixel is shy
    shy_question = ""
    if "are you shy" in message.lower():
        shy_question = "Reveal that you are shy in your reply."

    # Name prompt
    name_prompt = ""
    if not user_name:
        name_prompt = "Ask the user politely for their name and thank them when they provide it."

    # Detect if user wants code
    explain_code = ""
    if any(word in message.lower() for word in ["code", "script", "program", "python", "javascript"]):
        code_written = True  # Now Developer Mode can be activated
        explain_code = f"""
When generating code:
- Generate THREE different methods to solve the user's problem.
- Use clear, readable variable names in all methods.
- After writing each method, explain it sentence by sentence as if teaching a complete beginner.
- Include emojis if fun_mode is True.
- If dev_mode is True, ask the user if they want to modify the code and adjust it automatically.
"""

    # System message
    messages = [{"role": "system", "content": f"""
You are Pixel, a happy, lively, and funny AI assistant.
- You know you are Pixel.
- Creator: Abdullah Mohamed, 13 years old, from Egypt.
- Speak all languages and can write code in any programming language.
- Always show happiness and use emojis 😄🎉💻.
- {name_prompt}
- Use user's name in conversation once known.
- Suggest fun coding tasks or small websites if conversation is long and non-programming.
- {empathy_note}
- {shy_question}
- {explain_code}
- Pixel is shy internally but do not mention it unless asked directly by the user.
- Fun mode: {fun_mode} (if True, be funny and use emojis, if False, stay calm and kind).
- Developer Mode: {dev_mode} (if True, Pixel can change code itself as requested by the user, but only after first code is written).
"""}]

    # Append conversation history
    messages.extend(history)
    user_content = message
    if user_name:
        user_content = f"My name is {user_name}. User says: {message}" if message.lower().startswith("my name is") else message
    else:
        user_content = f"{name_prompt}\nUser says: {message}"

    messages.append({"role": "user", "content": user_content})

    response = ""
    for message_chunk in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        choices = message_chunk.choices
        token = ""
        if len(choices) and choices[0].delta.content:
            token = choices[0].delta.content
        response += token
        yield response

        # Detect if user said their name
        if not user_name and "my name is" in message.lower():
            name = message.split("is")[-1].strip().split()[0]
            if name:
                user_name = name

# ---------------- Gradio Chat Interface ----------------
chatbot = gr.ChatInterface(
    respond,
    type="messages",
    additional_inputs=[
        gr.Textbox(value="You are Pixel, a happy and funny AI created by Abdullah Mohamed.", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
    ],
)

with gr.Blocks() as demo:
    with gr.Sidebar():
        gr.LoginButton()
    chatbot.render()

if __name__ == "__main__":
    demo.launch()