Sephfox commited on
Commit
58b97f2
·
verified ·
1 Parent(s): f09a10e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from typing import List, Tuple
3
  from transformers import pipeline
 
4
 
5
  # Constants
6
  WIDTH, HEIGHT = 600, 600
@@ -15,7 +16,11 @@ velocities: List[Tuple[float, float]] = [(0.0, 0.0)] * len(touch_points)
15
  is_affected: List[bool] = [False] * len(touch_points)
16
 
17
  # Set up the Hugging Face pipeline
18
- text_generator = pipeline('text-generation', model='gpt2')
 
 
 
 
19
 
20
  # Streamlit app
21
  st.title("Artificial Touch Simulation")
@@ -56,27 +61,33 @@ def on_tap(x, y):
56
  is_affected[i] = True
57
 
58
  # Generate a description of the touch
59
- with touch_container:
60
- st.write(f"Touch at ({x:.2f}, {y:.2f})")
61
- 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']
62
- st.write(text)
63
 
64
  update_points()
65
 
66
- while True:
67
- with touch_container:
68
- for i, (x, y) in enumerate(touch_points):
69
- if is_affected[i]:
70
- st.circle((x, y), 5, color="red", fill_opacity=0.5)
71
- else:
72
- st.circle((x, y), 5, color="navy", fill_opacity=0.5)
73
-
74
- if st.button("Tap the screen"):
75
- on_tap(st.session_state.get('x', 0), st.session_state.get('y', 0))
76
-
77
- st.experimental_set_query_params(x=touch_points[0][0], y=touch_points[0][1])
78
- st.session_state['x'] = st.experimental_get_query_params().get('x', [0])[0]
79
- st.session_state['y'] = st.experimental_get_query_params().get('y', [0])[0]
80
-
81
- update_points()
82
- st.experimental_rerun()
 
 
 
 
 
 
 
 
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
 
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():
21
+ return pipeline('text-generation', model='gpt2')
22
+
23
+ text_generator = load_model()
24
 
25
  # Streamlit app
26
  st.title("Artificial Touch Simulation")
 
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()
69
 
70
+ # Initialize session state
71
+ 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))
78
+ ax.set_xlim(0, WIDTH)
79
+ ax.set_ylim(0, HEIGHT)
80
+
81
+ 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()