Mummia-99/Code_Generator
0
1import streamlit as st2import openai3import os4 5openai.api_key = os.getenv("openapikey")6 7def refactor_code(code):8 9 try:10 response = openai.chat.completions.create(11 model="gpt-3.5-turbo", # Or "gpt-4" if you have access12 messages=[13 {14 "role": "system",15 "content": "You are a helpful assistant that generate code. Provide the code directly, without explanation unless specifically requested.",16 },17 {18 "role": "user",19 "content": f"generated the following text based code:\n{code}",20 },21 ],22 max_tokens=800, #increased tokens as chat api is more verbose.23 )24 return response.choices[0].message.content.strip()25 except Exception as e:26 return f"An error occurred: {e}"27st.title("Code Generator")28code = st.text_input("Enter Code:")29if st.button("Submit"):30 if code:31 refactored_code = refactor_code(code)32 st.code(refactored_code)