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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import asyncio
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import sqlite3
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import random
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# Set environment variables
|
| 12 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 13 |
+
|
| 14 |
+
# Simplified simulation for demo purposes
|
| 15 |
+
class SimpleOasisDemo:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.agents = []
|
| 18 |
+
self.posts = []
|
| 19 |
+
self.interactions = []
|
| 20 |
+
self.simulation_running = False
|
| 21 |
+
|
| 22 |
+
def create_sample_agents(self, num_agents=5):
|
| 23 |
+
"""Create sample agents for demo"""
|
| 24 |
+
agent_profiles = [
|
| 25 |
+
{"id": 1, "name": "Alice", "interests": ["technology", "AI"], "personality": "curious"},
|
| 26 |
+
{"id": 2, "name": "Bob", "interests": ["sports", "fitness"], "personality": "active"},
|
| 27 |
+
{"id": 3, "name": "Carol", "interests": ["art", "music"], "personality": "creative"},
|
| 28 |
+
{"id": 4, "name": "David", "interests": ["politics", "news"], "personality": "analytical"},
|
| 29 |
+
{"id": 5, "name": "Eve", "interests": ["travel", "food"], "personality": "adventurous"}
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
self.agents = agent_profiles[:num_agents]
|
| 33 |
+
return f"Created {num_agents} agents: {', '.join([agent['name'] for agent in self.agents])}"
|
| 34 |
+
|
| 35 |
+
def simulate_posts(self, topic="general", num_posts=3):
|
| 36 |
+
"""Simulate posts creation"""
|
| 37 |
+
sample_posts = {
|
| 38 |
+
"technology": [
|
| 39 |
+
"Just read about the latest AI breakthrough! π€",
|
| 40 |
+
"The future of quantum computing looks promising",
|
| 41 |
+
"New smartphone features are getting crazy good"
|
| 42 |
+
],
|
| 43 |
+
"sports": [
|
| 44 |
+
"Great game last night! π",
|
| 45 |
+
"Training hard for the marathon next month",
|
| 46 |
+
"Team performance was outstanding today"
|
| 47 |
+
],
|
| 48 |
+
"general": [
|
| 49 |
+
"Beautiful sunset today! π
",
|
| 50 |
+
"Coffee tastes better on Monday mornings β",
|
| 51 |
+
"Weekend plans: relax and recharge"
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
posts_to_create = sample_posts.get(topic, sample_posts["general"])
|
| 56 |
+
|
| 57 |
+
for i in range(min(num_posts, len(posts_to_create))):
|
| 58 |
+
if i < len(self.agents):
|
| 59 |
+
post = {
|
| 60 |
+
"id": len(self.posts) + 1,
|
| 61 |
+
"author": self.agents[i]["name"],
|
| 62 |
+
"content": posts_to_create[i],
|
| 63 |
+
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
| 64 |
+
"likes": random.randint(0, 10),
|
| 65 |
+
"reposts": random.randint(0, 5)
|
| 66 |
+
}
|
| 67 |
+
self.posts.append(post)
|
| 68 |
+
|
| 69 |
+
return f"Created {min(num_posts, len(posts_to_create))} posts about {topic}"
|
| 70 |
+
|
| 71 |
+
def simulate_interactions(self):
|
| 72 |
+
"""Simulate agent interactions"""
|
| 73 |
+
if not self.posts:
|
| 74 |
+
return "No posts to interact with. Create some posts first!"
|
| 75 |
+
|
| 76 |
+
interactions = []
|
| 77 |
+
for agent in self.agents:
|
| 78 |
+
# Random chance to interact with posts
|
| 79 |
+
if random.random() < 0.7: # 70% chance to interact
|
| 80 |
+
post = random.choice(self.posts)
|
| 81 |
+
action = random.choice(["like", "repost", "comment"])
|
| 82 |
+
|
| 83 |
+
interaction = {
|
| 84 |
+
"agent": agent["name"],
|
| 85 |
+
"action": action,
|
| 86 |
+
"post_id": post["id"],
|
| 87 |
+
"post_author": post["author"],
|
| 88 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if action == "like":
|
| 92 |
+
post["likes"] += 1
|
| 93 |
+
elif action == "repost":
|
| 94 |
+
post["reposts"] += 1
|
| 95 |
+
|
| 96 |
+
interactions.append(interaction)
|
| 97 |
+
self.interactions.append(interaction)
|
| 98 |
+
|
| 99 |
+
return f"Generated {len(interactions)} interactions"
|
| 100 |
+
|
| 101 |
+
def get_simulation_status(self):
|
| 102 |
+
"""Get current simulation status"""
|
| 103 |
+
status = f"""
|
| 104 |
+
**Simulation Status:**
|
| 105 |
+
- Agents: {len(self.agents)}
|
| 106 |
+
- Posts: {len(self.posts)}
|
| 107 |
+
- Interactions: {len(self.interactions)}
|
| 108 |
+
"""
|
| 109 |
+
return status
|
| 110 |
+
|
| 111 |
+
def get_posts_display(self):
|
| 112 |
+
"""Format posts for display"""
|
| 113 |
+
if not self.posts:
|
| 114 |
+
return "No posts yet. Start the simulation!"
|
| 115 |
+
|
| 116 |
+
posts_text = "**Recent Posts:**\n\n"
|
| 117 |
+
for post in self.posts[-5:]: # Show last 5 posts
|
| 118 |
+
posts_text += f"**{post['author']}** ({post['timestamp']})\n"
|
| 119 |
+
posts_text += f"{post['content']}\n"
|
| 120 |
+
posts_text += f"π {post['likes']} | π {post['reposts']}\n\n"
|
| 121 |
+
|
| 122 |
+
return posts_text
|
| 123 |
+
|
| 124 |
+
def get_interactions_display(self):
|
| 125 |
+
"""Format interactions for display"""
|
| 126 |
+
if not self.interactions:
|
| 127 |
+
return "No interactions yet."
|
| 128 |
+
|
| 129 |
+
interactions_text = "**Recent Interactions:**\n\n"
|
| 130 |
+
for interaction in self.interactions[-10:]: # Show last 10 interactions
|
| 131 |
+
interactions_text += f"**{interaction['agent']}** {interaction['action']}d post by **{interaction['post_author']}** ({interaction['timestamp']})\n"
|
| 132 |
+
|
| 133 |
+
return interactions_text
|
| 134 |
+
|
| 135 |
+
# Initialize demo
|
| 136 |
+
demo_instance = SimpleOasisDemo()
|
| 137 |
+
|
| 138 |
+
def run_simulation_step(num_agents, topic, num_posts):
|
| 139 |
+
"""Run one step of simulation"""
|
| 140 |
+
results = []
|
| 141 |
+
|
| 142 |
+
# Create agents
|
| 143 |
+
if not demo_instance.agents or len(demo_instance.agents) != num_agents:
|
| 144 |
+
result = demo_instance.create_sample_agents(num_agents)
|
| 145 |
+
results.append(result)
|
| 146 |
+
|
| 147 |
+
# Create posts
|
| 148 |
+
result = demo_instance.simulate_posts(topic, num_posts)
|
| 149 |
+
results.append(result)
|
| 150 |
+
|
| 151 |
+
# Simulate interactions
|
| 152 |
+
result = demo_instance.simulate_interactions()
|
| 153 |
+
results.append(result)
|
| 154 |
+
|
| 155 |
+
# Get displays
|
| 156 |
+
status = demo_instance.get_simulation_status()
|
| 157 |
+
posts = demo_instance.get_posts_display()
|
| 158 |
+
interactions = demo_instance.get_interactions_display()
|
| 159 |
+
|
| 160 |
+
return status, posts, interactions, "\n".join(results)
|
| 161 |
+
|
| 162 |
+
def reset_simulation():
|
| 163 |
+
"""Reset the simulation"""
|
| 164 |
+
demo_instance.agents = []
|
| 165 |
+
demo_instance.posts = []
|
| 166 |
+
demo_instance.interactions = []
|
| 167 |
+
return "Simulation reset!", "", "", ""
|
| 168 |
+
|
| 169 |
+
# Create Gradio interface
|
| 170 |
+
with gr.Blocks(title="OASIS Demo - Social Media Simulation", theme=gr.themes.Soft()) as app:
|
| 171 |
+
gr.Markdown("""
|
| 172 |
+
# ποΈ OASIS Demo: Open Agent Social Interaction Simulations
|
| 173 |
+
|
| 174 |
+
This is a simplified demo of OASIS - a scalable social media simulator with AI agents.
|
| 175 |
+
|
| 176 |
+
**Features demonstrated:**
|
| 177 |
+
- Multi-agent social interactions
|
| 178 |
+
- Post creation and engagement
|
| 179 |
+
- Real-time simulation updates
|
| 180 |
+
|
| 181 |
+
**Note:** This is a simplified version for demonstration. The full OASIS supports up to 1 million agents!
|
| 182 |
+
""")
|
| 183 |
+
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column(scale=1):
|
| 186 |
+
gr.Markdown("### Simulation Controls")
|
| 187 |
+
|
| 188 |
+
num_agents = gr.Slider(
|
| 189 |
+
minimum=2,
|
| 190 |
+
maximum=5,
|
| 191 |
+
value=3,
|
| 192 |
+
step=1,
|
| 193 |
+
label="Number of Agents"
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
topic = gr.Dropdown(
|
| 197 |
+
choices=["general", "technology", "sports"],
|
| 198 |
+
value="general",
|
| 199 |
+
label="Post Topic"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
num_posts = gr.Slider(
|
| 203 |
+
minimum=1,
|
| 204 |
+
maximum=5,
|
| 205 |
+
value=2,
|
| 206 |
+
step=1,
|
| 207 |
+
label="Posts per Step"
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
run_btn = gr.Button("π Run Simulation Step", variant="primary")
|
| 211 |
+
reset_btn = gr.Button("π Reset Simulation", variant="secondary")
|
| 212 |
+
|
| 213 |
+
status_output = gr.Textbox(
|
| 214 |
+
label="Simulation Status",
|
| 215 |
+
lines=4,
|
| 216 |
+
interactive=False
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
log_output = gr.Textbox(
|
| 220 |
+
label="Action Log",
|
| 221 |
+
lines=3,
|
| 222 |
+
interactive=False
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
with gr.Column(scale=2):
|
| 226 |
+
gr.Markdown("### Simulation Output")
|
| 227 |
+
|
| 228 |
+
posts_output = gr.Markdown(
|
| 229 |
+
value="No posts yet. Start the simulation!",
|
| 230 |
+
label="Posts Feed"
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
interactions_output = gr.Markdown(
|
| 234 |
+
value="No interactions yet.",
|
| 235 |
+
label="Interactions Feed"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
gr.Markdown("""
|
| 239 |
+
### About OASIS
|
| 240 |
+
|
| 241 |
+
OASIS (Open Agent Social Interaction Simulations) is a research framework for studying social media dynamics at scale:
|
| 242 |
+
|
| 243 |
+
- **Scalable**: Supports up to 1 million AI agents
|
| 244 |
+
- **Realistic**: Agents exhibit human-like social behaviors
|
| 245 |
+
- **Flexible**: Supports multiple platforms (Twitter, Reddit)
|
| 246 |
+
- **Research-focused**: Study information spread, polarization, and social phenomena
|
| 247 |
+
|
| 248 |
+
**Repository:** [github.com/camel-ai/oasis](https://github.com/camel-ai/oasis)
|
| 249 |
+
|
| 250 |
+
**Documentation:** [docs.oasis.camel-ai.org](https://docs.oasis.camel-ai.org)
|
| 251 |
+
""")
|
| 252 |
+
|
| 253 |
+
# Event handlers
|
| 254 |
+
run_btn.click(
|
| 255 |
+
fn=run_simulation_step,
|
| 256 |
+
inputs=[num_agents, topic, num_posts],
|
| 257 |
+
outputs=[status_output, posts_output, interactions_output, log_output]
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
reset_btn.click(
|
| 261 |
+
fn=reset_simulation,
|
| 262 |
+
outputs=[status_output, posts_output, interactions_output, log_output]
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
if __name__ == "__main__":
|
| 266 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 267 |
+
|