fynn3003 commited on
Commit
20c75d5
1 Parent(s): 37c91c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain.chains import LLMChain
6
+ from langchain.chains import SimpleSequentialChain
7
+
8
+ def load_chain(api_key):
9
+ os.environ["OPENAI_API_KEY"] = api_key
10
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.1)
11
+ first_prompt = PromptTemplate(
12
+ input_variables=["user_in"],
13
+ template= "Write the outline of the coding steps to develop the program {user_in} in five steps. Use Python3 and Be concise. \n\n"
14
+ )
15
+ chain = LLMChain(llm=llm, prompt=first_prompt)
16
+
17
+ second_prompt = PromptTemplate(
18
+ input_variables=["program"],
19
+ 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.'''
20
+ )
21
+
22
+ chain_two = LLMChain(llm=llm, prompt=second_prompt)
23
+
24
+ overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)
25
+ return overall_chain
26
+
27
+ def answer_question(api_key, question):
28
+ chain = load_chain(api_key)
29
+ output = chain.run(input=question)
30
+ return output
31
+
32
+ ifaces = gr.Interface(
33
+ fn=answer_question,
34
+ inputs=[gr.inputs.Textbox(
35
+ label="Your OpenAI API Key",
36
+ placeholder="e.g. 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"),
37
+ gr.inputs.Textbox(label="Write a python script to:",
38
+ placeholder="e.g. Find the 10th number of the Fibonacci sequence")],
39
+ outputs=gr.outputs.Textbox(label="User guide"),
40
+ title="Python Code Generator",
41
+ description="Enter your OpenAI API key below and a description of your desired python project in 1-2 sentences"
42
+ )
43
+
44
+ ifaces.launch()