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

timeout added

Browse files

Fixing previous bug

Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -6,6 +6,7 @@ import gradio as gr
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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"
@@ -85,13 +86,36 @@ def generate_art(mood):
85
  return image
86
 
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  def mood_art_generator(image):
89
  mood = get_mood_from_image(image)
90
  print("Mood:", mood)
91
  if mood:
92
- art = generate_art(mood)
93
- output_text = f"You seem to be {mood}. Here's an artwork representing it!"
94
- return art, output_text
 
 
 
95
  else:
96
  return None, "Failed to generate artwork."
97
 
 
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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"
 
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