Spaces:
Sleeping
Sleeping
| 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) |