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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -26
app.py CHANGED
@@ -1,46 +1,67 @@
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}")
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline, set_seed
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 with seed for reproducibility
9
+ set_seed(42)
10
  generator = pipeline('text-generation', model='bigscience/T0_3B')
11
 
12
+ def generate_code(task_description, max_length, temperature, num_return_sequences):
13
+ """Generate code based on the provided task description."""
14
  try:
15
  logging.info(f"Generating code with input: {task_description}")
16
  prompt = f"Develop code for the following task: {task_description}"
17
+ response = generator(prompt, max_length=max_length, num_return_sequences=num_return_sequences, temperature=temperature)
18
+ codes = [resp['generated_text'].strip() for resp in response]
19
  logging.info("Code generation completed successfully.")
20
+ return codes
21
  except Exception as e:
22
  logging.error(f"Error generating code: {e}")
23
+ return [f"Error generating code: {e}"]
24
 
25
+ def main():
26
+ st.set_page_config(page_title="Advanced Code Generator", layout="wide")
27
 
28
+ st.title("Advanced Code Generator")
29
+ st.markdown("This application generates code based on the given task description using a text-generation model.")
 
30
 
31
+ # Input Section
32
+ st.header("Task Description")
33
+ task_description = st.text_area("Describe the task for which you need code:", height=150)
 
34
 
35
+ # Options Section
36
+ st.header("Options")
37
+ max_length = st.slider("Max Length", min_value=50, max_value=1000, value=250, step=50, help="Maximum length of the generated code.")
38
+ temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.1, help="Controls the creativity of the generated code.")
39
+ num_return_sequences = st.slider("Number of Sequences", min_value=1, max_value=5, value=1, step=1, help="Number of code snippets to generate.")
 
40
 
41
+ # Generate Code Button
42
+ if st.button("Generate Code"):
43
+ if task_description.strip():
44
+ with st.spinner("Generating code..."):
45
+ generated_codes = generate_code(task_description, max_length, temperature, num_return_sequences)
46
+ st.header("Generated Code")
47
+ for idx, code in enumerate(generated_codes):
48
+ st.subheader(f"Generated Code {idx + 1}")
49
+ st.code(code, language='python')
50
+ else:
51
+ st.error("Please enter a task description.")
52
+
53
+ # Save Code Section
54
+ if 'generated_codes' in locals() and generated_codes:
55
+ st.header("Save Code")
56
+ selected_code_idx = st.selectbox("Select which code to save:", range(1, len(generated_codes) + 1)) - 1
57
  file_name = st.text_input("Enter file name to save:", value="generated_code.py")
58
+ if st.button("Save", key="save_code"):
59
+ if file_name:
60
+ with open(file_name, "w") as file:
61
+ file.write(generated_codes[selected_code_idx])
62
+ st.success(f"Code saved to {file_name}")
63
+ else:
64
+ st.error("Please enter a valid file name.")
65
+
66
+ if __name__ == "__main__":
67
+ main()