RoniFinTech commited on
Commit
98b8e77
1 Parent(s): 0536019
cache/local_cache.py CHANGED
@@ -27,17 +27,14 @@ def ttl_cache(key_name, media_type=None, ttl_secs=20):
27
  # if media_type == 'image/png':
28
  # return StreamingResponse(BytesIO(_cache[key]), media_type=media_type)
29
  # else:
30
- return _cache[key]
31
 
32
  # Call the actual function if not in cache or expired
33
- response: StreamingResponse = await func(*args, **kwargs)
34
  # Cache the content of the response's body.
35
- content = response.body
36
- _cache[key] = content
37
  _cache_time[key] = datetime.now()
38
 
39
- # Reset the stream to its original state so it can be consumed by the caller
40
- response.body.seek(0)
41
  return response
42
 
43
  return wrapper
 
27
  # if media_type == 'image/png':
28
  # return StreamingResponse(BytesIO(_cache[key]), media_type=media_type)
29
  # else:
30
+ return StreamingResponse(BytesIO(_cache[key]), media_type="image/png")
31
 
32
  # Call the actual function if not in cache or expired
33
+ response, image_data = await func(*args, **kwargs)
34
  # Cache the content of the response's body.
35
+ _cache[key] = image_data
 
36
  _cache_time[key] = datetime.now()
37
 
 
 
38
  return response
39
 
40
  return wrapper
routers/intference/stable_diffusion.py CHANGED
@@ -63,5 +63,8 @@ async def generate(prompt: str):
63
 
64
  memory_stream = BytesIO()
65
  final_image.save(memory_stream, format="PNG")
 
 
66
  memory_stream.seek(0)
67
- return StreamingResponse(memory_stream, media_type="image/png")
 
 
63
 
64
  memory_stream = BytesIO()
65
  final_image.save(memory_stream, format="PNG")
66
+ image_data = memory_stream.getvalue() # get bytes of the image
67
+
68
  memory_stream.seek(0)
69
+ return StreamingResponse(memory_stream, media_type="image/png"), image_data
70
+