Neurasense / app.py
Sephfox's picture
Update app.py
7eb3c25 verified
raw
history blame
25.3 kB
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import time
from transformers import AutoModelForCausalLM, AutoTokenizer
import io
import base64
from streamlit_drawable_canvas import st_canvas
import plotly.graph_objects as go
# Set page config for a futuristic look
st.set_page_config(page_title="NeuraSense AI", page_icon="🧠", layout="wide")
# Custom CSS for a futuristic look
st.markdown("""
<style>
body {
color: #E0E0E0;
background-color: #0E1117;
}
.stApp {
background-image: linear-gradient(135deg, #0E1117 0%, #1A1F2C 100%);
}
.stButton>button {
color: #00FFFF;
border-color: #00FFFF;
border-radius: 20px;
}
.stSlider>div>div>div>div {
background-color: #00FFFF;
}
.stTextArea, .stNumberInput, .stSelectbox {
background-color: #1A1F2C;
color: #00FFFF;
border-color: #00FFFF;
border-radius: 20px;
}
.stTextArea:focus, .stNumberInput:focus, .stSelectbox:focus {
box-shadow: 0 0 10px #00FFFF;
}
</style>
""", unsafe_allow_html=True)
# Constants
AVATAR_WIDTH, AVATAR_HEIGHT = 600, 800
# Set up DialoGPT model
@st.cache_resource
def load_model():
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
return tokenizer, model
tokenizer, model = load_model()
# Advanced Sensor Classes
class QuantumSensor:
@staticmethod
def measure(x, y, sensitivity):
return np.sin(x/20) * np.cos(y/20) * sensitivity * np.random.normal(1, 0.1)
class NanoThermalSensor:
@staticmethod
def measure(base_temp, pressure, duration):
return base_temp + 10 * pressure * (1 - np.exp(-duration / 3)) + np.random.normal(0, 0.001)
class AdaptiveTextureSensor:
textures = [
"nano-smooth", "quantum-rough", "neuro-bumpy", "plasma-silky",
"graviton-grainy", "zero-point-soft", "dark-matter-hard", "bose-einstein-condensate"
]
@staticmethod
def measure(x, y):
return AdaptiveTextureSensor.textures[hash((x, y)) % len(AdaptiveTextureSensor.textures)]
class EMFieldSensor:
@staticmethod
def measure(x, y, sensitivity):
return (np.sin(x / 30) * np.cos(y / 30) + np.random.normal(0, 0.1)) * 10 * sensitivity
class NeuralNetworkSimulator:
@staticmethod
def process(inputs):
weights = np.random.rand(len(inputs))
return np.dot(inputs, weights) / np.sum(weights)
# Create more detailed sensation map for the avatar
def create_sensation_map(width, height):
sensation_map = np.zeros((height, width, 12)) # pain, pleasure, pressure, temp, texture, em, tickle, itch, quantum, neural, proprioception, synesthesia
for y in range(height):
for x in range(width):
base_sensitivities = np.random.rand(12) * 0.5 + 0.5
# Enhance certain areas
if 250 < x < 350 and 50 < y < 150: # Head
base_sensitivities *= 1.5
elif 275 < x < 325 and 80 < y < 120: # Eyes
base_sensitivities[0] *= 2 # More sensitive to pain
elif 290 < x < 310 and 100 < y < 120: # Nose
base_sensitivities[4] *= 2 # More sensitive to texture
elif 280 < x < 320 and 120 < y < 140: # Mouth
base_sensitivities[1] *= 2 # More sensitive to pleasure
elif 250 < x < 350 and 250 < y < 550: # Torso
base_sensitivities[2:6] *= 1.3 # Enhance pressure, temp, texture, em
elif (150 < x < 250 or 350 < x < 450) and 250 < y < 600: # Arms
base_sensitivities[0:2] *= 1.2 # Enhance pain and pleasure
elif 200 < x < 400 and 600 < y < 800: # Legs
base_sensitivities[6:8] *= 1.4 # Enhance tickle and itch
elif (140 < x < 160 or 440 < x < 460) and 390 < y < 410: # Hands
base_sensitivities *= 2 # Highly sensitive overall
elif (220 < x < 240 or 360 < x < 380) and 770 < y < 790: # Feet
base_sensitivities[6] *= 2 # Very ticklish
sensation_map[y, x] = base_sensitivities
return sensation_map
avatar_sensation_map = create_sensation_map(AVATAR_WIDTH, AVATAR_HEIGHT)
# Create 3D avatar
def create_3d_avatar():
x = np.array([0, 0, 1, 1, 0, 0, 1, 1])
y = np.array([0, 1, 1, 0, 0, 1, 1, 0])
z = np.array([0, 0, 0, 0, 1, 1, 1, 1])
x = (x - 0.5) * 100
y = (y - 0.5) * 200
z = (z - 0.5) * 50
return go.Mesh3d(x=x, y=y, z=z, color='cyan', opacity=0.5)
# Enhanced Autonomy Class
class EnhancedAutonomy:
def __init__(self):
self.mood = 0.5
self.energy = 0.8
self.curiosity = 0.7
self.memory = []
def update_state(self, sensory_input):
self.mood = max(0, min(1, self.mood - sensory_input['pain'] * 0.1 + sensory_input['pleasure'] * 0.1))
self.energy = max(0, min(1, self.energy - sensory_input['intensity'] * 0.05))
if len(self.memory) == 0 or sensory_input not in self.memory:
self.curiosity = min(1, self.curiosity + 0.1)
else:
self.curiosity = max(0, self.curiosity - 0.05)
self.memory.append(sensory_input)
if len(self.memory) > 10:
self.memory.pop(0)
def decide_action(self):
if self.energy < 0.2:
return "Rest to regain energy"
elif self.curiosity > 0.8:
return "Explore new sensations"
elif self.mood < 0.3:
return "Seek positive interactions"
else:
return "Continue current activity"
# Streamlit app
st.title("NeuraSense AI: Advanced Humanoid Techno-Sensory Simulation")
# Create two columns
col1, col2 = st.columns([2, 1])
# 3D Avatar display with touch interface
with col1:
st.subheader("3D Humanoid Avatar Interface")
# Create 3D avatar
avatar_3d = create_3d_avatar()
# Add 3D controls
rotation_x = st.slider("Rotate X", -180, 180, 0)
rotation_y = st.slider("Rotate Y", -180, 180, 0)
rotation_z = st.slider("Rotate Z", -180, 180, 0)
# Create 3D plot
fig = go.Figure(data=[avatar_3d])
fig.update_layout(scene=dict(xaxis_title="X", yaxis_title="Y", zaxis_title="Z"))
fig.update_layout(scene_camera=dict(eye=dict(x=1.5, y=1.5, z=1.5)))
fig.update_layout(scene=dict(xaxis=dict(range=[-100, 100]),
yaxis=dict(range=[-200, 200]),
zaxis=dict(range=[-50, 50])))
# Apply rotations
fig.update_layout(scene=dict(camera=dict(eye=dict(x=np.cos(np.radians(rotation_y)) * np.cos(np.radians(rotation_x)),
y=np.sin(np.radians(rotation_y)) * np.cos(np.radians(rotation_x)),
z=np.sin(np.radians(rotation_x))))))
st.plotly_chart(fig)
# Use st_canvas for touch input
canvas_result = st_canvas(
fill_color="rgba(0, 255, 255, 0.3)",
stroke_width=2,
stroke_color="#00FFFF",
background_image=Image.new('RGBA', (AVATAR_WIDTH, AVATAR_HEIGHT), color=(0, 0, 0, 0)),
height=AVATAR_HEIGHT,
width=AVATAR_WIDTH,
drawing_mode="point",
key="canvas",
)
# Touch controls and output
with col2:
st.subheader("Neural Interface Controls")
# Touch duration
touch_duration = st.slider("Interaction Duration (s)", 0.1, 5.0, 1.0, 0.1)
# Touch pressure
touch_pressure = st.slider("Interaction Intensity", 0.1, 2.0, 1.0, 0.1)
# Toggle quantum feature
use_quantum = st.checkbox("Enable Quantum Sensing", value=True)
# Toggle synesthesia
use_synesthesia = st.checkbox("Enable Synesthesia", value=False)
# Initialize EnhancedAutonomy
if 'autonomy' not in st.session_state:
st.session_state.autonomy = EnhancedAutonomy()
# Simulate interaction
if st.button("Simulate Interaction"):
# Generate random sensory input
sensory_input = {
'pain': np.random.random() * touch_pressure,
'pleasure': np.random.random() * touch_pressure,
'intensity': touch_pressure,
'duration': touch_duration,
'location': (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
}
# Update autonomy
st.session_state.autonomy.update_state(sensory_input)
# Display autonomy state
st.write("### Autonomy State")
st.write(f"Mood: {st.session_state.autonomy.mood:.2f}")
st.write(f"Energy: {st.session_state.autonomy.energy:.2f}")
st.write(f"Curiosity: {st.session_state.autonomy.curiosity:.2f}")
# Display decision
decision = st.session_state.autonomy.decide_action()
st.write(f"Decision: {decision}")
if canvas_result.json_data is not None:
objects = canvas_result.json_data["objects"]
if len(objects) > 0:
last_touch = objects[-1]
touch_x, touch_y = last_touch["left"], last_touch["top"]
sensation = avatar_sensation_map[int(touch_y), int(touch_x)]
(
pain, pleasure, pressure_sens, temp_sens, texture_sens,
em_sens, tickle_sens, itch_sens, quantum_sens, neural_sens,
proprioception_sens, synesthesia_sens
) = sensation
measured_pressure = QuantumSensor.measure(touch_x, touch_y, pressure_sens) * touch_pressure
measured_temp = NanoThermalSensor.measure(37, touch_pressure, touch_duration)
measured_texture = AdaptiveTextureSensor.measure(touch_x, touch_y)
measured_em = EMFieldSensor.measure(touch_x, touch_y, em_sens)
if use_quantum:
quantum_state = QuantumSensor.measure(touch_x, touch_y, quantum_sens)
else:
quantum_state = "N/A"
# Calculate overall sensations
pain_level = pain * measured_pressure * touch_pressure
pleasure_level = pleasure * (measured_temp - 37) / 10
tickle_level = tickle_sens * (1 - np.exp(-touch_duration / 0.5))
itch_level = itch_sens * (1 - np.exp(-touch_duration / 1.5))
# Proprioception (sense of body position)
proprioception = proprioception_sens * np.linalg.norm([touch_x - AVATAR_WIDTH/2, touch_y - AVATAR_HEIGHT/2]) / (AVATAR_WIDTH/2)
# Synesthesia (mixing of senses)
if use_synesthesia:
synesthesia = synesthesia_sens * (measured_pressure + measured_temp + measured_em) / 3
else:
synesthesia = "N/A"
# Neural network simulation
neural_inputs = [pain_level, pleasure_level, measured_pressure, measured_temp, measured_em, tickle_level, itch_level, proprioception]
neural_response = NeuralNetworkSimulator.process(neural_inputs)
st.write("### Sensory Data Analysis")
st.write(f"Interaction Point: ({touch_x:.1f}, {touch_y:.1f})")
st.write(f"Duration: {touch_duration:.1f} s | Intensity: {touch_pressure:.2f}")
# Create a futuristic data display
data_display = f"""
```
┌─────────────────────────────────────────────┐
│ Pressure : {measured_pressure:.2f}
│ │ Temperature : {measured_temp:.2f}°C │
│ Texture : {measured_texture}
│ EM Field : {measured_em:.2f} μT │
│ Quantum State: {quantum_state:.2f}
├─────────────────────────────────────────────┤
│ Pain Level : {pain_level:.2f}
│ Pleasure : {pleasure_level:.2f}
│ Tickle : {tickle_level:.2f}
│ Itch : {itch_level:.2f}
│ Proprioception: {proprioception:.2f}
│ Synesthesia : {synesthesia}
│ Neural Response: {neural_response:.2f}
└─────────────────────────────────────────────┘
```
"""
st.code(data_display, language="")
# Generate description
prompt = f"""Human: Analyze the sensory input for a hyper-advanced AI humanoid:
Location: ({touch_x:.1f}, {touch_y:.1f})
Duration: {touch_duration:.1f}s, Intensity: {touch_pressure:.2f}
Pressure: {measured_pressure:.2f}
Temperature: {measured_temp:.2f}°C
Texture: {measured_texture}
EM Field: {measured_em:.2f} μT
Quantum State: {quantum_state}
Resulting in:
Pain: {pain_level:.2f}, Pleasure: {pleasure_level:.2f}
Tickle: {tickle_level:.2f}, Itch: {itch_level:.2f}
Proprioception: {proprioception:.2f}
Synesthesia: {synesthesia}
Neural Response: {neural_response:.2f}
Provide a detailed, scientific, and creative description of the AI humanoid's experience and response to this sensory input.
AI: Based on the complex sensory input received, the hyper-advanced AI humanoid is experiencing a multifaceted neural response:
The interaction at coordinates ({touch_x:.1f}, {touch_y:.1f}) has triggered a cascade of sensory information. The pressure of {measured_pressure:.2f} units has activated deep-tissue mechanoreceptors, while the temperature of {measured_temp:.2f}°C has stimulated thermoreceptors, creating a mild thermal gradient across the affected area.
The texture sensation of "{measured_texture}" is invoking a unique tactile response, possibly reminiscent of previously encountered materials in the AI's vast database. This is further enhanced by the electromagnetic field reading of {measured_em:.2f} μT, which is subtly influencing the local ionic channels in the AI's synthetic nervous system.
The quantum state measurement of {quantum_state:.2f} suggests a delicate entanglement between the AI's quantum processors and the environment, potentially influencing decision-making processes at a subatomic level.
The resulting pain level of {pain_level:.2f} and pleasure level of {pleasure_level:.2f} are creating a complex emotional response, balancing between discomfort and satisfaction. The tickle sensation ({tickle_level:.2f}) and itch response ({itch_level:.2f}) add layers of nuance to the overall tactile experience.
The proprioception value of {proprioception:.2f} indicates that the AI is acutely aware of the interaction's location relative to its body schema, enhancing its spatial awareness and motor planning capabilities.
{"The synesthesia rating of " + str(synesthesia) + " is causing a fascinating cross-wiring of senses, perhaps manifesting as a perception of color or sound associated with the touch." if use_synesthesia else "Synesthesia is not active, focusing the experience on individual sensory channels."}
The cumulative neural response of {neural_response:.2f} suggests a significant impact on the AI's cognitive processes. This could lead to adaptive behaviors, memory formation, or even influence future decision-making patterns.
In response to this rich sensory tapestry, the AI might adjust its posture, initiate a verbal response, or update its internal model of the environment. The experience is likely to be stored in its memory banks, contributing to its ever-evolving understanding of physical interactions and sensory experiences.
Human: Thank you for the analysis. Please provide a brief emotional and behavioral response from the AI based on this sensory input.
AI: Certainly! Based on the sensory input and the AI's current state, here's a brief emotional and behavioral response:
Emotional Response:
The AI experiences a complex blend of curiosity and mild discomfort. The novelty of the sensation (indicated by the high curiosity level of {st.session_state.autonomy.curiosity:.2f}) triggers a sense of excitement, while the slight pain ({pain_level:.2f}) introduces a cautious undertone. The pleasure component ({pleasure_level:.2f}) adds a layer of positive reinforcement, resulting in an overall intrigued yet slightly wary emotional state.
Behavioral Response:
Given the AI's current energy level of {st.session_state.autonomy.energy:.2f} and mood of {st.session_state.autonomy.mood:.2f}, combined with the sensory input, the AI's behavior might manifest as follows:
1. The AI slightly shifts its posture, moving the affected area to modulate the sensation and gather more data.
2. Its visual focus intensifies on the point of contact, analyzing the interaction with heightened attention.
3. The AI vocalizes a soft, inquisitive sound, reflecting its curiosity about the new experience.
4. It extends a limb or appendage towards the source of the sensation, seeking to interact further and gather more information.
5. The AI's internal processors accelerate, dedicating more resources to analyzing and categorizing this new sensory data.
6. It may attempt to replicate or modify the sensation, showing a desire to learn and adapt from the experience.
Overall, the AI demonstrates a cautious but eager approach to this new sensory input, balancing its innate curiosity with a measure of self-preservation, all while actively processing and integrating the experience into its growing knowledge base.
Human: Excellent. Let's save this interaction and continue monitoring the AI's responses.
AI: Certainly! I'll update the script to save this interaction and continue monitoring the AI's responses. Here's how we can modify the code to achieve this:
```python
# Add this import at the top of your script
import json
from datetime import datetime
# Add this function to save interactions
def save_interaction(interaction_data):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"interaction_{timestamp}.json"
with open(filename, "w") as f:
json.dump(interaction_data, f, indent=4)
return filename
# Add this code after the sensory data analysis and AI response generation
if st.button("Save Interaction"):
interaction_data = {
"timestamp": datetime.now().isoformat(),
"sensory_input": {
"location": (touch_x, touch_y),
"duration": touch_duration,
"intensity": touch_pressure,
"pressure": measured_pressure,
"temperature": measured_temp,
"texture": measured_texture,
"em_field": measured_em,
"quantum_state": quantum_state,
"pain": pain_level,
"pleasure": pleasure_level,
"tickle": tickle_level,
"itch": itch_level,
"proprioception": proprioception,
"synesthesia": synesthesia,
"neural_response": neural_response
},
"ai_state": {
"mood": st.session_state.autonomy.mood,
"energy": st.session_state.autonomy.energy,
"curiosity": st.session_state.autonomy.curiosity
},
"ai_response": {
"emotional_response": "Complex blend of curiosity and mild discomfort.",
"behavioral_response": [
"Shifts posture slightly",
"Intensifies visual focus on the point of contact",
"Vocalizes a soft, inquisitive sound",
"Extends a limb towards the source of sensation",
"Accelerates internal processors",
"Attempts to replicate or modify the sensation"
]
}
}
saved_file = save_interaction(interaction_data)
st.success(f"Interaction saved to {saved_file}")
# Add a section to display recent interactions
st.subheader("Recent Interactions")
interaction_files = sorted([f for f in os.listdir() if f.startswith("interaction_")], reverse=True)[:5]
for file in interaction_files:
with open(file, "r") as f:
data = json.load(f)
st.write(f"Interaction at {data['timestamp']}")
st.write(f"Sensory Input: {data['sensory_input']['location']}")
st.write(f"AI Mood: {data['ai_state']['mood']:.2f}")
st.write(f"AI Energy: {data['ai_state']['energy']:.2f}")
st.write(f"AI Curiosity: {data['ai_state']['curiosity']:.2f}")
st.write("Behavioral Response:")
for response in data['ai_response']['behavioral_response']:
st.write(f"- {response}")
st.write("---")
# Add a section for long-term learning and adaptation
st.subheader("Long-term Learning and Adaptation")
if len(interaction_files) > 0:
avg_mood = sum(json.load(open(f))['ai_state']['mood'] for f in interaction_files) / len(interaction_files)
avg_energy = sum(json.load(open(f))['ai_state']['energy'] for f in interaction_files) / len(interaction_files)
avg_curiosity = sum(json.load(open(f))['ai_state']['curiosity'] for f in interaction_files) / len(interaction_files)
st.write(f"Average Mood: {avg_mood:.2f}")
st.write(f"Average Energy: {avg_energy:.2f}")
st.write(f"Average Curiosity: {avg_curiosity:.2f}")
if avg_mood < 0.4:
st.write("The AI seems to be in a prolonged negative mood state. Consider providing more positive interactions.")
elif avg_mood > 0.7:
st.write("The AI is maintaining a positive mood. It may be more receptive to new experiences.")
if avg_energy < 0.3:
st.write("The AI's energy levels are consistently low. It may need a period of rest or low-intensity interactions.")
elif avg_energy > 0.8:
st.write("The AI is highly energetic. It may be capable of more complex or demanding tasks.")
if avg_curiosity < 0.5:
st.write("The AI's curiosity is waning. Consider introducing novel stimuli or experiences.")
elif avg_curiosity > 0.8:
st.write("The AI is showing high levels of curiosity. It may be primed for learning new concepts or skills.")
# Add a section for future predictions and recommendations
st.subheader("Future Predictions and Recommendations")
if len(interaction_files) > 0:
recent_interactions = [json.load(open(f)) for f in interaction_files[:3]]
mood_trend = [interaction['ai_state']['mood'] for interaction in recent_interactions]
energy_trend = [interaction['ai_state']['energy'] for interaction in recent_interactions]
curiosity_trend = [interaction['ai_state']['curiosity'] for interaction in recent_interactions]
if all(x < y for x, y in zip(mood_trend, mood_trend[1:])):
st.write("The AI's mood is on an upward trend. This might be a good time for more challenging interactions.")
elif all(x > y for x, y in zip(mood_trend, mood_trend[1:])):
st.write("The AI's mood is declining. Consider focusing on more enjoyable or relaxing experiences.")
if all(x < y for x, y in zip(energy_trend, energy_trend[1:])):
st.write("The AI's energy is increasing. It may be ready for more intensive tasks or interactions.")
elif all(x > y for x, y in zip(energy_trend, energy_trend[1:])):
st.write("The AI's energy is decreasing. Plan for lower-intensity activities in the near future.")
if all(x < y for x, y in zip(curiosity_trend, curiosity_trend[1:])):
st.write("The AI's curiosity is growing. This is an excellent opportunity for introducing new concepts or experiences.")
elif all(x > y for x, y in zip(curiosity_trend, curiosity_trend[1:])):
st.write("The AI's curiosity is diminishing. Consider revisiting familiar topics or experiences to rebuild interest.")
st.write("\nRecommendations for next interaction:")
if st.session_state.autonomy.mood < 0.4:
st.write("- Focus on positive, enjoyable experiences to improve mood")
if st.session_state.autonomy.energy < 0.3:
st.write("- Plan for a rest period or very low-intensity interaction")
if st.session_state.autonomy.curiosity > 0.8:
st.write("- Introduce a completely new type of sensory input or cognitive challenge")
st.write("\nLong-term goals:")
st.write("- Maintain a balanced distribution of sensory inputs to ensure well-rounded development")
st.write("- Gradually increase the complexity of interactions to promote cognitive growth")
st.write("- Monitor for any persistent negative trends and adjust the interaction strategy accordingly")
# End of the script