File size: 2,601 Bytes
88b9f42
 
 
 
 
1c92ebb
88b9f42
845b16f
 
 
 
88b9f42
 
 
 
 
 
 
 
 
2ab7bbf
88b9f42
845b16f
88b9f42
 
 
 
f2b2988
88b9f42
 
 
 
 
f2b2988
1c92ebb
88b9f42
 
 
 
 
 
 
2ab7bbf
 
 
88b9f42
 
 
 
aaff33c
88b9f42
aaff33c
2ab7bbf
 
88b9f42
 
 
 
 
 
 
 
f2b2988
2ab7bbf
88b9f42
 
 
 
 
 
2ab7bbf
1c92ebb
 
 
 
 
f2b2988
88b9f42
 
 
 
 
 
f2b2988
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
"""Python file to serve as the frontend"""
import streamlit as st
from streamlit_chat import message
import os
import openai
import time

#OpenAI model deprecated from langchain
#from langchain.llms import OpenAI
#Import OpenAI Chat wrapper
from langchain.chat_models import ChatOpenAI
#Import Prompt Template
from langchain.prompts import PromptTemplate
#Import chains
from langchain.chains import LLMChain
#Import Sequential Chains
from langchain.chains import SimpleSequentialChain



def load_chain(api_key):
    os.environ["OPENAI_API_KEY"] = api_key
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.1)
    #user_in = input("Please describe your Python project in one to two sentences:  ")

    first_prompt = PromptTemplate(
        input_variables=["user_in"],
        template= "Write the outline of the coding steps to develop the program {user_in} in five steps. Use Python3 and Be concise. \n\n"
    )
    #First chain
    chain = LLMChain(llm=llm, prompt=first_prompt)

    second_prompt = PromptTemplate(
        input_variables=["program"],
        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.'''
    )

    chain_two = LLMChain(llm=llm, prompt=second_prompt)

    overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)


    return overall_chain




# From here down is all the StreamLit UI.
st.set_page_config(page_title="Python Project Generator", page_icon=":robot:")
st.header("Python Snippet Generator")

st.write("Enter your OpenAI API key below (not stored between sessions):")
openai_api_key = st.text_input("openai_api_key")

if "generated" not in st.session_state:
    st.session_state["generated"] = []

if "past" not in st.session_state:
    st.session_state["past"] = []


def get_text():
    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")
    return user_input


user_input = get_text()


if user_input:
    chain = load_chain(openai_api_key)
    
    with st.spinner('Wait for it...'):
        output = chain.run(input=user_input)
        time.sleep(5)
    st.success('Done!')
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output)

if st.session_state["generated"]:

    for i in range(len(st.session_state["generated"]) - 1, -1, -1):
        message(st.session_state["generated"][i], key=str(i))
        message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")