acecalisto3 commited on
Commit
d0a641b
·
verified ·
1 Parent(s): a26313b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -65
app.py CHANGED
@@ -1,71 +1,46 @@
1
- import tkinter as tk
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
- class CodeGeneratorApp:
12
- def __init__(self, master):
13
- self.master = master
14
- master.title("Advanced Code Generator")
15
- self.create_widgets()
16
- self.generator = pipeline('text-generation', model='bigscience/T0_3B')
17
-
18
- def create_widgets(self):
19
- # Input Frame
20
- input_frame = ttk.LabelFrame(self.master, text="Task Description")
21
- input_frame.pack(padx=10, pady=10)
22
- self.input_area = ScrolledText(input_frame, wrap=tk.WORD, width=80, height=10)
23
- self.input_area.pack(padx=5, pady=5)
24
-
25
- # Options Frame
26
- options_frame = ttk.LabelFrame(self.master, text="Options")
27
- options_frame.pack(padx=10, pady=5)
28
- ttk.Label(options_frame, text="Max Length:").grid(row=0, column=0, sticky="w")
29
- self.max_length_var = tk.IntVar(value=250)
30
- ttk.Entry(options_frame, textvariable=self.max_length_var, width=5).grid(row=0, column=1)
31
- self.temp_var = tk.DoubleVar(value=0.7)
32
- ttk.Label(options_frame, text="Temperature:").grid(row=1, column=0, sticky="w")
33
- ttk.Entry(options_frame, textvariable=self.temp_var, width=5).grid(row=1, column=1)
34
-
35
- # Buttons
36
- ttk.Button(self.master, text="Generate Code", command=self.generate_code).pack(pady=10)
37
- ttk.Button(self.master, text="Save Code", command=self.save_code).pack()
38
-
39
- # Output Frame
40
- output_frame = ttk.LabelFrame(self.master, text="Generated Code")
41
- output_frame.pack(padx=10, pady=10, expand=True, fill="both")
42
- self.output_area = ScrolledText(output_frame, wrap=tk.WORD, width=80)
43
- self.output_area.pack(padx=5, pady=5, expand=True, fill="both")
44
-
45
- def generate_code(self):
46
- input_text = self.input_area.get("1.0", tk.END).strip()
47
- max_length = self.max_length_var.get()
48
- temperature = self.temp_var.get()
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}")