|
|
import json
|
|
|
import gradio as gr
|
|
|
import time
|
|
|
import modal
|
|
|
import threading
|
|
|
import tempfile
|
|
|
import os
|
|
|
import re
|
|
|
import asyncio
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("π Starting Agentic Ecosystem Simulator for Hugging Face Spaces...")
|
|
|
|
|
|
|
|
|
app = modal.App("agentic-ecosystem-simple")
|
|
|
|
|
|
def simulate_ecosystem_step(environment):
|
|
|
"""Main orchestration function - runs the text-based ecosystem simulation using Modal"""
|
|
|
|
|
|
if not environment or not environment.strip():
|
|
|
return None, [], "Please provide an environment description.", None
|
|
|
|
|
|
try:
|
|
|
print(f"π± Simulating ecosystem for environment: {environment}")
|
|
|
|
|
|
|
|
|
f = modal.Function.from_name("agentic-ecosystem-simple", "simulate_ecosystem")
|
|
|
results = f.remote(environment)
|
|
|
|
|
|
|
|
|
if 'error' in results and results['error']:
|
|
|
print(f"β Error in simulation: {results['error']}")
|
|
|
return None, [], f"Simulation error: {results['error']}", None
|
|
|
|
|
|
|
|
|
species_list = results['species']
|
|
|
print(f"β Generated {len(species_list)} species")
|
|
|
|
|
|
|
|
|
events = results['events']
|
|
|
summary = results['summary']
|
|
|
print("β Generated events and summary")
|
|
|
|
|
|
print("π Ecosystem simulation completed!")
|
|
|
|
|
|
return (
|
|
|
species_list,
|
|
|
events,
|
|
|
summary,
|
|
|
"π Audio narration not available in HF Spaces environment"
|
|
|
)
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"Error in ecosystem simulation: {e}")
|
|
|
return None, [], f"Simulation error: {str(e)}", None
|
|
|
|
|
|
def check_modal_deployment():
|
|
|
"""Check if Modal is properly configured"""
|
|
|
try:
|
|
|
|
|
|
f = modal.Function.from_name("agentic-ecosystem-simple", "simulate_ecosystem")
|
|
|
return "β
Modal is properly configured and deployed!\n\nThe ecosystem simulator is ready to use."
|
|
|
except Exception as e:
|
|
|
return f"β Modal error: {str(e)}\nPlease ensure Modal functions are deployed and accessible."
|
|
|
|
|
|
|
|
|
nature_theme = gr.themes.Soft().set(
|
|
|
body_background_fill="#d4edda",
|
|
|
background_fill_primary="#e8f5e8",
|
|
|
background_fill_secondary="#d6ebf5",
|
|
|
block_background_fill="#f8f9fa",
|
|
|
button_primary_background_fill="#1e7e34",
|
|
|
button_primary_background_fill_hover="#28a745",
|
|
|
button_primary_text_color="#ffffff",
|
|
|
button_secondary_background_fill="#c3e6cb",
|
|
|
button_secondary_background_fill_hover="#b3d7e6",
|
|
|
button_secondary_text_color="#155724"
|
|
|
)
|
|
|
|
|
|
with gr.Blocks(title="Agentic Ecosystem Simulator", theme=nature_theme, css="""
|
|
|
/* Additional custom CSS for nature theme */
|
|
|
.gradio-container {
|
|
|
background: linear-gradient(135deg, #c8e6c8 0%, #b8d4e8 50%, #d0e2f0 100%);
|
|
|
min-height: 100vh;
|
|
|
}
|
|
|
|
|
|
/* Card-like styling for content blocks */
|
|
|
.block {
|
|
|
background: rgba(248, 249, 250, 0.95);
|
|
|
border-radius: 8px;
|
|
|
border: 1px solid #6c9a6c;
|
|
|
box-shadow: 0 2px 8px rgba(28, 126, 52, 0.1);
|
|
|
}
|
|
|
|
|
|
/* Nature-themed headers */
|
|
|
h1, h2, h3 {
|
|
|
color: #155724;
|
|
|
text-shadow: 1px 1px 2px rgba(28, 126, 52, 0.15);
|
|
|
}
|
|
|
""") as demo:
|
|
|
gr.Markdown("""
|
|
|
# π Agentic Ecosystem Simulator
|
|
|
## Powered by Modal Cloud Functions
|
|
|
|
|
|
Welcome to the Cloud-Based Ecosystem Simulator! This application uses multiple AI models working together
|
|
|
through Modal cloud functions to create and simulate a living ecosystem through text.
|
|
|
|
|
|
**How it works:**
|
|
|
1. **𧬠Species Generation** - AI creates plants and animals specifically adapted to your environment
|
|
|
2. **π Interaction Simulation** - AI simulates ecological interactions specific to your environment
|
|
|
3. **π Ecosystem Summary** - Key events are summarized with environmental context
|
|
|
4. **πΏ Environment Analysis** - Your input is analyzed for environment type, climate, time, season & more
|
|
|
|
|
|
**The more detailed your environment description, the more tailored your ecosystem will be!**
|
|
|
|
|
|
π **Status**: Running on Hugging Face Spaces with Modal cloud backend
|
|
|
|
|
|
β οΈ **Note**: Audio narration and visualization features are disabled in this cloud environment
|
|
|
""")
|
|
|
|
|
|
with gr.Row():
|
|
|
with gr.Column(scale=2):
|
|
|
environment_input = gr.Textbox(
|
|
|
label="πΏ Environment Description (be specific and detailed!)",
|
|
|
placeholder="e.g., 'tropical rainforest with morning mist and flowering vines', 'desert oasis with palm trees and rocky outcrops at sunset'",
|
|
|
lines=3,
|
|
|
value="temperate meadow in spring morning with wildflowers and gentle breeze"
|
|
|
)
|
|
|
|
|
|
simulate_btn = gr.Button("π Simulate Ecosystem Day", variant="primary", size="lg")
|
|
|
|
|
|
modal_status = gr.Textbox(
|
|
|
label="π₯οΈ Modal Status",
|
|
|
value="Click 'Check Modal' to see connection status",
|
|
|
lines=4,
|
|
|
interactive=False
|
|
|
)
|
|
|
check_modal_btn = gr.Button("π Check Modal Connection", variant="secondary")
|
|
|
|
|
|
with gr.Column(scale=3):
|
|
|
species_output = gr.JSON(
|
|
|
label="𧬠Generated Species",
|
|
|
show_label=True
|
|
|
)
|
|
|
|
|
|
with gr.Row():
|
|
|
with gr.Column():
|
|
|
events_output = gr.Textbox(
|
|
|
label="π
Day's Events",
|
|
|
lines=8,
|
|
|
show_label=True
|
|
|
)
|
|
|
|
|
|
with gr.Column():
|
|
|
summary_output = gr.Textbox(
|
|
|
label="π Ecosystem Summary",
|
|
|
lines=8,
|
|
|
show_label=True
|
|
|
)
|
|
|
|
|
|
audio_status = gr.Textbox(
|
|
|
label="π΅ Audio Status",
|
|
|
value="π Audio narration not available in HF Spaces environment",
|
|
|
lines=2,
|
|
|
interactive=False
|
|
|
)
|
|
|
|
|
|
|
|
|
simulate_btn.click(
|
|
|
fn=simulate_ecosystem_step,
|
|
|
inputs=[environment_input],
|
|
|
outputs=[species_output, events_output, summary_output, audio_status]
|
|
|
)
|
|
|
|
|
|
check_modal_btn.click(
|
|
|
fn=check_modal_deployment,
|
|
|
outputs=[modal_status]
|
|
|
)
|
|
|
|
|
|
|
|
|
gr.Markdown("""
|
|
|
### π‘ Detailed Environments to Try:
|
|
|
|
|
|
**Try adding environmental details like:**
|
|
|
- **Time of day**: morning, afternoon, evening, night
|
|
|
- **Season**: spring, summer, autumn, winter
|
|
|
- **Weather**: misty, sunny, rainy, cloudy, stormy
|
|
|
- **Features**: rocky, sandy, flowering, foggy, lush, sparse
|
|
|
|
|
|
**Example environments with rich details:**
|
|
|
- `tropical rainforest with morning mist and flowering plants`
|
|
|
- `desert oasis with palm trees and rocky outcrops at sunset`
|
|
|
- `temperate forest clearing in autumn with golden leaves`
|
|
|
- `alpine meadow with wildflowers on a spring morning`
|
|
|
- `wetland marsh with reeds and shallow water during summer evening`
|
|
|
- `volcanic island with unique flora adapting to the harsh terrain`
|
|
|
- `underground cave system with phosphorescent fungi and still pools`
|
|
|
- `arctic tundra with hardy vegetation under the midnight sun`
|
|
|
|
|
|
### π§ Technical Details
|
|
|
|
|
|
This simulator demonstrates **agentic AI orchestration** by chaining together multiple specialized AI models:
|
|
|
- Environment analysis and parsing
|
|
|
- Species generation based on habitat conditions
|
|
|
- Ecological interaction simulation
|
|
|
- Narrative summary generation
|
|
|
|
|
|
All processing happens in the cloud via Modal, making this a true **serverless AI application**.
|
|
|
""")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
print("\n" + "="*60)
|
|
|
print("π AGENTIC ECOSYSTEM SIMULATOR - HUGGING FACE SPACES")
|
|
|
print("="*60)
|
|
|
print("\nπ Starting Hugging Face Spaces interface...")
|
|
|
print("\nπ SETUP STATUS:")
|
|
|
print("β
Modal cloud functions ready")
|
|
|
print("β
Hugging Face Spaces compatible version")
|
|
|
print("β οΈ Audio and visualization features disabled for cloud compatibility")
|
|
|
print("\n⨠Ready to simulate ecosystems!")
|
|
|
print("="*60)
|
|
|
|
|
|
demo.launch()
|
|
|
|