Spaces:
Running
Running
Rename code_generator_app.py to app.py
Browse files- code_generator_app.py → app.py +31 -31
code_generator_app.py → app.py
RENAMED
@@ -1,32 +1,32 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import openai
|
3 |
-
import os
|
4 |
-
|
5 |
-
openai.api_key = "
|
6 |
-
|
7 |
-
def refactor_code(code):
|
8 |
-
|
9 |
-
try:
|
10 |
-
response = openai.chat.completions.create(
|
11 |
-
model="gpt-3.5-turbo", # Or "gpt-4" if you have access
|
12 |
-
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}"
|
27 |
-
st.title("Code Generator")
|
28 |
-
code = st.text_input("Enter Code:")
|
29 |
-
if st.button("Submit"):
|
30 |
-
if code:
|
31 |
-
refactored_code = refactor_code(code)
|
32 |
st.code(refactored_code)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("openapikey")
|
6 |
+
|
7 |
+
def refactor_code(code):
|
8 |
+
|
9 |
+
try:
|
10 |
+
response = openai.chat.completions.create(
|
11 |
+
model="gpt-3.5-turbo", # Or "gpt-4" if you have access
|
12 |
+
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}"
|
27 |
+
st.title("Code Generator")
|
28 |
+
code = st.text_input("Enter Code:")
|
29 |
+
if st.button("Submit"):
|
30 |
+
if code:
|
31 |
+
refactored_code = refactor_code(code)
|
32 |
st.code(refactored_code)
|