Kyan14 commited on
Commit
85fd9d1
·
1 Parent(s): 3e0d108

Reverting back due to no image being produced

Browse files
Files changed (1) hide show
  1. app.py +16 -47
app.py CHANGED
@@ -6,7 +6,6 @@ import gradio as gr
6
  from transformers import CLIPProcessor, CLIPModel
7
  import numpy as np
8
  import time
9
- import threading
10
 
11
  # Replace with your own API key
12
  STABLE_DIFFUSION_API_KEY = "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"
@@ -45,10 +44,6 @@ def get_mood_from_image(image: Image.Image):
45
 
46
  return selected_mood
47
 
48
- def is_black_image(image: Image.Image) -> bool:
49
- img_array = np.array(image)
50
- return np.all(img_array == 0)
51
-
52
  def generate_art(mood):
53
  # Implement art generation logic using the Stable Diffusion API
54
  prompt = f"{mood} generative art with vibrant colors and intricate patterns ({str(np.random.randint(1, 10000))})"
@@ -63,59 +58,33 @@ def generate_art(mood):
63
  }
64
 
65
  while True:
66
- while True:
67
- response = requests.post('https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5', headers=headers, json=json_data)
68
-
69
- if response.status_code == 503:
70
- print("Model is loading, waiting for 30 seconds before retrying...")
71
- time.sleep(30)
72
- continue
73
-
74
- if response.status_code != 200:
75
- print(f"Error: API response status code {response.status_code}")
76
- print("Response content:")
77
- print(response.content)
78
- return None
79
-
80
- image = Image.open(BytesIO(response.content))
81
-
82
- # Check if the image is black
83
- if not is_black_image(image):
84
- break
85
-
86
- return image
87
 
 
 
 
 
88
 
89
- import threading
 
 
 
 
90
 
91
- class ArtGenerationTimeout(Exception):
92
- pass
93
 
94
- def generate_art_with_timeout(mood, timeout=120):
95
- result = {}
96
 
97
- def generate_and_store_result():
98
- result['art'] = generate_art(mood)
99
-
100
- thread = threading.Thread(target=generate_and_store_result)
101
- thread.start()
102
- thread.join(timeout)
103
 
104
- if thread.is_alive():
105
- raise ArtGenerationTimeout()
106
- else:
107
- return result['art']
108
 
109
  def mood_art_generator(image):
110
  mood = get_mood_from_image(image)
111
  print("Mood:", mood)
112
  if mood:
113
- try:
114
- art = generate_art_with_timeout(mood)
115
- output_text = f"You seem to be {mood}. Here's an artwork representing it!"
116
- return art, output_text
117
- except ArtGenerationTimeout:
118
- return None, "Art generation took too long. Please try again."
119
  else:
120
  return None, "Failed to generate artwork."
121
 
 
6
  from transformers import CLIPProcessor, CLIPModel
7
  import numpy as np
8
  import time
 
9
 
10
  # Replace with your own API key
11
  STABLE_DIFFUSION_API_KEY = "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"
 
44
 
45
  return selected_mood
46
 
 
 
 
 
47
  def generate_art(mood):
48
  # Implement art generation logic using the Stable Diffusion API
49
  prompt = f"{mood} generative art with vibrant colors and intricate patterns ({str(np.random.randint(1, 10000))})"
 
58
  }
59
 
60
  while True:
61
+ response = requests.post('https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5', headers=headers, json=json_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ if response.status_code == 503:
64
+ print("Model is loading, waiting for 30 seconds before retrying...")
65
+ time.sleep(30)
66
+ continue
67
 
68
+ if response.status_code != 200:
69
+ print(f"Error: API response status code {response.status_code}")
70
+ print("Response content:")
71
+ print(response.content)
72
+ return None
73
 
74
+ break
 
75
 
76
+ image = Image.open(BytesIO(response.content))
 
77
 
78
+ return image
 
 
 
 
 
79
 
 
 
 
 
80
 
81
  def mood_art_generator(image):
82
  mood = get_mood_from_image(image)
83
  print("Mood:", mood)
84
  if mood:
85
+ art = generate_art(mood)
86
+ output_text = f"You seem to be {mood}. Here's an artwork representing it!"
87
+ return art, output_text
 
 
 
88
  else:
89
  return None, "Failed to generate artwork."
90