RRayna commited on
Commit
3af1159
·
verified ·
1 Parent(s): 4a53c26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -52,15 +52,25 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
52
 
53
  @tool
54
  def generate_image(prompt: str) -> str:
55
- """Generates an image based on a text prompt using a Hugging Face model.
56
- Returns the path or object of the generated image.
 
57
 
58
  Args:
59
  prompt: A descriptive text prompt for the image (e.g., 'A cat on the beach').
60
  """
61
  try:
62
- # 调用远程工具
63
- return image_generation_tool(prompt)
 
 
 
 
 
 
 
 
 
64
  except Exception as e:
65
  return f"Error generating image: {str(e)}"
66
 
 
52
 
53
  @tool
54
  def generate_image(prompt: str) -> str:
55
+ """
56
+ Generates an image using Stable Diffusion XL based on a text prompt.
57
+ Returns the path to the generated image file.
58
 
59
  Args:
60
  prompt: A descriptive text prompt for the image (e.g., 'A cat on the beach').
61
  """
62
  try:
63
+ # 我们显式指定一个免费的模型 ID:stable-diffusion-xl-base-1.0
64
+ client = InferenceClient("stabilityai/stable-diffusion-xl-base-1.0")
65
+
66
+ # 生成图片
67
+ image = client.text_to_image(prompt)
68
+
69
+ # 将图片保存到本地文件
70
+ filename = "generated_image.png"
71
+ image.save(filename)
72
+
73
+ return f"Image generated successfully and saved to {filename}"
74
  except Exception as e:
75
  return f"Error generating image: {str(e)}"
76