当前位置: 首页 > news >正文

电脑编程入门自学新手如何学seo

电脑编程入门自学,新手如何学seo,做网站上的在线支付怎么做,用身份证备案网站1. 前言 最近在测试VLM模型,发现官方的网页demo,代码中视频与图片分辨率可能由于高并发设置的很小,导致达不到预期效果,于是自己研究了一下,搞了一个简单的前端部署,自己在服务器部署了下UI界面&#xff0…

1. 前言

最近在测试VLM模型,发现官方的网页demo,代码中视频与图片分辨率可能由于高并发设置的很小,导致达不到预期效果,于是自己研究了一下,搞了一个简单的前端部署,自己在服务器部署了下UI界面,方便在本地笔记本进行测试。

2.代码

import streamlit as st
from PIL import Image
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch
import os# 加载模型和处理器 (只加载一次)
@st.cache_resource  # 这个装饰器会缓存模型和处理器
def load_model():model = Qwen2VLForConditionalGeneration.from_pretrained("../qwen2_vl/model_7b/", torch_dtype=torch.float16, device_map="auto")processor = AutoProcessor.from_pretrained("../qwen2_vl/model_7b/")return model, processor# 加载模型和处理器
model, processor = load_model()def load_image(image_file):img = Image.open(image_file)return img# Function to load and resize image to fixed height
def resize_image_to_height(image, height):# Resize image keeping the aspect ratiowidth = int(image.width * height / image.height)return image.resize((width, height))# 处理输入
def process_input(messages):# Preparation for inferencetext = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)image_inputs, video_inputs = process_vision_info(messages)inputs = processor(text=[text],images=image_inputs,videos=video_inputs,padding=True,return_tensors="pt",)inputs = inputs.to("cuda")# Inferencegenerated_ids = model.generate(**inputs, max_new_tokens=128)generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)# Clear all intermediate variables and free GPU memorydel text, image_inputs, video_inputs, inputs, generated_ids, generated_ids_trimmedtorch.cuda.empty_cache()return output_text# Streamlit UI
st.title("VLM 视觉内容理解")# 选择文件上传
uploaded_file = st.file_uploader("上传图片或视频", type=["jpg", "jpeg", "png", "mp4"])# 判断文件是否上传
if uploaded_file is not None:# 保存文件到本地upload_dir = "uploads"  # 上传文件保存目录if not os.path.exists(upload_dir):os.makedirs(upload_dir)file_path = os.path.join(upload_dir, uploaded_file.name)# 保存文件with open(file_path, "wb") as f:f.write(uploaded_file.getbuffer())# 判断文件类型if uploaded_file.type.startswith("image"):# 加载并显示图片img = load_image(file_path)# 设置固定高度fixed_height = 300  # 设置固定高度为300px# 调整图片的大小,使高度固定,宽度按比例调整img_resized = resize_image_to_height(img, fixed_height)st.image(img_resized, use_container_width=False)# 输入台词部分st.subheader("输入一句提示词")user_input = st.text_input("请输入提示词,并回车确认:")messages = [{"role": "user","content": [{"type": "image","image": file_path,  # 使用本地保存的文件路径"max_pixels": 1024 * 960},{"type": "text", "text": user_input},],}]elif uploaded_file.type.startswith("video"):# 设置视频固定高度fixed_height = 300  # 设置固定高度为300px# 显示视频st.video(file_path)st.subheader("输入一句提示词")user_input = st.text_input("请输入提示词,并回车确认:")# 通过 Markdown 来调整视频的显示样式st.markdown(f"""<style>video {{height: {fixed_height}px;width: auto;}}</style>""",unsafe_allow_html=True)messages = [{"role": "user","content": [{"type": "video","video": file_path,  # 使用本地保存的文件路径"max_pixels": 960 * 480,"fps": 1.0,},{"type": "text", "text": user_input},],}]# 调用模型进行推理result = process_input(messages)if result:# 使用 st.markdown 和 CSS 来自动换行st.markdown("### 模型推理结果:")# 将输出格式化为代码块样式,并通过 CSS 实现自动换行st.markdown(f'<pre style="white-space: pre-wrap; word-wrap: break-word;">{result[0]}</pre>',unsafe_allow_html=True)else:st.markdown("### 模型推理结果:无结果。")# 推理完成后删除本地文件try:os.remove(file_path)except Exception as e:pass

结论

主要是利用streamlit,进行UI的搭建,涉及本地文件的上传与下载到服务器中,推理完删除。
在这里插入图片描述

http://www.hkea.cn/news/358008/

相关文章:

  • 新建网站推广给企业百度问一问在线咨询客服
  • 曹鹏wordpress建站seo视频广东疫情防控措施
  • 网站开发的岗位排名优化工具
  • 岳阳做网站怎么做推广让别人主动加我
  • 不断改进网站建设公司百度官网优化
  • 万户网站宁波网站制作优化服务
  • 潍坊快速网站排名网站是怎么做出来的
  • 聚美优品的pc网站建设注册网址
  • 陕西省住房与城乡建设厅网站免费b站推广软件
  • 淮南市住房与城乡建设部网站网店买卖有哪些平台
  • 网页qq表情佛山百度快速排名优化
  • 网站建设方案论文1500社会新闻最新消息
  • 网站组建 需求分析市场监督管理局职责
  • 云课堂哪个网站做的好厦门关键词优化seo
  • 中企动力沈阳分公司seo免费诊断电话
  • 网站vps被黑湖人最新排名最新排名
  • 如何夸奖客户网站做的好seo课程心得体会
  • 有哪些做电子商务的网站时空seo助手
  • 临沂百度网站电脑培训机构哪个好
  • 无锡专业做网站的公司怎样把自己的产品放到网上销售
  • 大学网站建设管理办法推广技巧
  • 长春做网站公司seo关键词排名优化软件怎么选
  • 网站开发未按合同约定工期完工seo关键词排名怎么提升
  • 创可贴app海报制作网站百度seo优化方法
  • 龙岗品牌网站建设2024年新闻摘抄
  • 南阳住房和城乡建设厅网站招聘网站排名
  • 如何做网站活动封面建站的公司
  • 温州网站建设培训营销推广方案包括哪些内容
  • 厦门 建网站商业软文案例
  • wordpress读者墙站长之家seo工具包