hsuwill000 commited on
Commit
6298bb9
·
verified ·
1 Parent(s): 7b9aa9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -2,9 +2,8 @@ import gradio as gr
2
  from gradio_client import Client
3
  from PIL import Image
4
  import os
5
- import traceback
6
- import random
7
  import time
 
8
 
9
  # Create Client instances for the repositories
10
  clients = [
@@ -15,13 +14,15 @@ clients = [
15
  # Counter for image filenames to avoid overwriting
16
  count = 0
17
 
 
 
 
18
  # Gradio Interface Function to handle image generation
19
  def infer_gradio(prompt: str):
20
- global count
21
- random.seed(time.time())
22
- # Randomly choose a client
23
- client = random.choice(clients)
24
-
25
  # Prepare the inputs for the prediction
26
  inputs = {
27
  "prompt": prompt,
@@ -32,8 +33,8 @@ def infer_gradio(prompt: str):
32
  # Send the request to the model and receive the result (image URL or file path)
33
  result = client.predict(inputs, api_name="/infer")
34
 
35
- # Assuming the result is a URL or path, use it to open the image
36
- image = Image.open(result) # If the result is a URL, ensure you download it first
37
 
38
  # Create a unique filename to save the image
39
  filename = f"img_{count:08d}.jpg"
@@ -46,6 +47,9 @@ def infer_gradio(prompt: str):
46
  print(f"Saved image as {filename}")
47
 
48
  # Return the image to be displayed in Gradio
 
 
 
49
  return image
50
 
51
  except Exception as e:
 
2
  from gradio_client import Client
3
  from PIL import Image
4
  import os
 
 
5
  import time
6
+ import traceback
7
 
8
  # Create Client instances for the repositories
9
  clients = [
 
14
  # Counter for image filenames to avoid overwriting
15
  count = 0
16
 
17
+ # Global counter for selecting clients in order
18
+ client_index = 0
19
+
20
  # Gradio Interface Function to handle image generation
21
  def infer_gradio(prompt: str):
22
+ global count, client_index
23
+ # Select the current client based on the client_index
24
+ client = clients[client_index]
25
+
 
26
  # Prepare the inputs for the prediction
27
  inputs = {
28
  "prompt": prompt,
 
33
  # Send the request to the model and receive the result (image URL or file path)
34
  result = client.predict(inputs, api_name="/infer")
35
 
36
+ # Open the resulting image
37
+ image = Image.open(result)
38
 
39
  # Create a unique filename to save the image
40
  filename = f"img_{count:08d}.jpg"
 
47
  print(f"Saved image as {filename}")
48
 
49
  # Return the image to be displayed in Gradio
50
+ # Update the client_index to use the next client in the next call
51
+ client_index = (client_index + 1) % len(clients) # Cycle through clients
52
+
53
  return image
54
 
55
  except Exception as e: