MaMaL-Gen / app.py
hanbin's picture
Update app.py
fcb444c
raw
history blame
No virus
6.08 kB
import streamlit as st
import os
import openai
import backoff
# lucaslane5h8a@hotmail.com----hK4H0M64ihK4H0M64i----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
# os.environ["http_proxy"]="127.0.0.1:7890"
# os.environ["https_proxy"]="127.0.0.1:7890"
openai.api_key="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"
st.set_page_config(
page_title="首页",
page_icon="🚀",
layout="centered",
initial_sidebar_state="auto",
)
# set_page_config配置Streamlit应用程序的页面设置。自定义应用程序的标题、图标、布局等方面,以提供更好的用户体验。
# 注意:set_page_config必须在应用程序的所有其他元素之前调用,否则会引发异常。
# 参数说明:
# page_title:可选参数,用于设置应用程序的标题,通常显示在浏览器的标签页上。
# page_icon:可选参数,用于设置应用程序的图标,通常显示在浏览器标签页和书签栏中。
# layout:可选参数,用于设置应用程序的布局方式,可以是"centered"(居中)或"wide"(宽屏)。
# initial_sidebar_state:可选参数,用于设置侧边栏的初始状态。可以是"auto"(自动展开)或"collapsed"(折叠)
def init_sidebar():
"""
初始化侧边栏
:return:
"""
st.sidebar.title("关于我们")
markdown = """
汇报人:高洺策
其他小组成员:周小渲(组长)、王瑞琪、杨畔、宣乐卓、雷友素、单宁、王钦、刘亭秀、吴林泽、武俊呈
"""
st.sidebar.info(markdown)
logo = "./image/laomo.png"
st.sidebar.image(logo)
st.sidebar.title("劳模风范")
st.sidebar.image("./image/laomo1.png", use_column_width=True)
st.sidebar.image("./image/laomo2.png", use_column_width=True)
st.sidebar.image("./image/laomo3.png", use_column_width=True)
st.sidebar.image("./image/gongjiang1.png", use_column_width=True)
st.sidebar.image("./image/gongjiang2.png", use_column_width=True)
st.sidebar.image("./image/gongjiang3.png", use_column_width=True)
def init_content():
"""
初始化内容
:return:
"""
# Customize page title
st.title("劳模智能体(Agent)")
st.markdown(
"""
劳模Agent,即劳模智能体,该智能体可以讲述相关劳模的事迹以及与人类进行沟通,可以作为劳模学习和教学的辅助工具。
"""
)
# 插入图片,让图片自适应
st.image("./image/title.png",use_column_width=True)
# st.header("Instructions")
#
# markdown = """
# 1. For the [GitHub repository](https://github.com/giswqs/geemap-apps) or [use it as a template](https://github.com/new?template_name=geemap-apps&template_owner=giswqs) for your own project.
# 2. Customize the sidebar by changing the sidebar text and logo in each Python files.
# 3. Find your favorite emoji from https://emojipedia.org.
# 4. Add a new app to the `pages/` directory with an emoji in the file name, e.g., `1_🚀_Chart.py`.
# """
#
# st.markdown(markdown)
# 我要构建一个交互式的应用程序,让用户可以在应用程序中输入一些内容,然后应用程序会根据用户的输入做出相应的响应。
# 输入框,让用户输入内容
st.header("输入--")
text_area = st.text_area("", "在这里输入你的需求~~~~~~~~比如 你是谁?")
# 如果文本内容等于“你是谁?”,则输出“我是劳模智能体,我可以讲述相关劳模的事迹以及与人类进行沟通,可以作为劳模学习和教学的辅助工具。”
# 写一个标题
st.header("输出--")
# 定义一个输出框,默认输出“在这里输出模型回复~~~~~~~~”
text = st.empty()
# 修改输出框为多行文本框
# output_area = st.text_area("", "在这里输出模型回复~~~~~~~~")
# text.text("在这里输出模型回复~~~~~~~~")
if text_area == "你是谁?":
# st.success("我是劳模智能体,我可以讲述相关劳模的事迹以及与人类进行沟通,可以作为劳模学习和教学的辅助工具。")
# 在输出框output_area中显示文本内容"我是劳模智能体,我可以讲述相关劳模的事迹以及与人类进行沟通,可以作为劳模学习和教学的辅助工具。你可以随意向我提出问题,我会尽力回答你的问题。"
st.write("我是劳模智能体,我可以讲述相关劳模的事迹以及与人类进行沟通,可以作为劳模学习和教学的辅助工具。你可以随意向我提出问题,我会尽力回答你的问题。")
else:
@backoff.on_exception(
backoff.fibo,
# https://platform.openai.com/docs/guides/error-codes/python-library-error-types
(
openai.error.APIError,
openai.error.Timeout,
openai.error.RateLimitError,
openai.error.ServiceUnavailableError,
openai.error.APIConnectionError,
KeyError,
),
)
def call_lm(model,messages,max_tokens,temperature,stop_words):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
stop=stop_words,
)
return response.choices[0].message["content"].strip()
model = "gpt-3.5-turbo-0613"
messages=[
{"role": "system", "content": "你是一个劳模智能体,了解中国的劳模事迹。下面你需要回答用户提出的问题"},
{"role": "user", "content": text_area},
]
print("messages",messages)
max_tokens = 256
temperature = 0.9
stop_words = []
response = call_lm(model,messages,max_tokens,temperature,stop_words)
print("response",response)
st.write(response)
if __name__ == '__main__':
init_sidebar()
init_content()
pass