Spaces:
Runtime error
Runtime error
Commit ·
2787833
1
Parent(s): acec50e
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import transformers
|
| 4 |
+
import langchain
|
| 5 |
+
import agents
|
| 6 |
+
from streamlit.script_runner import StopException
|
| 7 |
+
|
| 8 |
+
# Define function to reverse prompt engineer code
|
| 9 |
+
def reverse_prompt_engineer(code):
|
| 10 |
+
# Use natural language processing to analyze code
|
| 11 |
+
nlp_analysis = langchain.analyze(code)
|
| 12 |
+
|
| 13 |
+
# Choose the best free pretrained model for this task
|
| 14 |
+
model_name = "microsoft/CodeGPT-small-py-adaptedGPT2"
|
| 15 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
|
| 17 |
+
|
| 18 |
+
# Generate perfect prompt using analyzed code
|
| 19 |
+
perfect_prompt = agents.generate_prompt(nlp_analysis)
|
| 20 |
+
|
| 21 |
+
# Chat with user to make additional changes to prompt
|
| 22 |
+
chatbot = agents.ChatGPT(model=model, tokenizer=tokenizer)
|
| 23 |
+
final_prompt = chatbot.chat(perfect_prompt)
|
| 24 |
+
|
| 25 |
+
# Use final prompt to generate similar code using ChatGPT
|
| 26 |
+
generated_code = chatbot.generate_code(final_prompt)
|
| 27 |
+
|
| 28 |
+
return generated_code
|
| 29 |
+
|
| 30 |
+
# Streamlit UI
|
| 31 |
+
st.set_page_config(page_title="Code Generator", layout="wide", initial_sidebar_state="expanded")
|
| 32 |
+
st.title("Code Generator")
|
| 33 |
+
|
| 34 |
+
st.sidebar.title("Input")
|
| 35 |
+
code_input = st.sidebar.text_area("Enter your code here:", '''
|
| 36 |
+
def greet(name):
|
| 37 |
+
print("Hello, " + name + ". How are you doing today?")
|
| 38 |
+
|
| 39 |
+
greet("John")
|
| 40 |
+
''')
|
| 41 |
+
|
| 42 |
+
if st.sidebar.button("Generate Code"):
|
| 43 |
+
if code_input.strip() == "":
|
| 44 |
+
st.error("Please enter some code in the input field.")
|
| 45 |
+
else:
|
| 46 |
+
try:
|
| 47 |
+
generated_code = reverse_prompt_engineer(code_input)
|
| 48 |
+
st.code(generated_code)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"An error occurred: {str(e)}")
|
| 51 |
+
raise StopException
|
| 52 |
+
|