Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,46 @@
|
|
1 |
-
import
|
2 |
-
import tkinter.ttk as ttk
|
3 |
-
from tkinter.scrolledtext import ScrolledText
|
4 |
-
from tkinter import filedialog
|
5 |
from transformers import pipeline
|
6 |
import logging
|
7 |
|
8 |
# Logging Setup
|
9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
try:
|
51 |
-
logging.info(f"Generating code with input: {input_text}") # Log the input
|
52 |
-
prompt = f"Develop code for the following task: {input_text}"
|
53 |
-
code = self.generator(prompt, max_length=max_length, num_return_sequences=1, temperature=temperature)[0]['generated_text'].strip()
|
54 |
-
self.output_area.delete("1.0", tk.END)
|
55 |
-
self.output_area.insert(tk.END, code)
|
56 |
-
logging.info("Code generation completed successfully.") # Log successful generation
|
57 |
-
except Exception as e:
|
58 |
-
logging.error(f"Error generating code: {e}")
|
59 |
-
self.output_area.insert(tk.END, f"Error generating code: {e}")
|
60 |
-
|
61 |
-
def save_code(self):
|
62 |
-
file_path = filedialog.asksaveasfilename(defaultextension=".py")
|
63 |
-
if file_path:
|
64 |
-
with open(file_path, "w") as file:
|
65 |
-
file.write(self.output_area.get("1.0", tk.END))
|
66 |
-
logging.info(f"Code saved to {file_path}")
|
67 |
-
|
68 |
-
if __name__ == "__main__":
|
69 |
-
root = tk.Tk()
|
70 |
-
app = CodeGeneratorApp(root)
|
71 |
-
root.mainloop()
|
|
|
1 |
+
import streamlit as st
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
import logging
|
4 |
|
5 |
# Logging Setup
|
6 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
7 |
|
8 |
+
# Initialize the generator
|
9 |
+
generator = pipeline('text-generation', model='bigscience/T0_3B')
|
10 |
+
|
11 |
+
def generate_code(task_description, max_length, temperature):
|
12 |
+
try:
|
13 |
+
logging.info(f"Generating code with input: {task_description}")
|
14 |
+
prompt = f"Develop code for the following task: {task_description}"
|
15 |
+
code = generator(prompt, max_length=max_length, num_return_sequences=1, temperature=temperature)[0]['generated_text'].strip()
|
16 |
+
logging.info("Code generation completed successfully.")
|
17 |
+
return code
|
18 |
+
except Exception as e:
|
19 |
+
logging.error(f"Error generating code: {e}")
|
20 |
+
return f"Error generating code: {e}"
|
21 |
+
|
22 |
+
st.title("Advanced Code Generator")
|
23 |
+
|
24 |
+
# Input Section
|
25 |
+
st.header("Task Description")
|
26 |
+
task_description = st.text_area("Describe the task for which you need code:", height=150)
|
27 |
+
|
28 |
+
# Options Section
|
29 |
+
st.header("Options")
|
30 |
+
max_length = st.slider("Max Length", min_value=50, max_value=1000, value=250, step=50)
|
31 |
+
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.1)
|
32 |
+
|
33 |
+
# Generate Code Button
|
34 |
+
if st.button("Generate Code"):
|
35 |
+
if task_description:
|
36 |
+
generated_code = generate_code(task_description, max_length, temperature)
|
37 |
+
st.header("Generated Code")
|
38 |
+
st.code(generated_code, language='python')
|
39 |
+
|
40 |
+
# Save Code Section (Optional)
|
41 |
+
if 'generated_code' in locals():
|
42 |
+
if st.button("Save Code"):
|
43 |
+
file_name = st.text_input("Enter file name to save:", value="generated_code.py")
|
44 |
+
with open(file_name, "w") as file:
|
45 |
+
file.write(generated_code)
|
46 |
+
st.success(f"Code saved to {file_name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|