gpt4free/gui/streamlit_app.py

53 lines
1.5 KiB
Python
Raw Normal View History

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
2023-04-24 15:52:44 +00:00
import streamlit as st
2023-04-29 09:25:24 +00:00
from gpt4free import you
2023-04-27 01:12:33 +00:00
def get_answer(question: str) -> str:
# Set cloudflare clearance cookie and get answer from GPT-4 model
2023-04-25 08:11:31 +00:00
try:
result = you.Completion.create(prompt=question)
return result.text
2023-04-25 08:11:31 +00:00
except Exception as e:
2023-04-27 01:12:33 +00:00
# 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.'
)
2023-04-27 01:12:33 +00:00
2023-04-24 15:52:44 +00:00
2023-04-27 01:12:33 +00:00
# Set page configuration and add header
2023-04-24 15:52:44 +00:00
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",
},
2023-04-24 15:52:44 +00:00
)
st.header('GPT4free GUI')
2023-04-27 01:12:33 +00:00
# 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')
2023-04-24 15:52:44 +00:00
if st.button('🧠 Think'):
2023-04-27 01:12:33 +00:00
answer = get_answer(question_text_area)
escaped = answer.encode('utf-8').decode('unicode-escape')
2023-04-27 01:12:33 +00:00
# Display answer
2023-04-24 15:52:44 +00:00
st.caption("Answer :")
st.markdown(escaped)
2023-04-24 15:52:44 +00:00
2023-04-27 01:12:33 +00:00
# Hide Streamlit footer
2023-04-24 15:52:44 +00:00
hide_streamlit_style = """
<style>
footer {visibility: hidden;}
</style>
"""
2023-04-27 01:12:33 +00:00
st.markdown(hide_streamlit_style, unsafe_allow_html=True)