Spaces:
Sleeping
Sleeping
Commit
·
7a3e379
1
Parent(s):
3b80791
Fix JSON Schema error and create minimal working app
Browse files
app.py
CHANGED
|
@@ -12,89 +12,83 @@ logging.basicConfig(level=logging.INFO,
|
|
| 12 |
stream=sys.stdout)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# 创建一个简单的示例图像
|
| 16 |
def create_dummy_image():
|
| 17 |
logger.info("Creating dummy image")
|
| 18 |
img = PILImage.new('RGB', (256, 256), color = (255, 100, 100))
|
| 19 |
return img
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
model_id = "runwayml/stable-diffusion-v1-5"
|
| 30 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
-
logger.info(f"Using device: {device}")
|
| 32 |
-
|
| 33 |
-
if torch.cuda.is_available():
|
| 34 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 35 |
-
pipe = pipe.to(device)
|
| 36 |
-
pipe.enable_attention_slicing()
|
| 37 |
-
else:
|
| 38 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
| 39 |
-
pipe = pipe.to(device)
|
| 40 |
-
|
| 41 |
-
logger.info("Model loaded successfully")
|
| 42 |
-
return pipe
|
| 43 |
-
except Exception as e:
|
| 44 |
-
logger.error(f"Failed to load model: {e}")
|
| 45 |
-
return None
|
| 46 |
-
|
| 47 |
-
# 全局变量
|
| 48 |
-
pipe = None
|
| 49 |
-
|
| 50 |
-
# 非常简单的功能实现
|
| 51 |
-
def generate(prompt):
|
| 52 |
-
global pipe
|
| 53 |
-
|
| 54 |
-
# 如果提示为空,使用默认提示
|
| 55 |
-
if not prompt or prompt.strip() == "":
|
| 56 |
-
prompt = "a beautiful landscape"
|
| 57 |
-
|
| 58 |
-
logger.info(f"Received prompt: {prompt}")
|
| 59 |
-
|
| 60 |
-
# 第一次调用时加载模型
|
| 61 |
-
if pipe is None:
|
| 62 |
-
pipe = get_model()
|
| 63 |
-
# 如果模型加载失败,返回示例图像
|
| 64 |
-
if pipe is None:
|
| 65 |
-
return create_dummy_image()
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
logger.info("Starting image generation")
|
| 69 |
-
# 使用最小的计算量生成图像
|
| 70 |
-
image = pipe(
|
| 71 |
-
prompt=prompt,
|
| 72 |
-
num_inference_steps=1,
|
| 73 |
-
guidance_scale=7.0,
|
| 74 |
-
height=256,
|
| 75 |
-
width=256
|
| 76 |
-
).images[0]
|
| 77 |
-
|
| 78 |
-
logger.info("Image generated successfully")
|
| 79 |
return image
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
fn=generate,
|
| 87 |
-
inputs="text",
|
| 88 |
-
outputs="image",
|
| 89 |
-
title="Simple Text-to-Image",
|
| 90 |
-
description="Type a prompt to generate an image.",
|
| 91 |
-
examples=["a cat", "mountain landscape"]
|
| 92 |
-
)
|
| 93 |
|
| 94 |
# 启动应用
|
| 95 |
if __name__ == "__main__":
|
| 96 |
try:
|
| 97 |
logger.info("Starting Gradio interface")
|
| 98 |
-
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
logger.error(f"Failed to launch: {e}")
|
|
|
|
| 12 |
stream=sys.stdout)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
# 修复 Gradio JSON Schema 错误
|
| 16 |
+
try:
|
| 17 |
+
import gradio_client.utils
|
| 18 |
+
|
| 19 |
+
# 保存原始函数
|
| 20 |
+
original_get_type = gradio_client.utils.get_type
|
| 21 |
+
|
| 22 |
+
# 创建新的 get_type 函数,处理布尔值
|
| 23 |
+
def patched_get_type(schema):
|
| 24 |
+
if schema is True or schema is False or schema is None:
|
| 25 |
+
return "any"
|
| 26 |
+
if not isinstance(schema, dict):
|
| 27 |
+
return "any"
|
| 28 |
+
return original_get_type(schema)
|
| 29 |
+
|
| 30 |
+
# 替换原始函数
|
| 31 |
+
gradio_client.utils.get_type = patched_get_type
|
| 32 |
+
logger.info("Successfully patched gradio_client.utils.get_type")
|
| 33 |
+
|
| 34 |
+
# 同样修补 _json_schema_to_python_type 函数
|
| 35 |
+
original_json_schema_to_python_type = gradio_client.utils._json_schema_to_python_type
|
| 36 |
+
|
| 37 |
+
def patched_json_schema_to_python_type(schema, defs=None):
|
| 38 |
+
if schema is True or schema is False:
|
| 39 |
+
return "bool"
|
| 40 |
+
if schema is None:
|
| 41 |
+
return "None"
|
| 42 |
+
if not isinstance(schema, dict):
|
| 43 |
+
return "any"
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
return original_json_schema_to_python_type(schema, defs)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logger.warning(f"Error in json_schema_to_python_type: {e}")
|
| 49 |
+
return "any"
|
| 50 |
+
|
| 51 |
+
gradio_client.utils._json_schema_to_python_type = patched_json_schema_to_python_type
|
| 52 |
+
logger.info("Successfully patched gradio_client.utils._json_schema_to_python_type")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
logger.error(f"Failed to patch Gradio utils: {e}")
|
| 55 |
+
|
| 56 |
# 创建一个简单的示例图像
|
| 57 |
def create_dummy_image():
|
| 58 |
logger.info("Creating dummy image")
|
| 59 |
img = PILImage.new('RGB', (256, 256), color = (255, 100, 100))
|
| 60 |
return img
|
| 61 |
|
| 62 |
+
# 使用极简方法
|
| 63 |
+
def simple_demo():
|
| 64 |
+
# 定义一个极简单的生成函数
|
| 65 |
+
def generate(text):
|
| 66 |
+
logger.info(f"Received text: {text}")
|
| 67 |
+
# 创建一个简单的图像作为响应
|
| 68 |
+
image = PILImage.new('RGB', (256, 256), color=(100, 200, 100))
|
| 69 |
+
logger.info("Created simple green image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return image
|
| 71 |
+
|
| 72 |
+
logger.info("Setting up simple demo without model loading")
|
| 73 |
+
# 创建最小界面
|
| 74 |
+
demo = gr.Interface(
|
| 75 |
+
fn=generate,
|
| 76 |
+
inputs=gr.Textbox(label="Prompt"),
|
| 77 |
+
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 78 |
+
title="Simple Text-to-Image",
|
| 79 |
+
examples=["a cat"],
|
| 80 |
+
cache_examples=False
|
| 81 |
+
)
|
| 82 |
+
return demo
|
| 83 |
|
| 84 |
+
# 只使用极简应用,避免加载任何模型
|
| 85 |
+
demo = simple_demo()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# 启动应用
|
| 88 |
if __name__ == "__main__":
|
| 89 |
try:
|
| 90 |
logger.info("Starting Gradio interface")
|
| 91 |
+
# 使用最小配置
|
| 92 |
+
demo.launch(server_name="0.0.0.0", show_api=False)
|
| 93 |
except Exception as e:
|
| 94 |
logger.error(f"Failed to launch: {e}")
|