Mummia-99 commited on
Commit
9adc003
·
verified ·
1 Parent(s): 138b89d

Upload code_generator_app.py

Browse files
Files changed (1) hide show
  1. code_generator_app.py +32 -0
code_generator_app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import os
4
+
5
+ openai.api_key = "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"
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)