File size: 2,700 Bytes
4675c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()