harishaseebat92 commited on
Commit
b23b6f1
·
verified ·
1 Parent(s): 8fb4891

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -1,9 +1,25 @@
 
 
1
  import numpy as np
2
  import plotly.graph_objects as go
3
  import gradio as gr
4
  from scipy.spatial import Delaunay
5
  import traceback
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def solve_and_plot_interactive(Lx: float,
8
  Ly: float,
9
  t_max: float,
@@ -14,8 +30,7 @@ def solve_and_plot_interactive(Lx: float,
14
  initial: str = "gaussian",
15
  bc: str = "dirichlet"):
16
  """
17
- Solves the 2D heat equation and returns an interactive Plotly figure
18
- that can be rendered in a web browser.
19
  """
20
  # --- 1. Simulation Setup ---
21
  x = np.linspace(0, Lx, Nx)
@@ -114,14 +129,10 @@ def gradio_interface(lx, ly, t_max, m_steps, gamma, nx, ny, initial, bc):
114
  Lx=lx, Ly=ly, t_max=t_max, M=m_steps, Gamma=gamma, Nx=nx, Ny=ny,
115
  initial=initial, bc=bc
116
  )
117
- # On success, return the figure to the Plot component
118
  return fig
119
  except Exception as e:
120
- # If any error occurs, create a text-based error to show in the UI
121
- # This prevents the app from crashing.
122
  error_text = traceback.format_exc()
123
- print(error_text) # Print the full error to the logs for debugging
124
- # Return a placeholder Plotly figure with the error message
125
  error_fig = go.Figure().update_layout(
126
  title_text="⚠️ Application Error",
127
  annotations=[dict(text=f"An error occurred: {e}", showarrow=False)]
@@ -136,7 +147,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="2D Heat Simulator") as demo:
136
  with gr.Row():
137
  with gr.Column(scale=1):
138
  gr.Markdown("## Simulation Parameters")
139
- # Reduced max values to be safer on free hardware
140
  lx_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lx")
141
  ly_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Ly")
142
  nx_slider = gr.Slider(10, 80, 40, 1, label="Nx (Grid Points X)")
@@ -160,7 +170,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="2D Heat Simulator") as demo:
160
  run_btn.click(fn=gradio_interface, inputs=inputs_list, outputs=plot_output)
161
 
162
  gr.Examples(
163
- # Using more modest values in the examples
164
  examples=[
165
  [1.0, 1.0, 0.5, 30, 0.1, 30, 30, "gaussian", "dirichlet"],
166
  [2.0, 1.0, 1.0, 40, 0.05, 40, 20, "sinusoidal", "periodic"],
@@ -168,7 +177,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="2D Heat Simulator") as demo:
168
  inputs=inputs_list,
169
  outputs=plot_output,
170
  fn=gradio_interface,
171
- # Set cache_examples to False to prevent crashes on startup
172
  cache_examples=False
173
  )
174
 
 
1
+ import csv
2
+ import sys
3
  import numpy as np
4
  import plotly.graph_objects as go
5
  import gradio as gr
6
  from scipy.spatial import Delaunay
7
  import traceback
8
 
9
+ # --- FIX for CSV Field Size Limit ---
10
+ # Increase the CSV field size limit to handle large Plotly JSON objects that are
11
+ # generated when Gradio's Examples component tries to log the output.
12
+ # This is the most robust way to set the limit to the maximum supported by the system.
13
+ max_int = sys.maxsize
14
+ while True:
15
+ try:
16
+ csv.field_size_limit(max_int)
17
+ break
18
+ except OverflowError:
19
+ max_int = int(max_int / 10)
20
+ # -----------------------------------------
21
+
22
+
23
  def solve_and_plot_interactive(Lx: float,
24
  Ly: float,
25
  t_max: float,
 
30
  initial: str = "gaussian",
31
  bc: str = "dirichlet"):
32
  """
33
+ Solves the 2D heat equation and returns an interactive Plotly figure.
 
34
  """
35
  # --- 1. Simulation Setup ---
36
  x = np.linspace(0, Lx, Nx)
 
129
  Lx=lx, Ly=ly, t_max=t_max, M=m_steps, Gamma=gamma, Nx=nx, Ny=ny,
130
  initial=initial, bc=bc
131
  )
 
132
  return fig
133
  except Exception as e:
 
 
134
  error_text = traceback.format_exc()
135
+ print(error_text)
 
136
  error_fig = go.Figure().update_layout(
137
  title_text="⚠️ Application Error",
138
  annotations=[dict(text=f"An error occurred: {e}", showarrow=False)]
 
147
  with gr.Row():
148
  with gr.Column(scale=1):
149
  gr.Markdown("## Simulation Parameters")
 
150
  lx_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lx")
151
  ly_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Ly")
152
  nx_slider = gr.Slider(10, 80, 40, 1, label="Nx (Grid Points X)")
 
170
  run_btn.click(fn=gradio_interface, inputs=inputs_list, outputs=plot_output)
171
 
172
  gr.Examples(
 
173
  examples=[
174
  [1.0, 1.0, 0.5, 30, 0.1, 30, 30, "gaussian", "dirichlet"],
175
  [2.0, 1.0, 1.0, 40, 0.05, 40, 20, "sinusoidal", "periodic"],
 
177
  inputs=inputs_list,
178
  outputs=plot_output,
179
  fn=gradio_interface,
 
180
  cache_examples=False
181
  )
182