Ahil1991 commited on
Commit
e8dd6d0
·
verified ·
1 Parent(s): def393d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -19
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 the next prime number in a given range
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 that utilizes multiple processes to find primes
25
- def start_prime_printer():
26
- num = 2 # Start from the first prime
27
- batch_size = 1000 # Size of the range each process will handle
28
- output = []
29
- num_processes = multiprocessing.cpu_count() # Utilize all available CPU cores
 
 
 
 
 
30
 
 
 
 
 
 
 
 
31
  while True:
32
  processes = []
33
  queue = multiprocessing.Queue()
34
 
35
- # Create and start processes
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 results from all processes
44
  for process in processes:
45
- process.join() # Wait for process to finish
46
- primes = queue.get() # Get primes from this process
47
- output.extend(primes)
48
 
49
- # Display the collected prime numbers
50
- yield "\n".join(output)
51
 
52
  num += num_processes * batch_size
53
- time.sleep(0.1) # Adjust the sleep time if needed to manage UI responsiveness
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  # Gradio Interface
56
  interface = gr.Interface(
57
- fn=start_prime_printer, # Function to execute
58
- inputs=None, # No input required
59
- outputs="text", # Output type
60
- live=True # Enable real-time updates
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