Afrinetwork7 commited on
Commit
af29733
1 Parent(s): 518cd76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -4
app.py CHANGED
@@ -4,6 +4,7 @@ import uuid
4
  from urllib.parse import quote
5
  from requests import get
6
  from bs4 import BeautifulSoup
 
7
 
8
  import gradio as gr
9
  import numpy as np
@@ -12,7 +13,7 @@ import spaces
12
  import torch
13
  from diffusers import DiffusionPipeline
14
 
15
- DESCRIPTION = """# Playground v2.5"""
16
  if not torch.cuda.is_available():
17
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
18
 
@@ -78,6 +79,8 @@ def translate_to_english(phrase, src_lang):
78
 
79
 
80
  @spaces.GPU(enable_queue=True)
 
 
81
  def generate(
82
  phrase: str,
83
  input_lang: str,
@@ -88,7 +91,6 @@ def generate(
88
  height: int = 1024,
89
  guidance_scale: float = 3,
90
  randomize_seed: bool = False,
91
- use_resolution_binning: bool = True,
92
  progress=gr.Progress(track_tqdm=True),
93
  ):
94
  pipe.to(device)
@@ -103,6 +105,14 @@ def generate(
103
  if not use_negative_prompt:
104
  negative_prompt = None # type: ignore
105
 
 
 
 
 
 
 
 
 
106
  images = pipe(
107
  prompt=prompt,
108
  negative_prompt=negative_prompt,
@@ -118,12 +128,19 @@ def generate(
118
 
119
  image_paths = [save_image(img) for img in images]
120
  print(image_paths)
 
 
 
 
 
 
 
121
  return image_paths, seed
122
 
123
 
124
  examples = [
125
- ["neon holography crystal cat", "en"],
126
- ["a cat eating a piece of cheese", "en"],
127
  ["an astronaut riding a horse in space", "en"],
128
  ["a cartoon of a boy playing with a tiger", "en"],
129
  ["a cute robot artist painting on an easel, concept art", "en"],
 
4
  from urllib.parse import quote
5
  from requests import get
6
  from bs4 import BeautifulSoup
7
+ import hashlib
8
 
9
  import gradio as gr
10
  import numpy as np
 
13
  import torch
14
  from diffusers import DiffusionPipeline
15
 
16
+ DESCRIPTION = """# Playground v2.5Afri Ai Image"""
17
  if not torch.cuda.is_available():
18
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
19
 
 
79
 
80
 
81
  @spaces.GPU(enable_queue=True)
82
+ CACHE_DIR = "image_cache"
83
+
84
  def generate(
85
  phrase: str,
86
  input_lang: str,
 
91
  height: int = 1024,
92
  guidance_scale: float = 3,
93
  randomize_seed: bool = False,
 
94
  progress=gr.Progress(track_tqdm=True),
95
  ):
96
  pipe.to(device)
 
105
  if not use_negative_prompt:
106
  negative_prompt = None # type: ignore
107
 
108
+ # Hash the prompt and seed to create a unique cache key
109
+ cache_key = hashlib.sha256(f"{prompt}{seed}".encode()).hexdigest()
110
+
111
+ # Check if the image is already in the cache
112
+ if os.path.exists(os.path.join(CACHE_DIR, f"{cache_key}.png"))):
113
+ print("Image found in cache")
114
+ return [os.path.join(CACHE_DIR, f"{cache_key}.png")], seed
115
+
116
  images = pipe(
117
  prompt=prompt,
118
  negative_prompt=negative_prompt,
 
128
 
129
  image_paths = [save_image(img) for img in images]
130
  print(image_paths)
131
+
132
+ # Save the first image to the cache directory
133
+ if not os.path.exists(CACHE_DIR):
134
+ os.makedirs(CACHE_DIR)
135
+ cache_path = os.path.join(CACHE_DIR, f"{cache_key}.png")
136
+ Image.open(image_paths[0]).save(cache_path)
137
+
138
  return image_paths, seed
139
 
140
 
141
  examples = [
142
+ ["nyɔnu e sa tomati ɖo aximɛ", "fon"],
143
+ ["ọba ilẹ̀ benin kan", "yo"],
144
  ["an astronaut riding a horse in space", "en"],
145
  ["a cartoon of a boy playing with a tiger", "en"],
146
  ["a cute robot artist painting on an easel, concept art", "en"],