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