Sephfox commited on
Commit
998978e
·
verified ·
1 Parent(s): d9be852

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import streamlit as st
3
  import numpy as np
4
  import matplotlib.pyplot as plt
@@ -26,44 +25,44 @@ tokenizer, model = load_model()
26
  class PressureSensor:
27
  def __init__(self, sensitivity=1.0):
28
  self.sensitivity = sensitivity
29
-
30
  def measure(self, pressure):
31
  return pressure * self.sensitivity
32
 
33
  class TemperatureSensor:
34
  def __init__(self, base_temp=37.0):
35
  self.base_temp = base_temp
36
-
37
  def measure(self, touch_temp):
38
  return self.base_temp + (touch_temp - self.base_temp) * 0.1
39
 
40
  class TextureSensor:
41
  def __init__(self):
42
  self.textures = ["smooth", "rough", "bumpy", "silky", "grainy"]
43
-
44
  def measure(self, x, y):
45
  return self.textures[hash((x, y)) % len(self.textures)]
46
 
47
  class EMFieldSensor:
48
  def measure(self, x, y):
49
- return np.sin(x/50) * np.cos(y/50) * 10 # simulated EM field
50
 
51
  # Create sensation map for the avatar
52
  def create_sensation_map(width, height):
53
  sensation_map = np.zeros((height, width, 7)) # RGBPVTE channels for pain, pleasure, neutral, pressure, velocity, temperature, and EM sensitivity
54
  for y in range(height):
55
  for x in range(width):
56
- pain = np.exp(-((x-100)**2 + (y-150)**2) / 5000) + np.exp(-((x-300)**2 + (y-450)**2) / 5000)
57
- pleasure = np.exp(-((x-200)**2 + (y-300)**2) / 5000) + np.exp(-((x-100)**2 + (y-500)**2) / 5000)
58
  neutral = 1 - (pain + pleasure)
59
- pressure = np.exp(-((x-50)**2 + (y-150)**2) / 2000) + np.exp(-((x-350)**2 + (y-150)**2) / 2000) + \
60
- np.exp(-((x-100)**2 + (y-550)**2) / 2000) + np.exp(-((x-300)**2 + (y-550)**2) / 2000)
61
- velocity = np.exp(-((x-200)**2 + (y-100)**2) / 5000) + np.exp(-((x-200)**2 + (y-300)**2) / 5000)
62
- temperature = np.exp(-((x-200)**2 + (y-200)**2) / 10000) # more sensitive in the core
63
- em_sensitivity = np.exp(-((x-200)**2 + (y-100)**2) / 8000) # more sensitive in the head
64
-
65
  sensation_map[y, x] = [pain, pleasure, neutral, pressure, velocity, temperature, em_sensitivity]
66
-
67
  return sensation_map
68
 
69
  avatar_sensation_map = create_sensation_map(AVATAR_WIDTH, AVATAR_HEIGHT)
@@ -78,21 +77,21 @@ em_sensor = EMFieldSensor()
78
  def create_avatar():
79
  img = Image.new('RGB', (AVATAR_WIDTH, AVATAR_HEIGHT), color='white')
80
  draw = ImageDraw.Draw(img)
81
-
82
  # Head
83
  draw.ellipse([150, 50, 250, 150], fill='beige', outline='black')
84
-
85
  # Body
86
  draw.rectangle([175, 150, 225, 400], fill='beige', outline='black')
87
-
88
  # Arms
89
  draw.rectangle([125, 150, 175, 350], fill='beige', outline='black')
90
  draw.rectangle([225, 150, 275, 350], fill='beige', outline='black')
91
-
92
  # Legs
93
  draw.rectangle([175, 400, 200, 550], fill='beige', outline='black')
94
  draw.rectangle([200, 400, 225, 550], fill='beige', outline='black')
95
-
96
  return img
97
 
98
  avatar_image = create_avatar()
@@ -127,16 +126,16 @@ with col2:
127
  def calculate_sensation(x, y, pressure, velocity):
128
  sensation = avatar_sensation_map[int(y), int(x)]
129
  pain, pleasure, neutral, pressure_sensitivity, velocity_sensitivity, temp_sensitivity, em_sensitivity = sensation
130
-
131
  measured_pressure = pressure_sensor.measure(pressure * pressure_sensitivity)
132
  measured_temp = temp_sensor.measure(37 + pressure * 5) # Simulating temperature increase with pressure
133
  measured_texture = texture_sensor.measure(x, y)
134
  measured_em = em_sensor.measure(x, y) * em_sensitivity
135
-
136
  modified_pain = pain * measured_pressure / 10
137
  modified_pleasure = pleasure * velocity * velocity_sensitivity
138
  modified_neutral = neutral * (1 - (measured_pressure + velocity) / 2)
139
-
140
  return modified_pain, modified_pleasure, modified_neutral, measured_pressure, measured_temp, measured_texture, measured_em
141
 
142
  def generate_description(x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em):
@@ -148,11 +147,11 @@ def generate_description(x, y, pressure, velocity, pain, pleasure, neutral, meas
148
  Resulting in:
149
  Pain: {pain:.2f}, Pleasure: {pleasure:.2f}, Neutral: {neutral:.2f}
150
  Avatar:"""
151
-
152
  input_ids = tokenizer.encode(prompt, return_tensors="pt")
153
  output = model.generate(input_ids, max_length=200, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7)
154
-
155
- return tokenizer.decode(output[0], skip_special_tokens=True).split("Avatar: ")[-1].strip()
156
 
157
  # Initialize session state
158
  if 'touch_history' not in st.session_state:
@@ -165,23 +164,24 @@ if canvas_result.json_data is not None:
165
  new_points = objects[-1].get("points", [])
166
  if new_points:
167
  for i in range(1, len(new_points)):
168
- x1, y1 = new_points[i-1]["x"], new_points[i-1]["y"]
169
  x2, y2 = new_points[i]["x"], new_points[i]["y"]
170
-
171
  # Calculate pressure and velocity
172
- distance = np.sqrt((x2-x1)**2 + (y2-y1)**2)
173
  velocity = distance / 0.01 # Assuming 10ms between points
174
  pressure = 1 + velocity / 100 # Simple pressure model
175
-
176
  x, y = (x1 + x2) / 2, (y1 + y2) / 2
177
  pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em = calculate_sensation(x, y, pressure, velocity)
178
-
179
  st.session_state.touch_history.append((x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em))
180
 
181
  # Display touch history and generate descriptions
182
  if st.session_state.touch_history:
183
  st.subheader("Touch History and Sensations")
184
- for x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em in st.session_state.touch_history[-5:]:
 
185
  st.write(f"Touch at ({x:.1f}, {y:.1f})")
186
  st.write(f"Pressure: {measured_pressure:.2f}, Temperature: {measured_temp:.2f}°C")
187
  st.write(f"Texture: {measured_texture}, EM Field: {measured_em:.2f}")
@@ -196,4 +196,4 @@ st.write("Draw on the avatar to simulate touch. The simulation will process pres
196
  # Add a button to clear the touch history
197
  if st.button("Clear Touch History"):
198
  st.session_state.touch_history = []
199
- st.experimental_rerun()
 
 
1
  import streamlit as st
2
  import numpy as np
3
  import matplotlib.pyplot as plt
 
25
  class PressureSensor:
26
  def __init__(self, sensitivity=1.0):
27
  self.sensitivity = sensitivity
28
+
29
  def measure(self, pressure):
30
  return pressure * self.sensitivity
31
 
32
  class TemperatureSensor:
33
  def __init__(self, base_temp=37.0):
34
  self.base_temp = base_temp
35
+
36
  def measure(self, touch_temp):
37
  return self.base_temp + (touch_temp - self.base_temp) * 0.1
38
 
39
  class TextureSensor:
40
  def __init__(self):
41
  self.textures = ["smooth", "rough", "bumpy", "silky", "grainy"]
42
+
43
  def measure(self, x, y):
44
  return self.textures[hash((x, y)) % len(self.textures)]
45
 
46
  class EMFieldSensor:
47
  def measure(self, x, y):
48
+ return np.sin(x / 50) * np.cos(y / 50) * 10 # simulated EM field
49
 
50
  # Create sensation map for the avatar
51
  def create_sensation_map(width, height):
52
  sensation_map = np.zeros((height, width, 7)) # RGBPVTE channels for pain, pleasure, neutral, pressure, velocity, temperature, and EM sensitivity
53
  for y in range(height):
54
  for x in range(width):
55
+ pain = np.exp(-((x - 100) ** 2 + (y - 150) ** 2) / 5000) + np.exp(-((x - 300) ** 2 + (y - 450) ** 2) / 5000)
56
+ pleasure = np.exp(-((x - 200) ** 2 + (y - 300) ** 2) / 5000) + np.exp(-((x - 100) ** 2 + (y - 500) ** 2) / 5000)
57
  neutral = 1 - (pain + pleasure)
58
+ pressure = np.exp(-((x - 50) ** 2 + (y - 150) ** 2) / 2000) + np.exp(-((x - 350) ** 2 + (y - 150) ** 2) / 2000) + \
59
+ np.exp(-((x - 100) ** 2 + (y - 550) ** 2) / 2000) + np.exp(-((x - 300) ** 2 + (y - 550) ** 2) / 2000)
60
+ velocity = np.exp(-((x - 200) ** 2 + (y - 100) ** 2) / 5000) + np.exp(-((x - 200) ** 2 + (y - 300) ** 2) / 5000)
61
+ temperature = np.exp(-((x - 200) ** 2 + (y - 200) ** 2) / 10000) # more sensitive in the core
62
+ em_sensitivity = np.exp(-((x - 200) ** 2 + (y - 100) ** 2) / 8000) # more sensitive in the head
63
+
64
  sensation_map[y, x] = [pain, pleasure, neutral, pressure, velocity, temperature, em_sensitivity]
65
+
66
  return sensation_map
67
 
68
  avatar_sensation_map = create_sensation_map(AVATAR_WIDTH, AVATAR_HEIGHT)
 
77
  def create_avatar():
78
  img = Image.new('RGB', (AVATAR_WIDTH, AVATAR_HEIGHT), color='white')
79
  draw = ImageDraw.Draw(img)
80
+
81
  # Head
82
  draw.ellipse([150, 50, 250, 150], fill='beige', outline='black')
83
+
84
  # Body
85
  draw.rectangle([175, 150, 225, 400], fill='beige', outline='black')
86
+
87
  # Arms
88
  draw.rectangle([125, 150, 175, 350], fill='beige', outline='black')
89
  draw.rectangle([225, 150, 275, 350], fill='beige', outline='black')
90
+
91
  # Legs
92
  draw.rectangle([175, 400, 200, 550], fill='beige', outline='black')
93
  draw.rectangle([200, 400, 225, 550], fill='beige', outline='black')
94
+
95
  return img
96
 
97
  avatar_image = create_avatar()
 
126
  def calculate_sensation(x, y, pressure, velocity):
127
  sensation = avatar_sensation_map[int(y), int(x)]
128
  pain, pleasure, neutral, pressure_sensitivity, velocity_sensitivity, temp_sensitivity, em_sensitivity = sensation
129
+
130
  measured_pressure = pressure_sensor.measure(pressure * pressure_sensitivity)
131
  measured_temp = temp_sensor.measure(37 + pressure * 5) # Simulating temperature increase with pressure
132
  measured_texture = texture_sensor.measure(x, y)
133
  measured_em = em_sensor.measure(x, y) * em_sensitivity
134
+
135
  modified_pain = pain * measured_pressure / 10
136
  modified_pleasure = pleasure * velocity * velocity_sensitivity
137
  modified_neutral = neutral * (1 - (measured_pressure + velocity) / 2)
138
+
139
  return modified_pain, modified_pleasure, modified_neutral, measured_pressure, measured_temp, measured_texture, measured_em
140
 
141
  def generate_description(x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em):
 
147
  Resulting in:
148
  Pain: {pain:.2f}, Pleasure: {pleasure:.2f}, Neutral: {neutral:.2f}
149
  Avatar:"""
150
+
151
  input_ids = tokenizer.encode(prompt, return_tensors="pt")
152
  output = model.generate(input_ids, max_length=200, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7)
153
+
154
+ return tokenizer.decode(output[0], skip_special_tokens=True).split("Avatar: ")[-1].strip()
155
 
156
  # Initialize session state
157
  if 'touch_history' not in st.session_state:
 
164
  new_points = objects[-1].get("points", [])
165
  if new_points:
166
  for i in range(1, len(new_points)):
167
+ x1, y1 = new_points[i - 1]["x"], new_points[i - 1]["y"]
168
  x2, y2 = new_points[i]["x"], new_points[i]["y"]
169
+
170
  # Calculate pressure and velocity
171
+ distance = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
172
  velocity = distance / 0.01 # Assuming 10ms between points
173
  pressure = 1 + velocity / 100 # Simple pressure model
174
+
175
  x, y = (x1 + x2) / 2, (y1 + y2) / 2
176
  pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em = calculate_sensation(x, y, pressure, velocity)
177
+
178
  st.session_state.touch_history.append((x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em))
179
 
180
  # Display touch history and generate descriptions
181
  if st.session_state.touch_history:
182
  st.subheader("Touch History and Sensations")
183
+ for x, y, pressure, velocity, pain, pleasure, neutral, measured_pressure, measured_temp, measured_texture, measured_em in
184
+ st.session_state.touch_history[-5:]:
185
  st.write(f"Touch at ({x:.1f}, {y:.1f})")
186
  st.write(f"Pressure: {measured_pressure:.2f}, Temperature: {measured_temp:.2f}°C")
187
  st.write(f"Texture: {measured_texture}, EM Field: {measured_em:.2f}")
 
196
  # Add a button to clear the touch history
197
  if st.button("Clear Touch History"):
198
  st.session_state.touch_history = []
199
+ st.experimental_rerun()