import streamlit as st from model import GeneralModel import os import chess import chess.engine import stat import re def app(): # Creating an object of prediction service pred = GeneralModel() api_key = st.sidebar.text_input("APIkey get it here https://beta.openai.com/account/api-keys", type="password") # Using the streamlit cache @st.cache def eval(fenstring): output = "" os.chmod("./stockfish_14_x64_popcnt",0o0777) engine = chess.engine.SimpleEngine.popen_uci("./stockfish_14_x64_popcnt") # Score: PovScore(Cp(+20), WHITE) board = chess.Board(fenstring) info = engine.analyse(board, chess.engine.Limit(depth=20),multipv=3) # Score: PovScore(Mate(+1), WHITE) engine.quit() evalstring = str(info) print (evalstring) #hacky way to parse best move, need to imprpve substrings = evalstring.split('Move.from_uci') # Extract the text between the single quotes using a regular expression match = re.search(r"'(.*?)'", substrings[1]) topmove = match.group(1) print(topmove) # Create a list of lists, where each inner list contains the UCI strings for a single move stringprompt = 'Based on the chess board position at this fen string ' + fenstring + 'can you give an in depth analysis as to why ' + topmove + ' is the best move to play based on Material, Development, Center-control, King-safety and Pawn-structures if relevant. Also describe any tactics or ideas that we should be thinking about' return pred.model_prediction(input=stringprompt.strip() , api_key=api_key) def process_prompt(input): return pred.model_prediction(input=input.strip() , api_key=api_key) if api_key: # Setting up the Title st.title("Stockfish fen eval to GPT-3 top move explanation chess coach") # st.write("---") s_example = "r2qnrk1/1p1bbppp/p1np4/2p2p2/P1B1P3/2NP1NQ1/1PP3PP/R1B2RK1 w - - 0 12" input = st.text_area( "input a fen string and get a gpt 3.5 explanation of the top move", value=s_example, max_chars=1250, height=50, ) if st.button("Submit"): with st.spinner(text="In progress"): report_text = eval(input) st.markdown(report_text) else: st.error("🔑 Please enter API Key")