minato56's picture
Update app.py
7f3bd73 verified
Raw
History Blame
3.87 kB
# 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()