Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import multiprocessing
|
|
|
3 |
import time
|
|
|
4 |
from math import isqrt
|
5 |
|
|
|
|
|
|
|
6 |
# Function to check if a number is prime
|
7 |
def is_prime(n):
|
8 |
if n < 2:
|
@@ -16,23 +21,35 @@ def is_prime(n):
|
|
16 |
return False
|
17 |
return True
|
18 |
|
19 |
-
# Function to find
|
20 |
def find_primes_in_range(start, end, queue):
|
21 |
primes = [str(num) for num in range(start, end) if is_prime(num)]
|
22 |
queue.put(primes)
|
23 |
|
24 |
-
# Function
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
while True:
|
32 |
processes = []
|
33 |
queue = multiprocessing.Queue()
|
34 |
|
35 |
-
#
|
36 |
for i in range(num_processes):
|
37 |
start = num + i * batch_size
|
38 |
end = start + batch_size
|
@@ -40,24 +57,38 @@ def start_prime_printer():
|
|
40 |
process.start()
|
41 |
processes.append(process)
|
42 |
|
43 |
-
# Collect
|
44 |
for process in processes:
|
45 |
-
process.join()
|
46 |
-
primes = queue.get()
|
47 |
-
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
|
52 |
num += num_processes * batch_size
|
53 |
-
time.sleep(0.1) # Adjust
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Gradio Interface
|
56 |
interface = gr.Interface(
|
57 |
-
fn=
|
58 |
-
inputs=None,
|
59 |
-
outputs="text",
|
60 |
-
live=True
|
61 |
)
|
62 |
|
63 |
# Launch the interface
|
|
|
1 |
import gradio as gr
|
2 |
import multiprocessing
|
3 |
+
import threading
|
4 |
import time
|
5 |
+
import os
|
6 |
from math import isqrt
|
7 |
|
8 |
+
# Path to save the primes
|
9 |
+
PRIME_SAVE_PATH = "primes.txt"
|
10 |
+
|
11 |
# Function to check if a number is prime
|
12 |
def is_prime(n):
|
13 |
if n < 2:
|
|
|
21 |
return False
|
22 |
return True
|
23 |
|
24 |
+
# Function to find primes in a given range
|
25 |
def find_primes_in_range(start, end, queue):
|
26 |
primes = [str(num) for num in range(start, end) if is_prime(num)]
|
27 |
queue.put(primes)
|
28 |
|
29 |
+
# Function to load previously saved primes
|
30 |
+
def load_saved_primes():
|
31 |
+
if os.path.exists(PRIME_SAVE_PATH):
|
32 |
+
with open(PRIME_SAVE_PATH, "r") as file:
|
33 |
+
return file.read().splitlines()
|
34 |
+
return []
|
35 |
+
|
36 |
+
# Function to save new primes to file
|
37 |
+
def save_primes_to_file(prime_list):
|
38 |
+
with open(PRIME_SAVE_PATH, "a") as file:
|
39 |
+
file.write("\n".join(prime_list) + "\n")
|
40 |
|
41 |
+
# Background prime number generator function
|
42 |
+
def prime_generator():
|
43 |
+
num = 2
|
44 |
+
batch_size = 1000
|
45 |
+
num_processes = multiprocessing.cpu_count()
|
46 |
+
prime_list = load_saved_primes() # Load previously generated primes
|
47 |
+
|
48 |
while True:
|
49 |
processes = []
|
50 |
queue = multiprocessing.Queue()
|
51 |
|
52 |
+
# Start multiple processes to find primes in parallel
|
53 |
for i in range(num_processes):
|
54 |
start = num + i * batch_size
|
55 |
end = start + batch_size
|
|
|
57 |
process.start()
|
58 |
processes.append(process)
|
59 |
|
60 |
+
# Collect primes from all processes
|
61 |
for process in processes:
|
62 |
+
process.join()
|
63 |
+
primes = queue.get()
|
64 |
+
prime_list.extend(primes)
|
65 |
|
66 |
+
# Save the newly found primes to the file
|
67 |
+
save_primes_to_file(primes)
|
68 |
|
69 |
num += num_processes * batch_size
|
70 |
+
time.sleep(0.1) # Adjust this to control responsiveness and resource usage
|
71 |
+
|
72 |
+
def start_background_thread():
|
73 |
+
thread = threading.Thread(target=prime_generator, daemon=True)
|
74 |
+
thread.start()
|
75 |
+
|
76 |
+
# Function to display the primes in Gradio
|
77 |
+
def display_primes():
|
78 |
+
prime_list = load_saved_primes()
|
79 |
+
while True:
|
80 |
+
yield "\n".join(prime_list)
|
81 |
+
time.sleep(1) # Refresh interval
|
82 |
+
|
83 |
+
# Start the background prime number generation
|
84 |
+
start_background_thread()
|
85 |
|
86 |
# Gradio Interface
|
87 |
interface = gr.Interface(
|
88 |
+
fn=display_primes, # Display function
|
89 |
+
inputs=None, # No inputs required
|
90 |
+
outputs="text", # Output is a text field
|
91 |
+
live=True # Enable live updates
|
92 |
)
|
93 |
|
94 |
# Launch the interface
|