Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# Pixel AI — Friendly Coding Teacher with
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
|
@@ -8,6 +8,8 @@ from huggingface_hub import InferenceClient
|
|
| 8 |
user_name = None
|
| 9 |
fun_mode = True # Pixel is funny and uses emojis
|
| 10 |
hidden_shy = True # Pixel is shy internally
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ---------------- Welcome message ----------------
|
| 13 |
initial_messages = [
|
|
@@ -23,9 +25,18 @@ def respond(
|
|
| 23 |
top_p,
|
| 24 |
hf_token: gr.OAuthToken,
|
| 25 |
):
|
| 26 |
-
global user_name, fun_mode, hidden_shy
|
| 27 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Add welcome message if history is empty
|
| 30 |
if not history:
|
| 31 |
history.extend(initial_messages)
|
|
@@ -55,12 +66,14 @@ def respond(
|
|
| 55 |
# Detect if user wants code
|
| 56 |
explain_code = ""
|
| 57 |
if any(word in message.lower() for word in ["code", "script", "program", "python", "javascript"]):
|
| 58 |
-
|
|
|
|
| 59 |
When generating code:
|
| 60 |
-
-
|
| 61 |
-
- Use
|
| 62 |
-
- After writing
|
| 63 |
- Include emojis if fun_mode is True.
|
|
|
|
| 64 |
"""
|
| 65 |
|
| 66 |
# System message
|
|
@@ -78,6 +91,7 @@ You are Pixel, a happy, lively, and funny AI assistant.
|
|
| 78 |
- {explain_code}
|
| 79 |
- Pixel is shy internally but do not mention it unless asked directly by the user.
|
| 80 |
- Fun mode: {fun_mode} (if True, be funny and use emojis, if False, stay calm and kind).
|
|
|
|
| 81 |
"""}]
|
| 82 |
|
| 83 |
# Append conversation history
|
|
@@ -112,21 +126,23 @@ You are Pixel, a happy, lively, and funny AI assistant.
|
|
| 112 |
user_name = name
|
| 113 |
|
| 114 |
# ---------------- Gradio Chat Interface ----------------
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
with gr.
|
| 128 |
-
gr.
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# Pixel AI — Friendly Coding Teacher with 3 Methods & Developer Mode + Button
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
|
|
|
| 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 = [
|
|
|
|
| 25 |
top_p,
|
| 26 |
hf_token: gr.OAuthToken,
|
| 27 |
):
|
| 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!"
|
| 35 |
+
return
|
| 36 |
+
dev_mode = True
|
| 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)
|
|
|
|
| 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.
|
| 73 |
+
- Use clear, readable variable names in all methods.
|
| 74 |
+
- After writing each method, explain it sentence by sentence as if teaching a complete beginner.
|
| 75 |
- Include emojis if fun_mode is True.
|
| 76 |
+
- If dev_mode is True, ask the user if they want to modify the code and adjust it automatically.
|
| 77 |
"""
|
| 78 |
|
| 79 |
# System message
|
|
|
|
| 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
|
|
|
|
| 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(
|
| 132 |
+
respond,
|
| 133 |
+
type="messages",
|
| 134 |
+
additional_inputs=[
|
| 135 |
+
gr.Textbox(value="You are Pixel, a happy and funny AI created by Abdullah Mohamed.", label="System message"),
|
| 136 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 137 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 138 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 139 |
+
],
|
| 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()
|