nullzero-live/python-project-generator
3
1"""Python file to serve as the frontend"""2import streamlit as st3from streamlit_chat import message4import os5import openai6import time7 8#OpenAI model deprecated from langchain9#from langchain.llms import OpenAI10#Import OpenAI Chat wrapper11from langchain.chat_models import ChatOpenAI12#Import Prompt Template13from langchain.prompts import PromptTemplate14#Import chains15from langchain.chains import LLMChain16#Import Sequential Chains17from langchain.chains import SimpleSequentialChain18 19 20 21def load_chain(api_key):22 os.environ["OPENAI_API_KEY"] = api_key23 llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.1)24 #user_in = input("Please describe your Python project in one to two sentences: ")25 26 first_prompt = PromptTemplate(27 input_variables=["user_in"],28 template= "Write the outline of the coding steps to develop the program {user_in} in five steps. Use Python3 and Be concise. \n\n"29 )30 #First chain31 chain = LLMChain(llm=llm, prompt=first_prompt)32 33 second_prompt = PromptTemplate(34 input_variables=["program"],35 template= '''Write the python3 code for each step of the {program} described. Use python3 style. Be concise in the code and opinionated about framework choice.'''36 )37 38 chain_two = LLMChain(llm=llm, prompt=second_prompt)39 40 overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)41 42 43 return overall_chain44 45 46 47 48# From here down is all the StreamLit UI.49st.set_page_config(page_title="Python Project Generator", page_icon=":robot:")50st.header("Python Snippet Generator")51 52st.write("Enter your OpenAI API key below (not stored between sessions):")53openai_api_key = st.text_input("openai_api_key")54 55if "generated" not in st.session_state:56 st.session_state["generated"] = []57 58if "past" not in st.session_state:59 st.session_state["past"] = []60 61 62def get_text():63 user_input = st.text_input("Please describe your desired python project in 1-2 sentences. The output will be five steps including code.", key="input")64 return user_input65 66 67user_input = get_text()68 69 70if user_input:71 chain = load_chain(openai_api_key)72 73 with st.spinner('Wait for it...'):74 output = chain.run(input=user_input)75 time.sleep(5)76 st.success('Done!')77 st.session_state.past.append(user_input)78 st.session_state.generated.append(output)79 80if st.session_state["generated"]:81 82 for i in range(len(st.session_state["generated"]) - 1, -1, -1):83 message(st.session_state["generated"][i], key=str(i))84 message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")