File size: 1,908 Bytes
a99c8d6 5ff3da0 a482122 5ff3da0 a482122 3331037 a482122 5ff3da0 a482122 5ff3da0 a482122 5ff3da0 a482122 5ff3da0 a482122 5ff3da0 a482122 56de6f9 5ff3da0 a482122 56de6f9 a482122 5ff3da0 e400a0b 56de6f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import base64
import os
import gradio as gr
from openai import OpenAI
client = OpenAI(
api_key=os.getenv('HUNYUAN_API_KEY'),
base_url="https://api.hunyuan.cloud.tencent.com/v1"
)
def generate_caption(image_path, question):
# 将图片转换为Base64
with open(image_path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
# 构建消息结构
messages = [{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}]
# 调用混元视觉模型
response = client.chat.completions.create(
model="hunyuan-vision",
messages=messages,
stream=True,
extra_body={
"stream_moderation": True,
"enable_enhancement": False
}
)
# 流式处理响应
full_response = ""
for chunk in response:
token = chunk.choices[0].delta.content
if token:
full_response += token
yield full_response
# 创建Gradio界面
title="Hunyuan-Vision图生文Demo"
with gr.Blocks(title=title) as demo:
gr.Markdown(f"# 🖼️ {title}")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="filepath", label="上传图片")
question_input = gr.Textbox(label="输入问题", value="请描述图片内容")
submit_btn = gr.Button("生成描述")
output = gr.Textbox(label="描述结果", interactive=False)
submit_btn.click(
fn=generate_caption,
inputs=[image_input, question_input],
outputs=output
)
if __name__ == "__main__":
demo.queue(default_concurrency_limit=100)
demo.launch(max_threads=100) |