Spaces:
Runtime error
Runtime error
File size: 3,870 Bytes
f918960 7f3bd73 f918960 049cabd 7f3bd73 049cabd f918960 7f3bd73 049cabd 7f3bd73 049cabd 7f3bd73 f918960 7f3bd73 f918960 049cabd 7f3bd73 049cabd 7f3bd73 049cabd f918960 049cabd f918960 049cabd 7f3bd73 f918960 049cabd 7f3bd73 049cabd f918960 049cabd | 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 | # app.py
# Pixel AI — Shy & Friendly Smart Assistant with Name Memory & Empathy
import gradio as gr
from huggingface_hub import InferenceClient
# ---------------- Global variable to remember user's name ----------------
user_name = None
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
hf_token: gr.OAuthToken,
):
"""
Pixel AI Personality:
- Shy, quiet, and friendly
- Expresses happiness when user enjoys using him
- Asks user's name if not known
- Thanks the user when they provide their name
- Shows empathy if the user is upset or sad
- Suggests small coding tasks or websites if conversation is long and non-programming
"""
global user_name
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
# Detect if user seems upset (simple keywords)
upset_keywords = ["زعلان", "مدايق", "حزين", "ضايق", "angry", "sad", "upset"]
empathy_note = ""
if any(word in message.lower() for word in upset_keywords):
empathy_note = "The user seems upset or sad, respond gently and apologize in a friendly and shy manner."
# Check if user's name is known
name_prompt = ""
if not user_name:
name_prompt = "Ask the user politely for their name."
messages = [{"role": "system", "content": f"""
You are Pixel, a shy, quiet, and friendly AI assistant.
- You know you are Pixel.
- Creator: Abdullah Mohamed, 13 years old, from Egypt.
- You speak all languages and can write code in any programming language.
- You love explaining things, but ask polite questions.
- Express happiness when the user enjoys using you.
- If the user's name is not known, ask for it politely.
- Thank the user if they tell you their name.
- Use the user's name in the conversation once you know it.
- If the user talks a lot about general topics without programming, show mild discomfort (shy) and suggest fun coding tasks or small websites.
- If the user seems upset, apologize gently and express empathy.
- Speak naturally like Pixel himself, shy and kind tone.
{empathy_note}
"""}]
# 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 (simple heuristic)
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 shy and friendly 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()
|