前言
本文介绍「星火杯」认知大模型场景创新赛中的落选项目- AI命理分析系统,属于个人娱乐练手。总结提炼了往期文章精华并发掘出新的知识。
包括本地部署版本和Web在线版本,两种打包方式基于
半自动化使用.bat手动打包迁移python项目
如何把 Gradio 应用上传到 Hugging Face
往期回顾:
从零开始-与大语言模型对话学技术-gradio篇(1)
从零开始-与大语言模型对话学技术-gradio篇(2)
从零开始-与大语言模型对话学技术-gradio篇(3)
项目简介
本项目是一个集成化的AI命理分析系统,实现了星座解析、塔罗解牌、八字合婚等多种智能化命理服务。系统通过Python和Gradio实现了交互式的网页界面,用户只需要输入必要个人信息,即可获得智能的命理运势分析。
Github链接
使用说明
最新Web在线版本使用
Web公开版本已在Hugging Face开源,点击链接即可在线使用,不同于本地版本,你必须配置自己的星火APl,全平台可用
hugging face连接
本地部署版本
- 打开AI命理分析系统V4.0进入虚拟python环境,自动检测依赖,安装环境并运行程序
显示如下提示表示编译运行成功,打开这个连接即可进入系统
- 你也可以打开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,
rungradio 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.tg
hTG = 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_url
if 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_url
def horoscope_reading(sex, name, birth_year, birth_month, birth_day, birth_hour, star):
XhApi.response_content = ''
global appid, api_secret, api_key, gpt_url
birth_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_url
if 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_url
birth_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_url
birth_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_url
birth_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_url
if 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_url
birth_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_url
birth_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_url
birth_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_url
if 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 # 应用ID
self.APIKey = APIKey # API Key
self.APISecret = APISecret # API Secret
self.host = urlparse(gpt_url).netloc # 从url解析出host
self.path = urlparse(gpt_url).path # 从url解析出path
self.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')
# 生成授权header
authorization_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
}
# 构造最终url
url = 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_content
response_content += content
if 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 data
def 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 = appid
ws.question = question
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
return response_content