soiz commited on
Commit
6437aa7
1 Parent(s): d11346d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,12 +1,21 @@
 
1
  import subprocess
2
  import sys
3
 
4
- # Check if Flask is installed
 
 
 
 
 
 
 
 
 
5
  try:
6
  import flask
7
  except ImportError:
8
  print("Flask is not installed. Installing Flask...")
9
- # Install Flask using subprocess
10
  result = subprocess.run([sys.executable, "-m", "pip", "install", "flask"], capture_output=True, text=True)
11
  if result.returncode != 0:
12
  print("Failed to install Flask.")
@@ -14,18 +23,8 @@ except ImportError:
14
  sys.exit(1)
15
  else:
16
  print("Flask installed successfully.")
17
- # Try importing Flask again
18
  import flask
19
 
20
- import gradio as gr
21
- from random import randint
22
- from all_models import models
23
- from flask import Flask, request, send_file
24
- from io import BytesIO
25
- from PIL import Image
26
-
27
- app = Flask(__name__)
28
-
29
  def load_model(model_name):
30
  try:
31
  # Load model within Gradio context
@@ -58,7 +57,7 @@ def gen_fn(model_str, prompt, negative_prompt=None):
58
  with gr.Blocks() as block:
59
  result = model(full_prompt)
60
 
61
- if isinstance(result, str): # Assuming result might be a file path
62
  if os.path.exists(result):
63
  return Image.open(result)
64
  else:
@@ -67,7 +66,7 @@ def gen_fn(model_str, prompt, negative_prompt=None):
67
  elif isinstance(result, Image.Image):
68
  return result
69
  else:
70
- print("Result is not an image:", type(result))
71
  return None
72
  except Exception as e:
73
  print("Error generating image:", e)
@@ -85,7 +84,7 @@ def home():
85
  if prompt:
86
  # Generate the image
87
  image = gen_fn(model, prompt, negative_prompt)
88
- if isinstance(image, Image.Image): # Ensure the result is a PIL image
89
  # Save image to BytesIO object
90
  img_io = BytesIO()
91
  image.save(img_io, format='PNG')
 
1
+ import os
2
  import subprocess
3
  import sys
4
 
5
+ import gradio as gr
6
+ from random import randint
7
+ from all_models import models
8
+ from flask import Flask, request, send_file
9
+ from io import BytesIO
10
+ from PIL import Image
11
+
12
+ app = Flask(__name__)
13
+
14
+ # Install Flask if not already installed
15
  try:
16
  import flask
17
  except ImportError:
18
  print("Flask is not installed. Installing Flask...")
 
19
  result = subprocess.run([sys.executable, "-m", "pip", "install", "flask"], capture_output=True, text=True)
20
  if result.returncode != 0:
21
  print("Failed to install Flask.")
 
23
  sys.exit(1)
24
  else:
25
  print("Flask installed successfully.")
 
26
  import flask
27
 
 
 
 
 
 
 
 
 
 
28
  def load_model(model_name):
29
  try:
30
  # Load model within Gradio context
 
57
  with gr.Blocks() as block:
58
  result = model(full_prompt)
59
 
60
+ if isinstance(result, str):
61
  if os.path.exists(result):
62
  return Image.open(result)
63
  else:
 
66
  elif isinstance(result, Image.Image):
67
  return result
68
  else:
69
+ print("Unexpected result type:", type(result))
70
  return None
71
  except Exception as e:
72
  print("Error generating image:", e)
 
84
  if prompt:
85
  # Generate the image
86
  image = gen_fn(model, prompt, negative_prompt)
87
+ if isinstance(image, Image.Image):
88
  # Save image to BytesIO object
89
  img_io = BytesIO()
90
  image.save(img_io, format='PNG')