Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,33 +27,7 @@ def save_image_to_disk(image: Image.Image, filename: str) -> str:
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error saving image: {str(e)}"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
def reliable_image_generator(prompt: str) -> Image.Image:
|
| 32 |
-
"""A highly reliable tool to generate images.
|
| 33 |
-
Args:
|
| 34 |
-
prompt: The description of the image to generate.
|
| 35 |
-
"""
|
| 36 |
-
try:
|
| 37 |
-
# 将提示词编码为 URL 格式
|
| 38 |
-
encoded_prompt = urllib.parse.quote(prompt)
|
| 39 |
-
url = f"https://image.pollinations.ai/prompt/{encoded_prompt}"
|
| 40 |
-
|
| 41 |
-
# 关键修复:伪装成正常的 Windows Chrome 浏览器
|
| 42 |
-
headers = {
|
| 43 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
response = requests.get(url, headers=headers)
|
| 47 |
-
|
| 48 |
-
# 提前拦截错误:如果不是 200 成功,直接把服务器的骂人话打印出来
|
| 49 |
-
if response.status_code != 200:
|
| 50 |
-
raise Exception(f"API Error {response.status_code}: 返回了非图片内容")
|
| 51 |
-
|
| 52 |
-
# 将网络请求返回的图片数据转化为真正的 PIL 图片对象
|
| 53 |
-
image = Image.open(BytesIO(response.content))
|
| 54 |
-
return image
|
| 55 |
-
except Exception as e:
|
| 56 |
-
raise Exception(f"Image generation failed: {str(e)}")
|
| 57 |
|
| 58 |
@tool
|
| 59 |
def get_hacker_news_top_stories(limit: int = 5) -> str:
|
|
@@ -136,8 +110,8 @@ custom_role_conversions=None,
|
|
| 136 |
|
| 137 |
|
| 138 |
# Import tool from Hub
|
| 139 |
-
|
| 140 |
-
|
| 141 |
with open("prompts.yaml", 'r') as stream:
|
| 142 |
prompt_templates = yaml.safe_load(stream)
|
| 143 |
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error saving image: {str(e)}"
|
| 29 |
|
| 30 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@tool
|
| 33 |
def get_hacker_news_top_stories(limit: int = 5) -> str:
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
# Import tool from Hub
|
| 113 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 114 |
+
|
| 115 |
with open("prompts.yaml", 'r') as stream:
|
| 116 |
prompt_templates = yaml.safe_load(stream)
|
| 117 |
|