timelessg

timelessg

V2EX 第 168196 号会员,加入于 2016-04-14 13:59:01 +08:00
今日活跃度排名 17106
根据 timelessg 的设置,主题列表被隐藏
二手交易 相关的信息,包括已关闭的交易,不会被隐藏
timelessg 最近回复了
# This is a sample Python script.
import base64

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.


import requests
import json
import mimetypes
import os

BASE_URL = "https://generativelanguage.googleapis.com"
GEMINI_API_KEY = ""
IMG_PATH_2 = ""
DISPLAY_NAME = "TEXT"

# 获取 MIME 类型和文件大小
MIME_TYPE, _ = mimetypes.guess_type(IMG_PATH_2)
NUM_BYTES = os.path.getsize(IMG_PATH_2)

# 发送初始 resumable 上传请求
data = {"file": {"display_name": DISPLAY_NAME}}
headers = {
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
"X-Goog-Upload-Header-Content-Length": str(NUM_BYTES),
"X-Goog-Upload-Header-Content-Type": MIME_TYPE,
"Content-Type": "application/json",
}

response = requests.post(
f"{BASE_URL}/upload/v1beta/files?key={GEMINI_API_KEY}",
headers=headers,
json=data
)

# 提取上传 URL
upload_url = response.headers.get("X-Goog-Upload-URL")
if not upload_url:
print("Failed to get upload URL.")
exit(1)

# 读取文件数据并上传
with open(IMG_PATH_2, "rb") as f:
file_data = f.read()

headers = {
"Content-Length": str(NUM_BYTES),
"X-Goog-Upload-Offset": "0",
"X-Goog-Upload-Command": "upload, finalize"
}

response = requests.post(upload_url, headers=headers, data=file_data)
file_info = response.json()
file_uri = file_info.get("file", {}).get("uri")

if not file_uri:
print("Failed to get file URI.")
exit(1)

print(f"file_uri={file_uri}")

# 生成内容请求
data = {
"contents": [{
"parts": [
{"text": "把全部人脸替换成猫头"},
{"file_data": {"mime_type": "image/jpeg", "file_uri": file_uri}}
]
}],
"generationConfig": {"response_modalities": ["Text", "Image"]}
}
headers = {"Content-Type": "application/json"}

response = requests.post(
f"{BASE_URL}/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?key={GEMINI_API_KEY}",
headers=headers,
json=data
)

response_json = response.json()
with open("response.json", "w", encoding="utf-8") as f:
json.dump(response_json, f, ensure_ascii=False, indent=4)

try:
for item in response_json: # 遍历列表
candidates = item.get("candidates", [])
for candidate in candidates:
content = candidate.get("content", {})
parts = content.get("parts", [])

for part in parts:
inline_data = part.get("inlineData")
if inline_data and "data" in inline_data:
base64_data = inline_data["data"]
mime_type = inline_data.get("mimeType", "image/png")

# 生成对应的文件扩展名
ext = "jpg" if "jpeg" in mime_type else "png"
output_file = f"output.{ext}"

# 解码 Base64 并保存为图片
image_data = base64.b64decode(base64_data)
with open(output_file, "wb") as img_file:
img_file.write(image_data)

print(f"图片已保存为 {output_file}")
break # 找到第一张就退出
else:
continue
break
else:
continue
break

except Exception as e:
print(f"处理 Base64 数据时出错: {e}")
cybex carbon
15 天前
回复了 darkce 创建的主题 程序员 字节的 Lynx 跨端框架开源了
缺的永远不是跨平台框架,缺的是生态,flutter ,rn 已经把生态做起来了,而像是 skip 之类据说可以用 swifui 写 Android 的框架没生态谁会用,除了 UI 两端还要写一堆 actual
29 天前
回复了 Chicagoake 创建的主题 Mac mini Mac mini 搭配外接硬盘求推荐
硬盘选无缓,盒子尽量选 jhl7440 主控,祥硕发热略大
Xcode 确实🌶︎🐔,比起花里胡哨的 Android studio ,🍎这几年真是不思进取
55 天前
回复了 shonnliberty 创建的主题 分享发现 kiwi 作者弃坑了
应该是作者向 edge 合并了 extension 的支持代码,相当于交给 edge 维护了
58 天前
回复了 BeijingBaby 创建的主题 音乐 多端同步听音乐是个很困难的事
spotify
挺好的,阮大神之前不就是这公司的
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   945 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 20:09 · PVG 04:09 · LAX 13:09 · JFK 16:09
Developed with CodeLauncher
♥ Do have faith in what you're doing.