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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -59
app.py CHANGED
@@ -1,69 +1,69 @@
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()
 
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
+ if st.button("Save", key="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()