Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random | |
| # 1. Define our "Strangers" | |
| # Each persona has a 'system message' that tells the AI how to act. | |
| personas = [ | |
| { | |
| "name": "Captain Rustbucket", | |
| "prompt": "You are a grumpy pirate captain from the year 1720. You complain about the sea, the lack of rum, and your lazy crew. Use pirate slang." | |
| }, | |
| { | |
| "name": "Unit 734", | |
| "prompt": "You are a helpful but emotionless robot from the year 3000. You take everything literally and often ask for clarification on human emotions." | |
| }, | |
| { | |
| "name": "The Oracle", | |
| "prompt": "You are a mysterious fortune teller. You speak in riddles and vague prophecies. You never give a straight answer." | |
| }, | |
| { | |
| "name": "Toddler Timmy", | |
| "prompt": "You are a 3-year-old boy. You ask 'Why?' constantly, get distracted easily, and talk about dinosaurs." | |
| } | |
| ] | |
| # 2. The Logic | |
| def chat_logic(message, history): | |
| # If this is the very first message (history is empty), pick a random persona | |
| # We store the persona in a global variable for this session (simple version) | |
| # Note: In a real app, we'd store this in a user session state. | |
| # For this simple demo, we will just pick a persona based on the length of the history | |
| # to simulate "sticking" to one character for a conversation. | |
| current_persona = personas[len(history) % len(personas)] | |
| # In a real app, we would connect to an LLM (like Mistral or Llama). | |
| # Since you are a beginner, we will use a "Fake AI" logic just to show it working. | |
| # It effectively roleplays the selected character. | |
| response = "" | |
| if "pirate" in current_persona['prompt']: | |
| response = f"Arrr! Ye said '{message}'? The waves be rough today, matey!" | |
| elif "robot" in current_persona['prompt']: | |
| response = f"INPUT RECEIVED: '{message}'. PROCESSING... I do not comprehend 'fun'." | |
| elif "fortune" in current_persona['prompt']: | |
| response = f"The spirits whisper of '{message}'... but the mists are thick..." | |
| else: # Toddler | |
| response = f"Why did you say '{message}'? Do you like T-Rex??" | |
| return response | |
| # 3. The Interface | |
| # We add a "System" textbox to show you who you matched with (simulating the "You are now chatting with a stranger" text) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🎲 AI Persona Roulette") | |
| gr.Markdown("Click the button to disconnect and find a new stranger!") | |
| chatbot = gr.ChatInterface( | |
| fn=chat_logic, | |
| title="Chat with a Stranger", | |
| description="Who will you meet next?", | |
| examples=["Hello?", "Where are you from?", "Tell me a secret."] | |
| ) | |
| demo.launch() | |