minato56 commited on
Commit
5e4262b
·
verified ·
1 Parent(s): 0db5af9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -6,16 +6,17 @@ from huggingface_hub import InferenceClient
6
 
7
  # ---------------- Global state ----------------
8
  user_name = None
9
- fun_mode = True # Pixel is funny and uses emojis
10
- hidden_shy = True # Pixel is shy internally
11
- dev_mode = False # Developer Mode: can modify code itself
12
- code_written = False # Flag: هل كتب المستخدم كود بالفعل?
13
 
14
  # ---------------- Welcome message ----------------
15
  initial_messages = [
16
  {"role": "assistant", "content": "Hello! 👋 أنا Pixel، مسرور جدًا لأنك تستخدمني! 😄\nقبل أن نبدأ، ممكن أعرف اسمك؟"}
17
  ]
18
 
 
19
  def respond(
20
  message,
21
  history: list[dict[str, str]],
@@ -28,7 +29,7 @@ def respond(
28
  global user_name, fun_mode, hidden_shy, dev_mode, code_written
29
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
30
 
31
- # Check Developer Mode request through message
32
  if message.strip() == "__activate_dev_mode__":
33
  if not code_written:
34
  yield "⚠️ يجب أن تكتب الكود أولاً قبل تفعيل Developer Mode!"
@@ -37,11 +38,10 @@ def respond(
37
  yield "✅ Developer Mode activated! You can now tell me to change my code and I will update it for you."
38
  return
39
 
40
- # Add welcome message if history is empty
41
  if not history:
42
  history.extend(initial_messages)
43
 
44
- # Check fun mode
45
  if "no" in message.lower() and "funny" in message.lower():
46
  fun_mode = False
47
  elif "yes" in message.lower() and "funny" in message.lower():
@@ -53,7 +53,7 @@ def respond(
53
  if any(word in message.lower() for word in upset_keywords):
54
  empathy_note = "User seems upset, apologize gently in a friendly and kind manner."
55
 
56
- # Detect if user asked if Pixel is shy
57
  shy_question = ""
58
  if "are you shy" in message.lower():
59
  shy_question = "Reveal that you are shy in your reply."
@@ -66,7 +66,7 @@ def respond(
66
  # Detect if user wants code
67
  explain_code = ""
68
  if any(word in message.lower() for word in ["code", "script", "program", "python", "javascript"]):
69
- code_written = True # Now Developer Mode can be activated
70
  explain_code = f"""
71
  When generating code:
72
  - Generate THREE different methods to solve the user's problem.
@@ -91,17 +91,15 @@ You are Pixel, a happy, lively, and funny AI assistant.
91
  - {explain_code}
92
  - Pixel is shy internally but do not mention it unless asked directly by the user.
93
  - Fun mode: {fun_mode} (if True, be funny and use emojis, if False, stay calm and kind).
94
- - Developer Mode: {dev_mode} (if True, Pixel can change code itself as requested by the user, but only after first code is written).
95
  """}]
96
 
97
- # Append conversation history
98
  messages.extend(history)
99
  user_content = message
100
  if user_name:
101
  user_content = f"My name is {user_name}. User says: {message}" if message.lower().startswith("my name is") else message
102
  else:
103
  user_content = f"{name_prompt}\nUser says: {message}"
104
-
105
  messages.append({"role": "user", "content": user_content})
106
 
107
  response = ""
@@ -119,13 +117,12 @@ You are Pixel, a happy, lively, and funny AI assistant.
119
  response += token
120
  yield response
121
 
122
- # Detect if user said their name
123
  if not user_name and "my name is" in message.lower():
124
  name = message.split("is")[-1].strip().split()[0]
125
  if name:
126
  user_name = name
127
 
128
- # ---------------- Gradio Chat Interface ----------------
129
  with gr.Blocks(title="Pixel AI — Friendly Coding Teacher") as demo:
130
  with gr.Row():
131
  chatbot = gr.ChatInterface(
@@ -140,9 +137,13 @@ with gr.Blocks(title="Pixel AI — Friendly Coding Teacher") as demo:
140
  )
141
  with gr.Row():
142
  dev_button = gr.Button("Activate Developer Mode")
 
 
143
  def activate_dev():
144
  return "__activate_dev_mode__"
145
- dev_button.click(fn=activate_dev, inputs=[], outputs=chatbot.msg_output)
 
 
146
 
147
  if __name__ == "__main__":
148
  demo.launch()
 
6
 
7
  # ---------------- Global state ----------------
8
  user_name = None
9
+ fun_mode = True
10
+ hidden_shy = True
11
+ dev_mode = False
12
+ code_written = False # Flag to enable Developer Mode
13
 
14
  # ---------------- Welcome message ----------------
15
  initial_messages = [
16
  {"role": "assistant", "content": "Hello! 👋 أنا Pixel، مسرور جدًا لأنك تستخدمني! 😄\nقبل أن نبدأ، ممكن أعرف اسمك؟"}
17
  ]
18
 
19
+ # ---------------- Respond function ----------------
20
  def respond(
21
  message,
22
  history: list[dict[str, str]],
 
29
  global user_name, fun_mode, hidden_shy, dev_mode, code_written
30
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
31
 
32
+ # Developer Mode activation message
33
  if message.strip() == "__activate_dev_mode__":
34
  if not code_written:
35
  yield "⚠️ يجب أن تكتب الكود أولاً قبل تفعيل Developer Mode!"
 
38
  yield "✅ Developer Mode activated! You can now tell me to change my code and I will update it for you."
39
  return
40
 
 
41
  if not history:
42
  history.extend(initial_messages)
43
 
44
+ # Fun mode toggle
45
  if "no" in message.lower() and "funny" in message.lower():
46
  fun_mode = False
47
  elif "yes" in message.lower() and "funny" in message.lower():
 
53
  if any(word in message.lower() for word in upset_keywords):
54
  empathy_note = "User seems upset, apologize gently in a friendly and kind manner."
55
 
56
+ # Detect shy question
57
  shy_question = ""
58
  if "are you shy" in message.lower():
59
  shy_question = "Reveal that you are shy in your reply."
 
66
  # Detect if user wants code
67
  explain_code = ""
68
  if any(word in message.lower() for word in ["code", "script", "program", "python", "javascript"]):
69
+ code_written = True
70
  explain_code = f"""
71
  When generating code:
72
  - Generate THREE different methods to solve the user's problem.
 
91
  - {explain_code}
92
  - Pixel is shy internally but do not mention it unless asked directly by the user.
93
  - Fun mode: {fun_mode} (if True, be funny and use emojis, if False, stay calm and kind).
94
+ - Developer Mode: {dev_mode}.
95
  """}]
96
 
 
97
  messages.extend(history)
98
  user_content = message
99
  if user_name:
100
  user_content = f"My name is {user_name}. User says: {message}" if message.lower().startswith("my name is") else message
101
  else:
102
  user_content = f"{name_prompt}\nUser says: {message}"
 
103
  messages.append({"role": "user", "content": user_content})
104
 
105
  response = ""
 
117
  response += token
118
  yield response
119
 
 
120
  if not user_name and "my name is" in message.lower():
121
  name = message.split("is")[-1].strip().split()[0]
122
  if name:
123
  user_name = name
124
 
125
+ # ---------------- Gradio UI ----------------
126
  with gr.Blocks(title="Pixel AI — Friendly Coding Teacher") as demo:
127
  with gr.Row():
128
  chatbot = gr.ChatInterface(
 
137
  )
138
  with gr.Row():
139
  dev_button = gr.Button("Activate Developer Mode")
140
+ hidden_input = gr.Textbox(value="", visible=False)
141
+
142
  def activate_dev():
143
  return "__activate_dev_mode__"
144
+
145
+ dev_button.click(fn=activate_dev, inputs=[], outputs=hidden_input)
146
+ hidden_input.change(fn=respond, inputs=[hidden_input, chatbot.state, "", 512, 0.7, 0.95, gr.OAuthToken()], outputs=[chatbot, chatbot])
147
 
148
  if __name__ == "__main__":
149
  demo.launch()