Sephfox commited on
Commit
c5987cc
·
verified ·
1 Parent(s): f3c5be8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -9
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import streamlit as st
 
2
  from typing import List, Tuple
3
  from transformers import pipeline
4
  import matplotlib.pyplot as plt
 
5
 
6
  # Constants
7
  WIDTH, HEIGHT = 600, 600
@@ -15,6 +17,10 @@ original_points = touch_points.copy()
15
  velocities: List[Tuple[float, float]] = [(0.0, 0.0)] * len(touch_points)
16
  is_affected: List[bool] = [False] * len(touch_points)
17
 
 
 
 
 
18
  # Set up the Hugging Face pipeline
19
  @st.cache_resource
20
  def load_model():
@@ -23,7 +29,7 @@ def load_model():
23
  text_generator = load_model()
24
 
25
  # Streamlit app
26
- st.title("Artificial Touch Simulation")
27
 
28
  # Create a Streamlit container for the touch simulation
29
  touch_container = st.container()
@@ -49,7 +55,19 @@ def update_points():
49
  # Reset affected flags
50
  is_affected = [False] * len(touch_points)
51
 
52
- def on_tap(x, y):
 
 
 
 
 
 
 
 
 
 
 
 
53
  global touch_points, velocities, is_affected
54
 
55
  for i, (tx, ty) in enumerate(touch_points):
@@ -57,12 +75,17 @@ def on_tap(x, y):
57
  if distance < 30:
58
  force_x = (tx - x) / distance
59
  force_y = (ty - y) / distance
60
- velocities[i] = (velocities[i][0] - force_x * 10, velocities[i][1] - force_y * 10)
61
  is_affected[i] = True
62
 
 
 
63
  # Generate a description of the touch
64
- st.write(f"Touch at ({x:.2f}, {y:.2f})")
65
- text = text_generator(f"The user touched the screen at ({x:.2f}, {y:.2f}).", max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95, num_beams=1)[0]['generated_text']
 
 
 
66
  st.write(text)
67
 
68
  update_points()
@@ -72,6 +95,12 @@ if 'x' not in st.session_state:
72
  st.session_state.x = 0
73
  if 'y' not in st.session_state:
74
  st.session_state.y = 0
 
 
 
 
 
 
75
 
76
  # Main app logic
77
  fig, ax = plt.subplots(figsize=(6, 6))
@@ -82,12 +111,29 @@ for i, (x, y) in enumerate(touch_points):
82
  color = "red" if is_affected[i] else "navy"
83
  ax.add_artist(plt.Circle((x, y), 5, color=color, alpha=0.5))
84
 
 
 
 
 
 
 
85
  touch_container.pyplot(fig)
86
 
87
- if touch_container.button("Tap the screen"):
88
- on_tap(st.session_state.x, st.session_state.y)
 
 
 
 
 
 
 
89
 
90
- st.session_state.x = st.slider("X coordinate", 0, WIDTH, st.session_state.x)
91
- st.session_state.y = st.slider("Y coordinate", 0, HEIGHT, st.session_state.y)
 
 
 
 
92
 
93
  update_points()
 
1
  import streamlit as st
2
+ import numpy as np
3
  from typing import List, Tuple
4
  from transformers import pipeline
5
  import matplotlib.pyplot as plt
6
+ import time
7
 
8
  # Constants
9
  WIDTH, HEIGHT = 600, 600
 
17
  velocities: List[Tuple[float, float]] = [(0.0, 0.0)] * len(touch_points)
18
  is_affected: List[bool] = [False] * len(touch_points)
19
 
20
+ # Create pain and pleasure points
21
+ pain_points = [(100, 100), (500, 500)]
22
+ pleasure_points = [(300, 300), (200, 400)]
23
+
24
  # Set up the Hugging Face pipeline
25
  @st.cache_resource
26
  def load_model():
 
29
  text_generator = load_model()
30
 
31
  # Streamlit app
32
+ st.title("Advanced Artificial Touch Simulation")
33
 
34
  # Create a Streamlit container for the touch simulation
35
  touch_container = st.container()
 
55
  # Reset affected flags
56
  is_affected = [False] * len(touch_points)
57
 
58
+ def calculate_sensation(x, y, pressure, duration):
59
+ sensation = 0
60
+ for px, py in pain_points:
61
+ distance = np.sqrt((x - px)**2 + (y - py)**2)
62
+ if distance < 50:
63
+ sensation -= (50 - distance) * pressure * (duration ** 1.5) / 50
64
+ for px, py in pleasure_points:
65
+ distance = np.sqrt((x - px)**2 + (y - py)**2)
66
+ if distance < 50:
67
+ sensation += (50 - distance) * pressure / 50
68
+ return sensation
69
+
70
+ def on_tap(x, y, pressure, duration):
71
  global touch_points, velocities, is_affected
72
 
73
  for i, (tx, ty) in enumerate(touch_points):
 
75
  if distance < 30:
76
  force_x = (tx - x) / distance
77
  force_y = (ty - y) / distance
78
+ velocities[i] = (velocities[i][0] - force_x * 10 * pressure, velocities[i][1] - force_y * 10 * pressure)
79
  is_affected[i] = True
80
 
81
+ sensation = calculate_sensation(x, y, pressure, duration)
82
+
83
  # Generate a description of the touch
84
+ st.write(f"Touch at ({x:.2f}, {y:.2f}) with pressure {pressure:.2f} for {duration:.2f} seconds")
85
+ st.write(f"Sensation: {sensation:.2f}")
86
+
87
+ prompt = f"The user touched the screen at ({x:.2f}, {y:.2f}) with a pressure of {pressure:.2f} for {duration:.2f} seconds, resulting in a sensation of {sensation:.2f}. Describe the experience:"
88
+ text = text_generator(prompt, max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95, num_beams=1)[0]['generated_text']
89
  st.write(text)
90
 
91
  update_points()
 
95
  st.session_state.x = 0
96
  if 'y' not in st.session_state:
97
  st.session_state.y = 0
98
+ if 'pressure' not in st.session_state:
99
+ st.session_state.pressure = 1.0
100
+ if 'duration' not in st.session_state:
101
+ st.session_state.duration = 0.0
102
+ if 'touch_start_time' not in st.session_state:
103
+ st.session_state.touch_start_time = None
104
 
105
  # Main app logic
106
  fig, ax = plt.subplots(figsize=(6, 6))
 
111
  color = "red" if is_affected[i] else "navy"
112
  ax.add_artist(plt.Circle((x, y), 5, color=color, alpha=0.5))
113
 
114
+ for x, y in pain_points:
115
+ ax.add_artist(plt.Circle((x, y), 10, color="red", alpha=0.3))
116
+
117
+ for x, y in pleasure_points:
118
+ ax.add_artist(plt.Circle((x, y), 10, color="green", alpha=0.3))
119
+
120
  touch_container.pyplot(fig)
121
 
122
+ col1, col2 = st.columns(2)
123
+ with col1:
124
+ st.session_state.x = st.slider("X coordinate", 0, WIDTH, st.session_state.x)
125
+ st.session_state.y = st.slider("Y coordinate", 0, HEIGHT, st.session_state.y)
126
+ with col2:
127
+ st.session_state.pressure = st.slider("Pressure", 0.1, 2.0, st.session_state.pressure)
128
+
129
+ if touch_container.button("Start Touch"):
130
+ st.session_state.touch_start_time = time.time()
131
 
132
+ if touch_container.button("End Touch"):
133
+ if st.session_state.touch_start_time is not None:
134
+ st.session_state.duration = time.time() - st.session_state.touch_start_time
135
+ on_tap(st.session_state.x, st.session_state.y, st.session_state.pressure, st.session_state.duration)
136
+ st.session_state.touch_start_time = None
137
+ st.session_state.duration = 0.0
138
 
139
  update_points()