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

#Import LLM wrapper
from langchain.llms import OpenAI
#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 = OpenAI(temperature=0.3)
    #user_in = input("Please describe your Python project in one to two sentences:  ")

    first_prompt = PromptTemplate(
        input_variables=["user_in"],
        template= "You are a senior Python Engineer. List five steps required to develop the software project specified. Only include the 5 steps as a high level outline. Explain that this is only a guide. \n\n {user_in}"
    )
    #First chain
    chain = LLMChain(llm=llm, prompt=first_prompt)

    second_prompt = PromptTemplate(
        input_variables=["project"],
        template= "As the senior engineer, Produce the python3 code for each step of the software {project} described. Use appropriate style, classes and variables. Be verbose 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 code in 1-2 sentences", key="input")
    return user_input


user_input = get_text()


if user_input:
    chain = load_chain(openai_api_key)
    output = chain.run(input=user_input)

    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")