Spaces:
Runtime error
Runtime error
| # 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() | |