rishab1090 commited on
Commit
fc4a48e
·
verified ·
1 Parent(s): f66402a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -22,18 +22,16 @@ def handle_trace(trace):
22
  tr = stream[-1]
23
  buffer.extend(tr.data.tolist())
24
 
25
- # Dummy fallback generator
26
  def dummy_data():
27
  global buffer
28
  while True:
29
  if use_dummy:
30
  data = [int(random.gauss(0, 500)) for _ in range(200)]
31
  if random.random() < 0.01:
32
- data = [x * 20 for x in data] # quake spike
33
  buffer.extend(data)
34
  time.sleep(1)
35
 
36
- # Start ObsPy client for given station
37
  def start_obspy():
38
  global selected_station
39
  try:
@@ -48,7 +46,6 @@ def start_obspy():
48
  except Exception as e:
49
  print("⚠️ ObsPy failed, using dummy mode:", e)
50
 
51
- # Function to return waveform plot + alert
52
  def stream_waveform():
53
  global buffer
54
  fig, ax = plt.subplots()
@@ -64,34 +61,29 @@ def stream_waveform():
64
  ax.set_axis_off()
65
  return fig, "Waiting for data..."
66
 
67
- # Change station dynamically
68
  def change_station(station_choice):
69
  global selected_station, buffer, stream, use_dummy
70
  buffer = []
71
  stream = Stream()
72
  use_dummy = True
73
-
74
  net, sta, cha = station_choice.split(".")
75
  selected_station = {"network": net, "station": sta, "channel": cha}
76
  threading.Thread(target=start_obspy, daemon=True).start()
77
  return f"📡 Switched to station: {station_choice}"
78
 
79
- # Start background dummy thread
80
  threading.Thread(target=dummy_data, daemon=True).start()
81
- # Start ObsPy default
82
  threading.Thread(target=start_obspy, daemon=True).start()
83
 
84
- # List of some popular global IRIS stations
85
  STATIONS = [
86
- "IU.ANMO.00BHZ", # Albuquerque, New Mexico
87
- "IU.TUC.00BHZ", # Tucson, Arizona
88
- "IU.COLA.00BHZ", # College, Alaska
89
- "IU.KONO.00BHZ", # Kongsberg, Norway
90
- "IU.KEV.00BHZ", # Kevo, Finland
91
- "IU.DWPF.00BHZ", # Disney Wildlife Preserve, Florida
92
  ]
93
 
94
- # Gradio UI
95
  with gr.Blocks() as demo:
96
  gr.Markdown("## 🌍 Live Seismic Waveform (IRIS Stations)")
97
 
@@ -101,11 +93,11 @@ with gr.Blocks() as demo:
101
  chart = gr.Plot()
102
  alert_box = gr.Label(label="Status")
103
 
104
- # Switch station callback
105
  station_dropdown.change(fn=change_station, inputs=station_dropdown, outputs=switch_status)
106
 
107
- # Update waveform every 1s
108
- demo.load(stream_waveform, None, [chart, alert_box], every=1)
 
109
 
110
  if __name__ == "__main__":
111
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
22
  tr = stream[-1]
23
  buffer.extend(tr.data.tolist())
24
 
 
25
  def dummy_data():
26
  global buffer
27
  while True:
28
  if use_dummy:
29
  data = [int(random.gauss(0, 500)) for _ in range(200)]
30
  if random.random() < 0.01:
31
+ data = [x * 20 for x in data]
32
  buffer.extend(data)
33
  time.sleep(1)
34
 
 
35
  def start_obspy():
36
  global selected_station
37
  try:
 
46
  except Exception as e:
47
  print("⚠️ ObsPy failed, using dummy mode:", e)
48
 
 
49
  def stream_waveform():
50
  global buffer
51
  fig, ax = plt.subplots()
 
61
  ax.set_axis_off()
62
  return fig, "Waiting for data..."
63
 
 
64
  def change_station(station_choice):
65
  global selected_station, buffer, stream, use_dummy
66
  buffer = []
67
  stream = Stream()
68
  use_dummy = True
 
69
  net, sta, cha = station_choice.split(".")
70
  selected_station = {"network": net, "station": sta, "channel": cha}
71
  threading.Thread(target=start_obspy, daemon=True).start()
72
  return f"📡 Switched to station: {station_choice}"
73
 
74
+ # Start background threads
75
  threading.Thread(target=dummy_data, daemon=True).start()
 
76
  threading.Thread(target=start_obspy, daemon=True).start()
77
 
 
78
  STATIONS = [
79
+ "IU.ANMO.00BHZ",
80
+ "IU.TUC.00BHZ",
81
+ "IU.COLA.00BHZ",
82
+ "IU.KONO.00BHZ",
83
+ "IU.KEV.00BHZ",
84
+ "IU.DWPF.00BHZ",
85
  ]
86
 
 
87
  with gr.Blocks() as demo:
88
  gr.Markdown("## 🌍 Live Seismic Waveform (IRIS Stations)")
89
 
 
93
  chart = gr.Plot()
94
  alert_box = gr.Label(label="Status")
95
 
 
96
  station_dropdown.change(fn=change_station, inputs=station_dropdown, outputs=switch_status)
97
 
98
+ # Timer replaces every=
99
+ timer = gr.Timer(1.0)
100
+ timer.tick(stream_waveform, None, [chart, alert_box])
101
 
102
  if __name__ == "__main__":
103
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)