Spaces:
Sleeping
Sleeping
File size: 7,625 Bytes
f69aeb0 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
import os
import openai
import streamlit as st
from langchain.agents import load_tools, initialize_agent
from langchain.llms import OpenAI
import time
from streamlit_option_menu import option_menu
def initialize_tools_and_agent(openai_api_key):
llm = OpenAI(temperature=0,openai_api_key=openai_api_key)
tools = load_tools(["llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
return agent
# Set Streamlit page configuration
st.set_page_config(page_title='Math ChatGPT4',
page_icon=":memo:",
layout='wide',
initial_sidebar_state='collapsed')
st.title('Mathematical Puzzles Solving Tool')
#st.header('Language Model App. for Mathematical Puzzle Solver')
# Set CSS properties for HTML components
st.markdown("""
<style>
body {
color: #ff0;
background-color: #4f8bf9;
}
h1, h2 {
color: #000000;
}
</style>
""", unsafe_allow_html=True)
hide_style='''
<style>
#MainMenu {visibility:hidden;}
footer {visibility:hidden;}
.css-hi6a2p {padding-top: 0rem;}
head {visibility:hidden;}
</style>
'''
with st.sidebar:
selected = option_menu("Main Menu", ["Home", 'Help'],
icons=['house', 'gear'], menu_icon="cast", default_index=0)
selected
hide_style='''
<style>
#MainMenu {visibility:hidden;}
footer {visibility:hidden;}
.css-hi6a2p {padding-top: 0rem;}
head {visibility:hidden;}
</style>
'''
hide_streamlit_style = """
<style>
div[data-testid="stToolbar"] {
visibility: hidden;
height: 0%;
position: fixed;
}
div[data-testid="stDecoration"] {
visibility: hidden;
height: 0%;
position: fixed;
}
div[data-testid="stStatusWidget"] {
visibility: hidden;
height: 0%;
position: fixed;
}
#MainMenu {
visibility: hidden;
height: 0%;
}
header {
visibility: hidden;
height: 0%;
}
footer {
visibility: hidden;
height: 0%;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.markdown("""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #f8f9fa;
color: black;
text-align: center;
padding: 10px;
}
</style>
<div class="footer">
<p>Copyright © 2023 AI-Books. All Rights Reserved.</p>
</div>
""", unsafe_allow_html=True)
if selected=="Help":
st.markdown(hide_style,unsafe_allow_html=True)
# st.title("Help")
# Import required libraries
import streamlit as st
# Set Streamlit page configuration
#st.set_page_config(page_title='Help - XLS Office Documents Analysis with ChatGPT4 NLP Model',
# page_icon=":memo:",
# layout='wide',
# initial_sidebar_state='collapsed')
import streamlit as st
# Set CSS properties for HTML components
st.markdown("""
<style>
body {
color: #000;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
h1, h2 {
color: #ff6600;
text-align: center;
}
</style>
""", unsafe_allow_html=True)
# Display application title
# st.markdown("""
# <h1>ChatGPT-4 Bot: Puzzle Solving and Imagination Companion</h1>
# <h2>Your Partner for Puzzles and Creative Adventures</h2>
# """, unsafe_allow_html=True)
## Display authorship details
# st.markdown("""
## Developed by [Your Name]
# * AI Enthusiast
# * Expert in AI-driven Conversations
# """, unsafe_allow_html=True)
# Display application introduction
st.markdown("""
### Welcome to ChatGPT-4 Bot!
Introducing your versatile AI companion, ChatGPT-4 Bot. This interactive tool is designed to be your guide for solving mathematical puzzles . Whether you're a curious kid, a young learner, or simply looking to exercise your mind, ChatGPT-4 Bot is here to assist.
### Dive into Puzzle Challenges
Are math puzzles your thing? ChatGPT-4 Bot is equipped to help you tackle a variety of mathematical challenges. From solving equations to deciphering number patterns, this AI-powered tool is your partner in puzzle-solving.
### How to Interact
Puzzles Mode: Input math-related questions or challenges and receive step-by-step solutions.
### Who Can Benefit
- Students seeking assistance with math problems
- Curious minds interested in AI-driven conversations
### Get in Touch
If you have any inquiries or suggestions, don't hesitate to get in touch. Let's embark on a journey of exploration and learning together!
""", unsafe_allow_html=True)
#-----------------
hide_style='''
<style>
#MainMenu {visibility:hidden;}
footer {visibility:hidden;}
.css-hi6a2p {padding-top: 0rem;}
head {visibility:hidden;}
</style>
'''
st.markdown(hide_style,unsafe_allow_html=True)
if selected=="Home":
openai_api_key = st.sidebar.text_input('OpenAI API Key', type="password")
question_examples = [
"A store sells oranges in packs of 5 and apples in packs of 8. If John bought 7 packs of oranges and 4 packs of apples, how many fruits did he buy in total?",
"A box contains 24 red balls and 36 blue balls. If 30% of the red balls and 40% of the blue balls are removed, how many balls are left in the box?",
"A company has 800 employees. If 25% of the employees are managers and the rest are regular employees, how many regular employees are there in the company?",
"A classroom has a total of 35 students. If 40% of the students are boys, how many girls are there in the classroom?",
# Add more examples as needed
]
# Dropdown select box for question examples
selected_example = st.selectbox('Choose a question example:', question_examples)
# Pre-populate the question field with the selected example
# question = st.text_input('Enter your question:', value=selected_example)
math_question = st.text_area("selected example or write any puzzle and press enter:", value=selected_example)
if not openai_api_key or not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠️')
else:
if st.button('Solve'):
try:
agent = initialize_tools_and_agent(openai_api_key)
response = agent.run(math_question)
with st.spinner('Solving...'):
time.sleep(2) # Simulate processing time
st.success('Solved!')
st.markdown(f"**The Solution Is:**\n\n{response}")
if(response=="Agent stopped due to iteration limit or time limit."):
st.warning('Please enter your Puzzle!mathmatical tool', icon='⚠️')
except openai.error.OpenAIError as e:
#st.error(f"An error occurred: {e}")
st.warning('Please enter your puzzle! mathmatical tool', icon='⚠️')
except Exception as e:
#st.error(f"An unexpected error occurred: {e}")
st.warning('Please enter your puzzle!mathmatical tool', icon='⚠️')
|