-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Description
测试代码:
import argparse
import requests
import revTongYi.qianwen as qwen
import time
import json
def get_image_bytes(image_path):
if image_path.startswith('http://') or image_path.startswith('https://'):
response = requests.get(image_path)
response.raise_for_status() # 检查请求是否成功
return response.content
else:
with open(image_path, 'rb') as f:
return f.read()
def main():
parser = argparse.ArgumentParser(description='Chatbot 参数解析')
# 添加参数
parser.add_argument('question', type=str, help='问题内容')
parser.add_argument('-p', '--image', type=str, required=True, help='图片的 URL 或本地文件路径')
parser.add_argument('-c', '--cookie', type=str, default='cookies.json', help='包含 cookies 的文件路径')
parser.add_argument('-i', '--lastId', type=str, help='最后的 ID')
parser.add_argument('-s', '--sessionId', type=str, help='会话 ID')
parser.add_argument('-k','--keep-session', action='store_true', help='是否保留会话,否则自动删除')
# 解析参数
args = parser.parse_args()
# 从文件中加载 cookies
with open(args.cookie, 'r') as f:
cookies = json.load(f)
cookies = {it['name']:it['value'] for it in cookies}
# 获取图片的二进制数据
image_bytes = get_image_bytes(args.image)
# 创建 Chatbot 实例
chatbot = qwen.Chatbot(cookies=cookies)
lastId, sessionId=args.lastId,args.sessionId
# 流式获取响应
last_out = ""
for resp in chatbot.ask(prompt=args.question,parentId=lastId,sessionId=sessionId,image=image_bytes,stream=True):
if resp.contents:
resp_text = resp.contents[-1].content
print(resp_text.replace(last_out, ""), end="", flush=True)
time.sleep(0.1)
last_out = resp_text
sessionId = resp.sessionId
if not args.keep_session:
chatbot.delete_session(sessionId=sessionId)
if __name__ == "__main__":
main()
执行:
python t.py -p WechatIMG449.jpg -k "请识别图中的属性与属性值,用 json 格式输出,不要输出其它无关内容"
输出:
抱歉,系统超时,请稍后重试。

Metadata
Metadata
Assignees
Labels
No labels