Code_Generator / code_generator_app.py
Mummia-99's picture
Upload code_generator_app.py
9adc003 verified
raw
history blame
1.13 kB
import streamlit as st
import openai
import os
openai.api_key = "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"
def refactor_code(code):
try:
response = openai.chat.completions.create(
model="gpt-3.5-turbo", # Or "gpt-4" if you have access
messages=[
{
"role": "system",
"content": "You are a helpful assistant that generate code. Provide the code directly, without explanation unless specifically requested.",
},
{
"role": "user",
"content": f"generated the following text based code:\n{code}",
},
],
max_tokens=800, #increased tokens as chat api is more verbose.
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {e}"
st.title("Code Generator")
code = st.text_input("Enter Code:")
if st.button("Submit"):
if code:
refactored_code = refactor_code(code)
st.code(refactored_code)