acecalisto3 commited on
Commit
7c640fa
·
verified ·
1 Parent(s): 2455726

Update app.py

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