Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,21 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# Pixel AI — Friendly Coding Teacher with 3 Methods & Developer Mode
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 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
|
| 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,8 +28,8 @@ def respond(
|
|
| 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
|
| 33 |
-
if
|
| 34 |
if not code_written:
|
| 35 |
yield "⚠️ يجب أن تكتب الكود أولاً قبل تفعيل Developer Mode!"
|
| 36 |
return
|
|
@@ -38,10 +37,11 @@ def respond(
|
|
| 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 |
-
#
|
| 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
|
| 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
|
| 70 |
explain_code = f"""
|
| 71 |
When generating code:
|
| 72 |
- Generate THREE different methods to solve the user's problem.
|
|
@@ -91,15 +91,17 @@ 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}.
|
| 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,35 +119,28 @@ You are Pixel, a happy, lively, and funny AI assistant.
|
|
| 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
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
# عند الضغط على الزر، نرسل "__activate_dev_mode__" إلى Textbox المخفي
|
| 143 |
-
def activate_dev():
|
| 144 |
-
return "__activate_dev_mode__"
|
| 145 |
-
|
| 146 |
-
dev_button.click(fn=activate_dev, inputs=[], outputs=hidden_input)
|
| 147 |
-
# إرسال Textbox ��لمخفي إلى ChatInterface عبر submit
|
| 148 |
-
hidden_input.submit(fn=lambda x: x, inputs=[hidden_input], outputs=[chatbot])
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# Pixel AI — Friendly Coding Teacher with 3 Methods & Developer Mode
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
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 |
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 |
+
# Developer Mode activation only after code written
|
| 32 |
+
if "deV_mod" in message:
|
| 33 |
if not code_written:
|
| 34 |
yield "⚠️ يجب أن تكتب الكود أولاً قبل تفعيل Developer Mode!"
|
| 35 |
return
|
|
|
|
| 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 |
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 |
# 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 |
- {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 |
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 |
+
chatbot = gr.ChatInterface(
|
| 130 |
+
respond,
|
| 131 |
+
type="messages",
|
| 132 |
+
additional_inputs=[
|
| 133 |
+
gr.Textbox(value="You are Pixel, a happy and funny AI created by Abdullah Mohamed.", label="System message"),
|
| 134 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 135 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 136 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 137 |
+
],
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
with gr.Blocks() as demo:
|
| 141 |
+
with gr.Sidebar():
|
| 142 |
+
gr.LoginButton()
|
| 143 |
+
chatbot.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
demo.launch()
|