Ubuntu开机启动gpt4free的streamlit网页服务

写一个启动时运行的脚本/home/xxx/startstreamlit.sh

#!/bin/sh -e
cd /home/xxx/gpt4free && nohup /home/xxx/py38/bin/python3 /home/xxx/py38/bin/streamlit run  app_streamlit.py &

然后创建一个/etc/rc.local文件,在其中运行前面写的脚本:

#!/bin/bash
bash /home/xxx/startstreamlit.sh
exit 0

这样开机时就会运行/etc/rc.local,并进而运行startstreamlit.sh,启动了里面的streamlit服务。

streamlit的文件为app_streamlit.py,放置在/home/xxx/gpt4free目录,平时启动的话就到这个目录,执行streamlit run app_streamlit.py 即可。文件源码如下:

# cat app_streamlit.py
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))

import streamlit as st
# from gpt4free import you
# from gpt4free import theb
import gpt4free
#from gpt4free import Provider, quora 
from gpt4free import deepai

def get_answer(question: str) -> str:
    # Set cloudflare clearance cookie and get answer from GPT-4 model
    try:
        # result = you.Completion.create(prompt=question)
        # result = theb.Completion.create(prompt=question)
        # response = gpt4free.Completion.create(Provider.Theb, prompt=question)
        #messages = [
        #            {"role": "system", "content": "You are a helpful assistant."},
        #                {"role": "user", "content": question},
        #                        ]
        tmp = ""
        for chunk in deepai.Completion.create(question):
            print(chunk, end="", flush=True)
            tmp += chunk
            status.text(tmp)  # 实时显示返回的字符串
            # return chunk
        return tmp
        return response
        return result.text

    except Exception as e:
        # Return error message if an exception occurs
        return (
            f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.'
        )


# Set page configuration and add header
st.set_page_config(
    page_title="gpt4freeGUI",
    initial_sidebar_state="expanded",
    page_icon="🧠",
    menu_items={
        'Get Help': 'https://github.com/xtekky/gpt4free/blob/main/README.md',
        'Report a bug': "https://github.com/xtekky/gpt4free/issues",
        'About': "### gptfree GUI",
    },
)
st.header('GPT4free GUI')

status = st.empty()  # 用于逐个显示返回字符

# Add text area for user input and button to get answer
question_text_area = st.text_area('🤖 Ask Any Question :', placeholder='Explain quantum computing in 50 words')
if st.button('🧠 Think'):
    answer = get_answer(question_text_area)

    # escaped = answer.encode('utf-8').decode('unicode-escape')
    escaped = answer
    # Display answer
    st.caption("Answer :")
    st.markdown(escaped)

# Hide Streamlit footer
hide_streamlit_style = """
            <style>
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值