File size: 1,070 Bytes
228719e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9adc003
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
import streamlit as st
import openai
import os

openai.api_key = os.getenv("openapikey")

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)