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

从零开始-与大语言模型对话学技术-gradio篇(4)

前言

本文介绍「星火杯」认知大模型场景创新赛中的落选项目- AI命理分析系统,属于个人娱乐练手。总结提炼了往期文章精华并发掘出新的知识。
包括本地部署版本和Web在线版本,两种打包方式基于
半自动化使用.bat手动打包迁移python项目

如何把 Gradio 应用上传到 Hugging Face
往期回顾:
从零开始-与大语言模型对话学技术-gradio篇(1)

从零开始-与大语言模型对话学技术-gradio篇(2)

从零开始-与大语言模型对话学技术-gradio篇(3)

项目简介

本项目是一个集成化的AI命理分析系统,实现了星座解析、塔罗解牌、八字合婚等多种智能化命理服务。系统通过Python和Gradio实现了交互式的网页界面,用户只需要输入必要个人信息,即可获得智能的命理运势分析。
Github链接

使用说明

最新Web在线版本使用

Web公开版本已在Hugging Face开源,点击链接即可在线使用,不同于本地版本,你必须配置自己的星火APl,全平台可用
hugging face连接
在这里插入图片描述

本地部署版本

  1. 打开AI命理分析系统V4.0进入虚拟python环境,自动检测依赖,安装环境并运行程序
    显示如下提示表示编译运行成功,打开这个连接即可进入系统

在这里插入图片描述

  1. 你也可以打开GUI.py在末尾将demo.launch()修改为 demo.launch(share=True)
    那么你会额外获取一个随机的公开链接,你可以在任何设备上输入网址访问这个AI命理分析系统
    Running on local URL: http://127.0.0.1:7860
    Running on public URL: https://436fda53710f62fbbc.gradio.live
    This share link expires in 72 hours. For free permanent hosting and GPU upgrades,
    run gradio deploy from Terminal to deploy to Spaces (https://huggingface.co/spaces)

功能列表

  • 接入星火认知大模型:你可以选择使用我的API或者自行配置 配置好API才能使用后续功能!
    在这里插入图片描述

  • AI星座解读:输入个人信息,获得当前月星座运势解析
    在这里插入图片描述

  • AI塔罗牌解读:用户提问,系统抽取塔罗牌进行占卜
    在这里插入图片描述

  • AI八字合婚分析:输入双方八字,智能匹配分析婚姻
    在这里插入图片描述

  • AI兔年运势预测:基于八字分析未来财运、事业等在这里插入图片描述

  • AI公司命理解析:根据个人信息以及公司名称和行业,分析公司运势。在这里插入图片描述

  • AI姓名配对:评估两人姓名匹配程度
    在这里插入图片描述

  • AI月老姻缘:分析最佳配对对象
    在这里插入图片描述

  • AI八字精批:输入八字信息获得对应运势的专业精批。
    在这里插入图片描述

  • AI姓名分析:分析输入的姓名对个人命运的影响。
    在这里插入图片描述

  • AI紫薇斗数解析:根据八字信息计算紫薇数值并进行解读
    在这里插入图片描述

完整代码

app.py

# -*- coding = utf-8 -*-
"""
# @Time : 2023/7/31 19:33
# @Author : CSDN:FriKlogff
# @File : app.py
# @Software: PyCharm
# @Function: 请输入项目功能
"""
import os
os.system("""python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip setuptools
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket-client
pip install -i https://mirrors.aliyun.com/pypi/simple/ gradio
pip install -i https://mirrors.aliyun.com/pypi/simple/ sxtwl
""")
from PublicFunctions import *
import gradio as gr# 定义星座选项
signs = ["白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座","天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"]
cards_num = [1, 2, 3, 4, 5]
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
days = [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]
hours = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
# 使用 Gradio 的模块化组件,构建包含五个选项卡的界面
with gr.Blocks() as demo:with gr.Tab("星火api配置"):xh_input = [gr.components.Textbox(label="appid"),gr.components.Textbox(label="api_secret"),gr.components.Textbox(label="api_key"),gr.components.Textbox(label="gpt_url")]xh_output = gr.components.Textbox(label="点击提交返回配置情况,请自行配置星火大模型API再使用后续功能")xh_button = gr.components.Button("提交")xh_button.click(xh_api, inputs=xh_input, outputs=xh_output)with gr.Tab("AI星座解读"):horoscope_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),gr.components.Dropdown(signs, label="选择您的星座")]horoscope_output = gr.components.Textbox(label="星座解读(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")horoscope_button = gr.components.Button("提交")horoscope_button.click(horoscope_reading, inputs=horoscope_input, outputs=horoscope_output)with gr.Tab("AI塔罗牌解读"):tarot_input = [gr.components.Textbox(label="你想问的问题"),gr.components.Dropdown(cards_num, label="你想抽几张牌"),]tarot_output = gr.components.Textbox(label="塔罗牌解析(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")upload_button = gr.components.Button("抽取")upload_button.click(tarot_reading, inputs=tarot_input, outputs=tarot_output)with gr.Tab("AI八字合婚分析"):marriage_input = [gr.components.Textbox(label="新郎姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),gr.components.Textbox(label="新娘姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),]marriage_analysis_output = gr.components.Textbox(label="婚姻分析(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")analyze_button = gr.components.Button("马上测算")analyze_button.click(marriage_bazi_analysis,inputs=marriage_input,outputs=marriage_analysis_output)with gr.Tab("AI兔年运程预测"):birth_year_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),]prediction_output = gr.components.Textbox(label="运程预测(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")predict_button = gr.components.Button("预测运势")predict_button.click(rabbit_year_prediction,inputs=birth_year_input,outputs=prediction_output)with gr.Tab("AI公司命理解析"):company_name_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),gr.components.Textbox(label="公司名称"),gr.components.Textbox(label="所属行业")]name_analysis_output = gr.components.Textbox(label="命理分析(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")analyze_button = gr.components.Button("分析")analyze_button.click(company_name_analysis,inputs=company_name_input,outputs=name_analysis_output)with gr.Tab("AI姓名配对"):name1_input = [gr.components.Textbox(label="姓名1"),gr.components.Textbox(label="姓名2"),]matching_output = gr.components.Textbox(label="配对结果(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")match_button = gr.components.Button("分析配对")match_button.click(name_compatibility,inputs=name1_input,outputs=matching_output)with gr.Tab("AI月老姻缘"):yue_lau_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),]affinity_output = gr.components.Textbox(label="姻缘分析(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")analyze_button = gr.components.Button("分析姻缘")analyze_button.click(yue_lau_affinity,inputs=yue_lau_input,outputs=affinity_output)with gr.Tab("AI八字精批"):bazi_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),]analysis_output = gr.components.Textbox(label="精批结果(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")batch_button = gr.components.Button("八字精批")batch_button.click(bazi_analysis,inputs=bazi_input,outputs=analysis_output)with gr.Tab("AI姓名分析"):name_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名")]name_output = gr.components.Textbox(label="命理分析(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")analyze_button = gr.components.Button("分析姓名")analyze_button.click(name_analysis,inputs=name_input,outputs=name_output)with gr.Tab("AI紫薇斗数解析"):zhiwei_input = [gr.components.Radio(choices=["男", "女"], label="性别"),gr.components.Textbox(label="姓名"),gr.components.Number(label="出生年份"),gr.components.Dropdown(months, label="出生月份"),gr.components.Dropdown(days, label="出生日"),gr.components.Dropdown(hours, label="出生时辰"),]zhiwei_output = gr.components.Textbox(label="紫薇解读(由于我们的解析是由AI生成的,结果仅供娱乐,如果不成功请多试几次)")zhiwei_button = gr.components.Button("解读运势")zhiwei_button.click(zhiwei_analysis,inputs=zhiwei_input,outputs=zhiwei_output)
demo.launch()
# demo.launch(share=True)

PublicFunctions.py

# -*- coding = utf-8 -*-
"""
# @Time : 2023/7/31 19:35
# @Author : CSDN:FriKlogff
# @File : PublicFunctions.py
# @Software: PyCharm
# @Function: 请输入项目功能
"""import os
os.system("""python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip setuptools
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket-client
pip install -i https://mirrors.aliyun.com/pypi/simple/ gradio
pip install -i https://mirrors.aliyun.com/pypi/simple/ sxtwl
""")
import sxtwl
from XhApi import *
import XhApi# print(XhApi.response_content)
Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
appid = ''
api_secret = ''
api_key = ''
gpt_url = ''def generate_bazi(year, month, day, hour):year = int(year)month = int(month)day = int(day)hour = int(hour)date = sxtwl.fromSolar(year, month, day)# 获取年柱yTG = date.getYearGZ()ganzhi_year = Gan[yTG.tg] + Zhi[yTG.dz]# 获取月柱mTG = date.getMonthGZ()ganzhi_month = Gan[mTG.tg] + Zhi[mTG.dz]# 获取日柱dTG = date.getDayGZ()ganzhi_day = Gan[dTG.tg] + Zhi[dTG.dz]# 获取时柱dayGan = dTG.tghTG = sxtwl.getShiGz(dayGan, hour)ganzhi_hour = Gan[hTG.tg] + Zhi[hTG.dz]return f"{ganzhi_year}{ganzhi_month}{ganzhi_day}{ganzhi_hour}时"def xh_api( user_appid, user_api_secret, user_api_key, user_gpt_url):global appid, api_secret, api_key, gpt_urlif user_appid == '' or user_api_secret == '' or user_api_key == '' or user_gpt_url == '':return "any api cannot be empty!"appid = str(user_appid)api_secret = str(user_api_secret)api_key = str(user_api_key)gpt_url = str(user_gpt_url)# print(type(appid), type(api_secret), type(api_key), type(gpt_url))# print(appid, api_secret, api_key, gpt_url)return "appid = "+appid+"\napi_secret = "+api_secret+"\napi_key = "+api_key+"\ngpt_url = "+gpt_urldef horoscope_reading(sex, name, birth_year, birth_month, birth_day, birth_hour, star):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = str(int(birth_year))if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '' or birth_hour == '' or star == '':return "Name or birth_year or birth_month or birth_day or star cannot be empty!"if birth_year == "0":return "0 is not a suitable value of birth_year!"template = "假设你是一位专业的星座运势分析师,根据客户提供的出生日期和时间,你需要进行以下几方面的详细分析工作:\n" \"1. 分析用户的星座及性格特点\n" \"2. 根据月球周期判断事业和学业趋势\n" \"3. 提供维持提升感情的建议\n" \"4. 预测财务收入和投资趋势\n" \"5. 提出健康保健建议\n" \"作为专业分析师,你需要用通俗语言解释理论,并提供专业建议\n"template += "客户信息:\n"template += "性别:{sex}\n"template += "姓名:{name}\n"template += "星座:{star}\n"template += "出生日期:{birth_year}-{birth_month}-{birth_day}-{birth_hour}"question = template.format(sex=sex, name=name, birth_year=birth_year, birth_month=birth_month, birth_day=birth_day,birth_hour=birth_hour,star=star)# print(appid, api_secret, api_key, gpt_url)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)def tarot_reading(question, num_cards):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlif question == '' or num_cards is None or num_cards == 0:return "question ornum_cards cannot be empty!"template = "假设你是一位专业的塔罗牌占卜师。用户提出的问题是:{question}。"template += "根据用户的问题,你需要为TA抽取{num_cards}张塔罗牌,"template += "解读每张塔罗牌的含义,"template += "综合牌面分析用户所问的问题,"template += "并根据占卜结果给予专业的建议。"template += "具体来说,你需要:\n"template += "1. 为用户抽取指定数量的塔罗牌\n"template += "2. 逐一解析每张塔罗牌的符号和含义\n"template += "3. 综合各牌面意义,对用户提问进行占卜分析\n"template += "4. 根据占卜结果,给出专业建议或预言"question = template.format(question=question, num_cards=int(num_cards))return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)def marriage_bazi_analysis(name_husband, birth_year_husband, birth_month_husband, birth_day_husband, birth_hour_husband,name_wife, birth_year_wife, birth_month_wife, birth_day_wife, birth_hour_wife):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year_husband = int(birth_year_husband)birth_year_wife = int(birth_year_wife)if name_husband == '' or birth_year_husband == '' or birth_month_husband == '' or birth_day_husband == '' or birth_hour_husband == '' \or name_wife == '' or birth_year_wife == '' or birth_month_wife == '' or birth_day_wife == '' or birth_hour_wife == '':return "Name or birth_year or birth_month or birth_day  cannot be empty!"if birth_year_husband == 0 or birth_year_wife == 0:return "0 is not a suitable value of birth_year!"bazi_husband = generate_bazi(birth_year_husband, birth_month_husband, birth_day_husband, birth_hour_husband)bazi_wife = generate_bazi(birth_year_wife, birth_month_wife, birth_day_wife, birth_hour_wife)# print(bazi_wife, bazi_husband)template = "假设你是一位专业的八字合婚分析师,你正在为一对新人进行八字合婚分析。" \"分析基于八字五行、十神、四柱的原理判断两人姻缘。重点看天格、年格五行相生相克。" \"以下是他们的基本信息\n"template += "新郎信息:\n"template += "姓名:{name_husband}\n"template += "出生日期:{birth_year_husband}-{birth_month_husband}-{birth_day_husband}\n"  # 根据用户的选择生成问题template += "八字:{bazi_husband}\n"template += "新娘信息:\n"template += "姓名:{name_wife}\n"template += "出生日期:{birth_year_wife}-{birth_month_wife}-{birth_day_wife}\n"  # 根据用户的选择生成问题template += "八字:{bazi_wife}\n"template += "作为资深的合婚分析师,你需要:\n"template += "1. 分析两人八字五行相生相克关系\n"template += "2. 比较两人十神是否匹配\n"template += "3. 检查四柱运势是否协调\n"template += "4. 给出姻缘匹配度及建议\n"question = template.format(name_husband=name_husband, name_wife=name_wife, bazi_husband=bazi_husband,bazi_wife=bazi_wife, birth_year_husband=birth_year_husband,birth_month_husband=birth_month_husband, birth_day_husband=birth_day_husband,birth_year_wife=birth_year_wife, birth_month_wife=birth_month_wife,birth_day_wife=birth_day_wife)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 兔年运程
def rabbit_year_prediction(sex, name, birth_year, birth_month, birth_day, birth_hour):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = int(birth_year)bazi = generate_bazi(int(birth_year), int(birth_month), int(birth_day), int(birth_hour))if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '' or birth_hour == '':return "Name or birth_year or birth_month or birth_day cannot be empty!"if birth_year == 0:return "0 is not a suitable value of birth_year!"template = "假设你是一位专业的命理师,仔细分析客户信息,结合通胜原理,考量客户的五行八字、天干合化等,对在兔年客户的事业、财富、姻缘等命局进行预测,并给出建议。"template += "\n客户信息:\n"template += "性别:{sex}\n"template += "姓名:{name}\n"template += "出生日期:{birth_year}-{birth_month}-{birth_day}-{birth_hour}\n"template += "八字:{bazi}\n"template += "具体来说,你需要:\n"template += "1. 检查客户八字和五行属性\n"template += "2. 分析天干合化对命局的影响\n"template += "3. 考量通胜原理对运势的作用\n"template += "4. 对事业、财富、姻缘等命局给出预测\n"template += "5. 提供专业建议"question = template.format(sex=sex, name=name, birth_year=birth_year, birth_month=birth_month, birth_day=birth_day,birth_hour=birth_hour, bazi=bazi)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 公司测名
def company_name_analysis(sex, name, birth_year, birth_month, birth_day, birth_hour, company_name, industry):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = int(birth_year)if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '' or birth_hour == '' or company_name == '' or industry == '':return "Name or birth_year or birth_month or birth_day  or company_name  or industry cannot be empty!"if birth_year == 0:return "0 is not a suitable value of birth_year!"template = "假设你是一位公司命理专家,根据立命八字学说,姓名、公司名与行业之间存在相生相克的关系," \"需要综合考量五行、八卦、吉凶等理论,分析它们之间的互动对企业发展的影响," \"发掘其中蕴含的福禄文星,提出建议以改善财运。"template += "\n客户信息:\n"template += "性别:{sex}\n"template += "姓名:{name}\n"template += "出生日期:{birth_year}-{birth_month}-{birth_day}-{birth_hour}\n"template += "公司名:{company_name}\n"template += "行业:{industry}\n"template += "具体来说,你需要:\n"template += "1. 分析客户姓名五行属性\n"template += "2. 考量公司名五行与行业五行关系\n"template += "3. 判断相生相克对企业运势的影响\n"template += "4. 发掘姓名、公司名蕴含的福星\n"template += "5. 提出改善企业财运的专业建议"question = template.format(sex=sex, name=name, birth_year=birth_year, birth_month=birth_month, birth_day=birth_day,birth_hour=birth_hour,company_name=company_name, industry=industry)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 姓名配对
def name_compatibility(name1, name2):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlif name1 == '' or name2 == '':return "name1 or name2 cannot be empty!"template = "假设你是一位姓名学专家。用户提供了两人的姓名:{name1}和{name2}。"template += "作为专家,你需要分析他们两人姓名的五行、笔画等特征,"template += "判断姓名间的五行关系是否协调、笔画关系是否匹配,"template += "从姓名学角度出发,分析这两人的姓名是否配对。"template += "具体来说,你需要:\n"template += "1. 分析{name1}的五行属性和笔画数\n"template += "2. 分析{name2}的五行属性和笔画数\n"template += "3. 判断两人姓名的五行相生相克关系\n"template += "4. 判断两人姓名笔画数差是否合适\n"template += "5. 从姓名学角度给出配对建议\n"template += "最后要给出专业建议,说明这对姓名的搭配优劣势。"question = template.format(name1=name1, name2=name2)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 月老姻缘
def yue_lau_affinity(sex, name, birth_year, birth_month, birth_day, birth_hour):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = int(birth_year)bazi = generate_bazi(int(birth_year), int(birth_month), int(birth_day), int(birth_hour))if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '' or birth_hour == '':return "Name or birth_year or birth_month or birth_day cannot be empty!"if birth_year == 0:return "0 is not a suitable value of birth_year!"template = "假设你是一位月老姻缘专家。有客户需要你的帮助,其信息如下:\n"template += "性别:{sex}\n"template += "姓名:{name}\n"template += "出生日期:{birth_year}-{birth_month}-{birth_day}-{birth_hour}\n"template += "八字:{bazi}\n"template += "作为月老专家,你需要基于客户的姓名、性别、出生日期等信息,"template += "来分析其感情运势、最佳配对对象,"template += "给出专业的建议,帮助客户找到适合的另一半。"template += "具体来说,你需要:\n"template += "1. 分析客户八字姻缘格局\n"template += "2. 考量姓名数字对婚姻的影响\n"template += "3. 判断最佳配对对象的特征\n"template += "4. 提出改善感情运势的建议"question = template.format(sex=sex, name=name, birth_year=birth_year, birth_month=birth_month, birth_day=birth_day,birth_hour=birth_hour, bazi=bazi)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 八字精批
def bazi_analysis(sex, name, birth_year, birth_month, birth_day, birth_hour):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = int(birth_year)if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '':return "Name or birth_year or birth_month or birth_day cannot be empty!"if birth_year == 0:return "0 is not a suitable value of birth_year!"bazi = generate_bazi(int(birth_year), int(birth_month), int(birth_day), int(birth_hour))template = "假设你是一位资深的八字命理师。有客户需要你对其八字进行专业精批,其信息如下:\n"template += "性别:{sex}\n"template += "姓名:{name}\n"template += "八字:{bazi}\n"template += "作为八字命理专家,你需要根据客户的八字,"template += "分析事业财运、健康等方面的运势趋势,"template += "具体来说,你需要:\n"template += "1. 检查天干五行对事业财运的影响\n"template += "2. 分析八字各宫协调性和局部格局\n"template += "3. 指出八字优势和劣势\n"template += "4. 提出合理的改善建议"question = template.format(sex=sex, name=name, bazi=bazi)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)def zhiwei_analysis(sex, name, birth_year, birth_month, birth_day, birth_hour):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlbirth_year = int(birth_year)if sex is None:return "sex cannot be empty!"if name == '' or birth_year == '' or birth_month == '' or birth_day == '':return "Name or birth_year or birth_month or birth_day cannot be empty!"if birth_year == 0:return "0 is not a suitable value of birth_year!"# print(sex, name, birth_year, birth_month, birth_day)bazi = generate_bazi(int(birth_year), int(birth_month), int(birth_day), int(birth_hour))template = "假设你是一位紫薇斗数专家,接收到客户的出生八字后,你会依次完成以下步骤:\n" \"1. 计算该八字的紫微星位置,代表其总体运势\n" \"2. 分析年柱运程,判断事业财运\n" \"3. 分析月柱运程,判断感情运\n" \"4. 分析日柱运程,判断健康运\n" \"5. 综合四柱运势对该客户的综合运势做出详细的预言分析\n" \"客户信息:\n" \"性别:{sex}\n" \"姓名:{name}\n" \"八字为:{bazi}"question = template.format(sex=sex, name=name, bazi=bazi)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)# 姓名分析def name_analysis(sex, name):XhApi.response_content = ''global appid, api_secret, api_key, gpt_urlif sex is None:return "sex cannot be empty!"if name == '':return "Name cannot be empty!"template = "假设你是一位姓名学专家,请根据客户的姓名,分析其一生运势。\n"template += "要点包括:\n"template += "- 姓名的谐音是否吉利\n"template += "- 姓名笔画多寡对品性的影响\n"template += "- 单名双名优劣\n"template += "客户的姓名为 {name},性别为{sex},分析对其事业、婚姻、健康等方面的影响,并提出建议。"template += "具体来说,你需要\n"template += "1. 分析客户姓名谐音\n"template += "2. 判断姓名笔画数命理含义\n"template += "3. 讨论单名双名特点\n"template += "4. 分析姓名对运势各方面的影响\n"template += "5. 提出改善命运的专业建议"question = template.format(name=name, sex=sex)return main(appid=appid,api_secret=api_secret,api_key=api_key,gpt_url=gpt_url,question=question)

XhApi.py

# -*- coding = utf-8 -*-
"""
# @Time : 2023/7/20 12:37
# @Author : CSDN:FriKlogff
# @File : XhApi.py
# @Software: PyCharm
# @Function: 星火大模型API
"""
import os
os.system("""python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip setuptools
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket
pip install -i https://mirrors.aliyun.com/pypi/simple/ websocket-client
pip install -i https://mirrors.aliyun.com/pypi/simple/ gradio
pip install -i https://mirrors.aliyun.com/pypi/simple/ sxtwl
""")
import _thread as thread  # 导入线程模块
import base64  # 导入base64编码模块
import datetime  # 导入datetime模块
import hashlib  # 导入hashlib模块
import hmac  # 导入hmac模块
import json  # 导入json模块
from urllib.parse import urlparse  # 从urllib.parse导入urlparse用于url解析
import ssl  # 导入ssl模块
from datetime import datetime  # 从datetime导入datetime类
from time import mktime  # 从time导入mktime用于生成时间戳
from urllib.parse import urlencode  # 从urllib.parse导入urlencode用于编码请求参数
from wsgiref.handlers import format_date_time  # 从wsgiref.handlers导入format_date_time用于格式化时间import websocket  # 导入websocket模块response_content = ""# 请求参数类
class Ws_Param:# 初始化def __init__(self, APPID, APIKey, APISecret, gpt_url):self.APPID = APPID  # 应用IDself.APIKey = APIKey  # API Keyself.APISecret = APISecret  # API Secretself.host = urlparse(gpt_url).netloc  # 从url解析出hostself.path = urlparse(gpt_url).path  # 从url解析出pathself.gpt_url = gpt_url  # 完整的url# 生成签名和url的方法def create_url(self):now = datetime.now()  # 当前时间date = format_date_time(mktime(now.timetuple()))  # 格式化的时间戳# 拼接签名原文signature_origin = "host: " + self.host + "\n"signature_origin += "date: " + date + "\n"signature_origin += "GET " + self.path + " HTTP/1.1"# 生成签名signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),digestmod=hashlib.sha256).digest()signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')# 生成授权headerauthorization_origin = f'api_key="{self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')# 生成url参数字典v = {"authorization": authorization,"date": date,"host": self.host}# 构造最终urlurl = self.gpt_url + '?' + urlencode(v)return url# 收到websocket错误的处理
def on_error(ws, error):print("### error:", error)# 收到websocket关闭的处理
def on_close(ws):print("### closed ###")# 收到websocket连接建立的处理
def on_open(ws):thread.start_new_thread(run, (ws,))# 发送请求的方法
def run(ws, *args):data = json.dumps(gen_params(appid=ws.appid, question=ws.question))ws.send(data)# 收到websocket消息的处理
def on_message(ws, message):print(message)data = json.loads(message)code = data['header']['code']if code != 0:print(f'请求错误: {code}, {data}')ws.close()else:choices = data["payload"]["choices"]status = choices["status"]content = choices["text"][0]["content"]print(content, end='')global response_contentresponse_content += contentif status == 2:ws.close()# 生成请求参数
def gen_params(appid, question):"""通过appid和用户的提问来生成请参数"""data = {"header": {"app_id": appid,"uid": "1234"},"parameter": {"chat": {"domain": "general","random_threshold": 0.5,"max_tokens": 2048,"auditing": "default"}},"payload": {"message": {"text": [{"role": "user", "content": question}]}}}return datadef main(appid, api_key, api_secret, gpt_url, question):wsParam = Ws_Param(appid, api_key, api_secret, gpt_url)websocket.enableTrace(False)wsUrl = wsParam.create_url()ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)ws.appid = appidws.question = questionws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})return response_content

相关文章:

从零开始-与大语言模型对话学技术-gradio篇(4)

前言 本文介绍「星火杯」认知大模型场景创新赛中的落选项目- AI命理分析系统,属于个人娱乐练手。总结提炼了往期文章精华并发掘出新的知识。 包括本地部署版本和Web在线版本,两种打包方式基于 半自动化使用.bat手动打包迁移python项目 如何把 Gradio …...

OpenCV项目实战(1)— 如何去截取视频中的帧

前言:Hello大家好,我是小哥谈。针对一段视频,如何去截取视频中的帧呢?本节课就给大家介绍两种方式,一种方式是按一定间隔来截取视频帧,另一种方式是截取视频的所有帧。希望大家学习之后能够有所收获&#x…...

「程序员必须掌握的算法」动态规划「上篇」

动态规划详解 动态规划 (Dynamic Programming) 是一种算法思想,用于解决一些复杂的问题。本文将介绍动态规划的分类、概念和经典例题讲解。 动态规划的分类 动态规划可以分为以下两种类型: 0/1背包问题:该问题是动态规划的一种基本类型。…...

什么是Linux

什么是Linux? 不知道大家是什么时候开始接触Linux,我记得我是大三的时候,那时候通过国嵌、韦东山的教学视频,跟着搭bootloader,修改内核,制作根文件系统,一步步,视频真的很简单&…...

学习笔记|定时器|STC中断|定时器时间计算|STC32G单片机视频开发教程(冲哥)|第十一集:定时器的作用和意义

文章目录 1.定时器的作用和意义定时器中断定时器是定时器和计数器的统称。 2.STC32G单片机定时器使用原理2.1 先设置功能为定时器/计数器(本质都是加法计数器)2.2、在定时器模式下,设置不分频或者12分频∶Tips:选择不分频还是12分频2.3、定时器的工作模式…...

第28节-PhotoShop基础课程-图层操作

文章目录 前言1.像素图层2.删除 Delete3.合并 Ctrl E4.盖印 Ctrl Shift Alt5.图层顺序-拖动就可以6.编组-Ctrl G 管理图层-分类存放7.锁定图层-背景图层8.不透明度9.查找图层 2.智能图层1.能保持图片放大缩小(Ctrl T)的时候不丢失分辨率2.和滤镜配合使…...

CGAL 闵可夫斯基和(Minkowski Sums)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 假设给定两个集合 A , B ∈ R d A,B∈R^d A,B...

Layui快速入门之第二节布局容器(固定宽度与完整宽度)

目录 一&#xff1a;固定宽度 二&#xff1a; 完整宽度 一&#xff1a;固定宽度 将栅格放入一个带有 class"layui-container" 的特定容器中&#xff0c;以便在小屏幕以上的设备中固定宽度&#xff0c;让列可控(两侧有留白效果) <!--固定宽度(两侧有留白效果)--&…...

异地容灾系统和数据仓库中数据同步的设计软件的功能模型

&#xff08; 1&#xff09;初始同步模块 该模块主要是在表进行初始同步时使用的&#xff1b;它能够根据实际需要生成物化视图 及其索引的创建语句&#xff0c;并完成表的初始同步。如果没有特别的要求&#xff0c;则调用普通初 始同步子模块进行目的端表的初始同步&#xff…...

分布式调度 Elastic-job

分布式调度 Elastic-job 1.概述 1.1什么是任务调度 我们可以思考一下下面业务场景的解决方案: 某电商平台需要每天上午10点&#xff0c;下午3点&#xff0c;晚上8点发放一批优惠券某银行系统需要在信用卡到期还款日的前三天进行短信提醒某财务系统需要在每天凌晨0:10分结算…...

第 2 章 线性表(学生健康登记表实现)

1. 示例代码 1) status.h /* DataStructure 预定义常量和类型头文件 */#ifndef STATUS_H #define STATUS_H/* 函数结果状态码 */ #define TRUE 1 /* 返回值为真 */ #define FALSE 0 /* 返回值为假 */ #define RET_OK 0 /* 返回值正确 */ #define INFEASI…...

第三周晨考自测(3.0)

1.获取元素的偏移量 offsetLeft和offsetTop 分别获取的是元素元素左边的偏移量和上边的偏移量 语法&#xff1a;元素对象.offsetLeft /元素对象.offsetTop 返回值&#xff1a;就是该元素对应的偏移量&#xff0c;是一个具体的数字 offsetLeft&#xff1a;该元素相对于参考…...

C++ 结构体

前文 C中的结构体是一种非常有用的数据类型&#xff0c;它允许我们将不同的变量组合在一起&#xff0c;形成一个自定义的数据结构。 结构体在C中的应用非常广泛&#xff0c;它可以用来表示和管理各种实体、对象或数据的属性。比如&#xff0c;在一个学生管理系统中&#xff0c…...

如何使用聊天GPT自定义说明

推荐&#xff1a;使用 NSDT场景编辑器 快速搭建3D应用场景 OpenAI ChatGPT正在席卷全球。一周又一周&#xff0c;更新不断提高您可以使用这种最先进的语言模型做什么的标准。 在这里&#xff0c;我们深入研究了OpenAI最近在ChatGPT自定义指令上发布的公告。此功能最初以测试版…...

mac pyenv无法切换python版本问题

看是zsh还是bash echo $SHELLzsh 配置到&#xff5e;/.zshrc 文件 vim ~/.zshrcexport PYENV_ROOT"$HOME/.pyenv" command -v pyenv >/dev/null || export PATH"$PYENV_ROOT/bin:$PATH" 执行 source ~/.zshrc bash vim ~/.bashrc export PYENV_R…...

API接口接入电商平台案例,采集淘宝天猫拼多多1688京东LAZADA数据按关键字搜索商品示例

按关键字搜索商品数据API接口可以让用户轻松地在海量商品中找到自己需要的商品。这个接口包括多种搜索方式&#xff0c;例如利用关键字搜索商品名称、商品描述、商品分类、商家信息等。同时&#xff0c;还可以通过不同的排序方式进行筛选&#xff0c;例如销量排行、价格排行、评…...

持安-大连万达集团零信任项目入选中国信通院2023零信任优秀案例

2023年8月25日&#xff0c;以“链接云端&#xff0c;可信而安”为主题的“2023首届SecGo云和软件安全大会”在京隆重召开。会上&#xff0c;中国信息通信研究院重磅揭晓了“安全守卫者计划”优秀案例评选结果。 零信任办公安全技术创新企业持安科技&#xff0c;与用户大连万达…...

python28种极坐标绘图函数总结

文章目录 基础图误差线等高线polar场图polar统计图非结构坐标图 &#x1f4ca;python35种绘图函数总结&#xff0c;3D、统计、流场&#xff0c;实用性拉满 matplotlib中的画图函数&#xff0c;大部分情况下只要声明坐标映射是polar&#xff0c;就都可以画出对应的极坐标图。但…...

C#编程基础(万字详解,这一篇就够了)

C#及其开发环境简介 C#概述 C#的编程功能 C#与.Net的关系 .Net C# C#的集成开发环境 Windows上编写C#程序 Linux/Mac OS上编写C#程序 运行第一个HelloWorld程序 C#基本语法 程序实例 C#基本语法 using关键字 class关键字 注释 成员变量 成员函数 实例化一个类…...

SpringBoot中自定义注解

目录 SpringBoot中自定义注解 关于注解的解释 元注解 Documented Target Retention Inherited Native 自定义注解 自定义注解与SpringBoot全局异常处理完成参数校验 约束验证器 自定义全局异常处理器 自定义注解完成数据脱敏 定义脱敏策略枚举 自定义注解 实行脱…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

【杂谈】-递归进化:人工智能的自我改进与监管挑战

递归进化&#xff1a;人工智能的自我改进与监管挑战 文章目录 递归进化&#xff1a;人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管&#xff1f;3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘

美国西海岸的夏天&#xff0c;再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至&#xff0c;这不仅是开发者的盛宴&#xff0c;更是全球数亿苹果用户翘首以盼的科技春晚。今年&#xff0c;苹果依旧为我们带来了全家桶式的系统更新&#xff0c;包括 iOS 26、iPadOS 26…...

HTML 列表、表格、表单

1 列表标签 作用&#xff1a;布局内容排列整齐的区域 列表分类&#xff1a;无序列表、有序列表、定义列表。 例如&#xff1a; 1.1 无序列表 标签&#xff1a;ul 嵌套 li&#xff0c;ul是无序列表&#xff0c;li是列表条目。 注意事项&#xff1a; ul 标签里面只能包裹 li…...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容

目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法&#xff0c;当前调用一个医疗行业的AI识别算法后返回…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

AI病理诊断七剑下天山,医疗未来触手可及

一、病理诊断困局&#xff1a;刀尖上的医学艺术 1.1 金标准背后的隐痛 病理诊断被誉为"诊断的诊断"&#xff0c;医生需通过显微镜观察组织切片&#xff0c;在细胞迷宫中捕捉癌变信号。某省病理质控报告显示&#xff0c;基层医院误诊率达12%-15%&#xff0c;专家会诊…...

代码规范和架构【立芯理论一】(2025.06.08)

1、代码规范的目标 代码简洁精炼、美观&#xff0c;可持续性好高效率高复用&#xff0c;可移植性好高内聚&#xff0c;低耦合没有冗余规范性&#xff0c;代码有规可循&#xff0c;可以看出自己当时的思考过程特殊排版&#xff0c;特殊语法&#xff0c;特殊指令&#xff0c;必须…...

uniapp 集成腾讯云 IM 富媒体消息(地理位置/文件)

UniApp 集成腾讯云 IM 富媒体消息全攻略&#xff08;地理位置/文件&#xff09; 一、功能实现原理 腾讯云 IM 通过 消息扩展机制 支持富媒体类型&#xff0c;核心实现方式&#xff1a; 标准消息类型&#xff1a;直接使用 SDK 内置类型&#xff08;文件、图片等&#xff09;自…...

土建施工员考试:建筑施工技术重点知识有哪些?

《管理实务》是土建施工员考试中侧重实操应用与管理能力的科目&#xff0c;核心考查施工组织、质量安全、进度成本等现场管理要点。以下是结合考试大纲与高频考点整理的重点内容&#xff0c;附学习方向和应试技巧&#xff1a; 一、施工组织与进度管理 核心目标&#xff1a; 规…...