12月18日在火山引擎FORCE大会上,
字节跳动正式发布豆包视觉推理大模型,并宣布进入“厘时代”,价格降至0.003元/千token,比行业价格低85%,一块钱能够处理284张图片。
豆包大模型日均tokens使用量超过4万亿,发布7个月以来增长超过33倍。
看到这个消息, 大家会想到啥?
想起 之前智谱AI的GLM-4V-Flash免费, 我们可以利用大模型视觉推理 做很多事情。之前我写了一篇文章 【AI+编程】我用智谱AI写看图写话简单应用
关注我的读者都知道,我偶尔看看股票。那我们来分析下股票来试一下。
这里还是以GLM-4V-FLASH举例, 我准备找张图片分析下 。
大家先看效果
这里附上完整代码:
import streamlit as st
import os
import json
import base64
from dotenv import load_dotenv
import requests
from PIL import Image
from io import BytesIO
from tenacity import retry, stop_after_attempt, wait_exponential
API_KEY = "xxxxxxxxx"
if not API_KEY:
st.error("请设置 GLM_API_KEY 环境变量")
st.stop()
# API配置
API_URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
def encode_image_to_base64(image):
"""将PIL Image对象转换为base64字符串"""
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return img_str
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
def call_glm_api(model, messages):
"""调用GLM API"""
try:
payload = {
"model": model,
"messages": messages
}
response = requests.post(API_URL, headers=HEADERS, json=payload)
response.raise_for_status()
result = response.json()
if "choices" in result and len(result["choices"]) > 0:
return result["choices"][0]["message"]["content"]
else:
raise Exception("API返回的响应格式不正确")
except requests.exceptions.RequestException as e:
st.warning(f"API 调用失败,正在重试... ({str(e)})")
raise
def process_image(image, prompt):
"""处理图片并获取描述"""
try:
image_base64 = encode_image_to_base64(image)
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_base64}"
}
}
]
}
]
description = call_glm_api("glm-4v-flash", messages)
return description
except Exception as e:
st.error(f"处理图片时发生错误:{str(e)}")
return None
def analyze_future_trend(stock_info):
"""使用GLM-4-FLASH分析明日涨停走势"""
prompt = f"""
作为一位资深的A股分析师,请根据以下股票信息分析明日可能的涨停走势:
{stock_info}
请考虑以下因素:
1. 当前股价与行业平均水平的对比
2. 近期的成交量变化
3. 市场整体趋势
4. 该股票所属行业的recent news
5. 技术面指标(如MACD、KDJ等)
请给出详细的分析和预测,包括:
1. 涨停可能性(高、中、低)
2. 可能影响股价的关键因素
3. 建议的观察点或风险提示
请以结构化的方式呈现你的分析结果,使用Markdown格式输出。
"""
try:
messages = [
{"role": "user", "content": prompt}
]
analysis = call_glm_api("glm-4-flash", messages)
return analysis
except Exception as e:
st.error(f"分析未来趋势时发生错误:{str(e)}")
return None
# Streamlit UI
st.title("股票解析与趋势预测")
# 侧边栏配置
with st.sidebar:
st.header("📸 上传图片")
uploaded_file = st.file_uploader("选择一张图片...", type=["jpg", "jpeg", "png"])
st.header("💭 输入提示")
user_prompt = st.text_input(
"请输入描述要求",
"你是A股专家,请清晰列出股票名称、代码、价格、涨幅,通过表格形式输出"
)
# 主界面
if uploaded_file is not None:
# 显示上传的图片
image = Image.open(uploaded_file)
st.image(image, caption="上传的图片", use_column_width=True)
# 生成描述
if st.button("✨ 股市风云", type="primary"):
with st.spinner("正在分析图片..."):
description = process_image(image, user_prompt)
if description:
st.header("🎯 图像描述")
st.write(description)
# 分析明日涨停走势
st.header("🚀 明日涨停走势分析")
with st.spinner("正在分析未来趋势..."):
future_analysis = analyze_future_trend(description)
if future_analysis:
st.markdown(future_analysis)
else:
st.error("无法生成未来趋势分析")
else:
st.info("👆 请先上传一张图片")
# 使用说明
st.sidebar.markdown("---")
st.sidebar.header("📖 使用说明")
st.sidebar.write("""
1. 上传包含股票信息的图片
2. 点击"股市风云"按钮获取图片中的股票信息(使用GLM-4V-FLASH)
3. 系统会自动使用GLM-4-FLASH分析并预测明日涨停走势
4. 查看分析结果,包括股票信息和未来趋势预测
""")
# 版权信息
st.sidebar.markdown("---")
st.sidebar.markdown("Powered by GLM-4V & GLM-4")
AI大模型时代, 我们可以用AI 工具做不同场景的AI应用,干以前很多不能想的事情, 工作生活场景无处不在,只要你敢想。
这个视觉推理, 我能想到的场景:
1、 学习方面。帮家里小孩 辅导作文看图写话, 批改作业(作业帮? 如果有心, 我也可以干一个)
2、图片OCR识别, 车牌识别什么的
3、医学领域, 识别 分析医学图像(如 X 光片、CT 扫描)以识别疾病或异常情况。
4、根据用户的视觉偏好分析图像内容,提供个性化的产品或内容推荐。
你现在知道在抖音刷美女视频, 抖音会一直推给你这些感兴趣的内容了吧。 让你上瘾不可自拔。
…