Geek7 commited on
Commit
21441f0
1 Parent(s): fb77553

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +10 -2
myapp.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  from threading import RLock
7
  from huggingface_hub import InferenceClient
8
  from all_models import models # Importing models from all_models
 
9
 
10
  myapp = Flask(__name__)
11
  CORS(myapp) # Enable CORS for all routes
@@ -40,9 +41,16 @@ async def infer(client, prompt, seed=1, timeout=inference_timeout, model="prompt
40
 
41
  if task.done() and result is not None:
42
  with lock:
 
 
 
 
 
 
43
  temp_image = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
44
  with open(temp_image.name, "wb") as f:
45
- f.write(result) # Save the result image as a temporary file
 
46
  return temp_image.name # Return the path to the saved image
47
  return None
48
 
@@ -79,4 +87,4 @@ def generate_api():
79
 
80
  # Add this block to make sure your app runs when called
81
  if __name__ == "__main__":
82
- myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testin
 
6
  from threading import RLock
7
  from huggingface_hub import InferenceClient
8
  from all_models import models # Importing models from all_models
9
+ from io import BytesIO # For converting image to bytes
10
 
11
  myapp = Flask(__name__)
12
  CORS(myapp) # Enable CORS for all routes
 
41
 
42
  if task.done() and result is not None:
43
  with lock:
44
+ # Convert image result to bytes
45
+ image_bytes = BytesIO()
46
+ result.save(image_bytes, format='PNG') # Save the image to a BytesIO object
47
+ image_bytes.seek(0) # Go to the start of the byte stream
48
+
49
+ # Save the result image as a temporary file
50
  temp_image = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
51
  with open(temp_image.name, "wb") as f:
52
+ f.write(image_bytes.read()) # Write the bytes to the temp file
53
+
54
  return temp_image.name # Return the path to the saved image
55
  return None
56
 
 
87
 
88
  # Add this block to make sure your app runs when called
89
  if __name__ == "__main__":
90
+ myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing