Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,19 @@ def reliable_image_generator(prompt: str) -> Image.Image:
|
|
| 36 |
try:
|
| 37 |
# 将提示词编码为 URL 格式
|
| 38 |
encoded_prompt = urllib.parse.quote(prompt)
|
| 39 |
-
# 调用免费免 Key 的画图接口
|
| 40 |
url = f"https://image.pollinations.ai/prompt/{encoded_prompt}"
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# 将网络请求返回的图片数据转化为真正的 PIL 图片对象
|
| 43 |
image = Image.open(BytesIO(response.content))
|
| 44 |
return image
|
|
|
|
| 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
|