Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,9 @@ import subprocess
|
|
| 9 |
API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 10 |
|
| 11 |
# Page configuration
|
| 12 |
-
st.set_page_config(page_title="
|
| 13 |
|
| 14 |
# --- Helper Functions ---
|
| 15 |
-
def check_command_availability(command):
|
| 16 |
-
"""Checks if a command is available in the system's PATH."""
|
| 17 |
-
try:
|
| 18 |
-
subprocess.run(["which", command], check=True, capture_output=True)
|
| 19 |
-
return True
|
| 20 |
-
except subprocess.CalledProcessError:
|
| 21 |
-
return False
|
| 22 |
-
|
| 23 |
def send_message_to_model(message, model_name, temperature, top_p, top_k, max_tokens):
|
| 24 |
"""Sends a message to the AI model and returns the response."""
|
| 25 |
try:
|
|
@@ -42,91 +34,82 @@ def send_message_to_model(message, model_name, temperature, top_p, top_k, max_to
|
|
| 42 |
st.error(f"β Error communicating with the AI: {e}")
|
| 43 |
return None
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
"""Generates a
|
| 47 |
prompt = f"""
|
| 48 |
-
You are an expert
|
| 49 |
|
| 50 |
-
**Goal:** Create the most complete, detailed, efficient, and secure
|
| 51 |
|
| 52 |
-
**
|
| 53 |
|
| 54 |
**Detail Level:** {detail_level}
|
| 55 |
-
**Script Type:** {script_type}
|
| 56 |
**Security Level:** {security_level}
|
| 57 |
**Prompt Detail Level**:{prompt_detail}
|
| 58 |
-
**
|
| 59 |
-
**
|
| 60 |
-
**Custom
|
| 61 |
-
**
|
| 62 |
|
| 63 |
**Response Format:**
|
| 64 |
-
- Respond in Markdown format, including a
|
| 65 |
-
- The
|
| 66 |
- Do not include comments, explanations, or any other text outside the code block.
|
| 67 |
-
- The
|
| 68 |
- The code must be realistic, using real-world examples, data, and situations.
|
| 69 |
- Explore different approaches, techniques, and advanced practices, always prioritizing security and efficiency.
|
| 70 |
-
-
|
| 71 |
-
-
|
| 72 |
-
- If the description asks to create a file, the
|
| 73 |
-
- If the description asks to read a file, the
|
| 74 |
-
- Use
|
| 75 |
-
-
|
| 76 |
|
| 77 |
**Log Level**:{log_level}
|
| 78 |
-
**Error Handling**:{add_error_handling}
|
| 79 |
|
| 80 |
**Important:**
|
| 81 |
-
- Generate only one
|
| 82 |
- Create the longest, most complete, and detailed code possible to cover a wide range of possibilities and scenarios.
|
| 83 |
-
- Consider all the details of the request, expanding the response and improving the
|
| 84 |
-
- Use contextual information (such as the
|
| 85 |
- If possible, use incremental reasoning to add improvements, expansions, and considerations to your code.
|
| 86 |
-
- Use the history of the conversations so that the response is incremental.
|
| 87 |
- If the prompt asks to manage a service or process, consider different init systems (systemd, init, etc.).
|
| 88 |
-
- If
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
"""
|
| 91 |
response = send_message_to_model(prompt, model_name, temperature, top_p, top_k, max_tokens)
|
| 92 |
return response
|
| 93 |
|
| 94 |
-
def
|
| 95 |
-
"""Parses the markdown and saves the
|
| 96 |
-
match = re.search(r'```
|
| 97 |
if match:
|
| 98 |
-
|
| 99 |
else:
|
| 100 |
-
|
| 101 |
|
| 102 |
-
file_name = f"
|
| 103 |
|
| 104 |
if add_header:
|
| 105 |
current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 106 |
-
header = f"""
|
| 107 |
-
|
| 108 |
-
# Script Generated by Google Gemini 2 Linux Command Gen Pro
|
| 109 |
# Date: {current_date}
|
| 110 |
# Author: Elias Andrade AKA Chaos4455
|
| 111 |
#===============================================================================\n"""
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
with open(file_name, "w", encoding=encoding) as f:
|
| 116 |
-
f.write(
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
if executable_permission:
|
| 120 |
-
try:
|
| 121 |
-
os.chmod(file_name, 0o755)
|
| 122 |
-
except Exception as e:
|
| 123 |
-
st.warning(f"Warning: Could not set executable permission for {file_name}. Reason: {e}")
|
| 124 |
-
|
| 125 |
-
return file_name, sh_code
|
| 126 |
|
| 127 |
def main():
|
| 128 |
-
st.title("
|
| 129 |
-
st.markdown("Generate advanced
|
| 130 |
st.markdown("---")
|
| 131 |
|
| 132 |
# Layout in columns (sidebar and main area)
|
|
@@ -143,53 +126,51 @@ def main():
|
|
| 143 |
max_tokens = st.number_input("π Max Tokens", min_value=128, max_value=8192, value=8192, step=128, help="Adjust the maximum size of the response.")
|
| 144 |
|
| 145 |
with st.expander("π Prompt Settings"):
|
| 146 |
-
prompt_presets = st.selectbox("π― Predefined Prompts", ["None", "
|
| 147 |
prompt_detail = st.selectbox("π§ Prompt Detail", ["More descriptive", "Default", "Concise"], index=1, help="Defines the level of detail of the prompt")
|
| 148 |
|
| 149 |
-
with st.expander("
|
| 150 |
-
|
| 151 |
-
"
|
| 152 |
[
|
| 153 |
-
|
| 154 |
-
"CentOS 8", "CentOS 7",
|
| 155 |
-
"RedHat Enterprise Linux 9", "RedHat Enterprise Linux 8",
|
| 156 |
-
"Debian 11", "Debian 10", "Debian 9", "Debian 8", "Debian 7", "Other"
|
| 157 |
],
|
| 158 |
-
index=0, help="Choose the target
|
| 159 |
)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
log_level = st.selectbox("ποΈ Logging Level", ["Detailed", "Default", "Minimum"], index=1, help="Defines the detail level of logs.")
|
| 164 |
-
|
| 165 |
-
detail_level = st.selectbox("Detail Level", ["More detailed", "Default", "More concise"], index=1)
|
| 166 |
-
script_type = st.selectbox("Script Type", ["More automatic", "More interactive"], index=0)
|
| 167 |
security_level = st.radio("Security Level", ["High", "Medium", "Low"], index=1)
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
custom_command_requirements = st.text_input("Custom Command Requirements", placeholder="Ex: Use specific tools, parameters", help="Add specific requirements for the generated command.")
|
| 171 |
-
command_specific_details = st.text_input("Command Specific Details", placeholder="Ex: Specific configurations, edge cases", help="Add specific details for command generation.")
|
| 172 |
|
| 173 |
|
| 174 |
with col2:
|
| 175 |
-
|
| 176 |
-
prompt_base = st.text_input("Describe the
|
| 177 |
|
| 178 |
if prompt_presets != "None":
|
| 179 |
if prompt_base:
|
| 180 |
prompt_base = f"{prompt_presets} , {prompt_base}"
|
| 181 |
else:
|
| 182 |
prompt_base = prompt_presets;
|
| 183 |
-
if st.button("β¨ Generate
|
| 184 |
if not prompt_base:
|
| 185 |
-
st.error("β οΈ Please enter a
|
| 186 |
return
|
| 187 |
|
| 188 |
-
with st.spinner("β³ Generating
|
| 189 |
-
ai_code =
|
| 190 |
prompt_base,
|
| 191 |
detail_level,
|
| 192 |
-
|
| 193 |
security_level,
|
| 194 |
model_name,
|
| 195 |
temperature,
|
|
@@ -199,29 +180,27 @@ def main():
|
|
| 199 |
prompt_detail,
|
| 200 |
encoding,
|
| 201 |
add_header,
|
| 202 |
-
add_error_handling,
|
| 203 |
log_level,
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
command_specific_details
|
| 208 |
)
|
| 209 |
|
| 210 |
if ai_code:
|
| 211 |
-
st.markdown("### β
Generated
|
| 212 |
-
st.code(ai_code, language="
|
| 213 |
|
| 214 |
short_title = prompt_base[:30].strip().replace(" ", "_").lower()
|
| 215 |
-
|
| 216 |
|
| 217 |
st.download_button(
|
| 218 |
-
label="β¬οΈ Download
|
| 219 |
-
data=
|
| 220 |
-
file_name=
|
| 221 |
-
mime="application/x-
|
| 222 |
)
|
| 223 |
else:
|
| 224 |
-
st.error("β Error generating the
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
main()
|
|
|
|
| 9 |
API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 10 |
|
| 11 |
# Page configuration
|
| 12 |
+
st.set_page_config(page_title="βοΈ Gemini2 Terraform Gen Pro", page_icon="βοΈ", layout="wide")
|
| 13 |
|
| 14 |
# --- Helper Functions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def send_message_to_model(message, model_name, temperature, top_p, top_k, max_tokens):
|
| 16 |
"""Sends a message to the AI model and returns the response."""
|
| 17 |
try:
|
|
|
|
| 34 |
st.error(f"β Error communicating with the AI: {e}")
|
| 35 |
return None
|
| 36 |
|
| 37 |
+
def generate_terraform_code(prompt_base, detail_level, provider, security_level, model_name, temperature, top_p, top_k, max_tokens, prompt_detail, encoding, add_header, log_level, infrastructure_type, custom_requirements, specific_details):
|
| 38 |
+
"""Generates a Terraform code based on user settings."""
|
| 39 |
prompt = f"""
|
| 40 |
+
You are an expert DevOps engineer specializing in Terraform. Your task is to generate a complete, secure, and efficient Terraform configuration based on the following description:
|
| 41 |
|
| 42 |
+
**Goal:** Create the most complete, detailed, efficient, and secure Terraform configuration possible, considering all variables, edge cases, and potential scenarios.
|
| 43 |
|
| 44 |
+
**Configuration Description:** {prompt_base}
|
| 45 |
|
| 46 |
**Detail Level:** {detail_level}
|
|
|
|
| 47 |
**Security Level:** {security_level}
|
| 48 |
**Prompt Detail Level**:{prompt_detail}
|
| 49 |
+
**Infrastructure Type:** {infrastructure_type}
|
| 50 |
+
**Provider:** {provider}
|
| 51 |
+
**Custom Requirements:** {custom_requirements if custom_requirements else "None"}
|
| 52 |
+
**Specific Details:** {specific_details if specific_details else "None"}
|
| 53 |
|
| 54 |
**Response Format:**
|
| 55 |
+
- Respond in Markdown format, including a HCL code block with its original formatting, without line breaks.
|
| 56 |
+
- The HCL code block must be delimited by ```terraform and ```.
|
| 57 |
- Do not include comments, explanations, or any other text outside the code block.
|
| 58 |
+
- The code must maintain its full vertical formatting, respecting indentation and line breaks.
|
| 59 |
- The code must be realistic, using real-world examples, data, and situations.
|
| 60 |
- Explore different approaches, techniques, and advanced practices, always prioritizing security and efficiency.
|
| 61 |
+
- Use advanced Terraform resources such as modules, loops, variables, functions and provisioners when necessary.
|
| 62 |
+
- Unless the user specifies otherwise, use the most current and secure versions of the resources, using terraform and following best practices.
|
| 63 |
+
- If the description asks to create a file, the terraform code should create the file directly in the file system and not use a screen output for this.
|
| 64 |
+
- If the description asks to read a file, the terraform code should read the file directly from the file system and not use a screen input for this.
|
| 65 |
+
- Use incremental reasoning to add improvements, expansions, and considerations to your code.
|
| 66 |
+
- Use the history of the conversations so that the response is incremental.
|
| 67 |
|
| 68 |
**Log Level**:{log_level}
|
|
|
|
| 69 |
|
| 70 |
**Important:**
|
| 71 |
+
- Generate only one configuration at a time.
|
| 72 |
- Create the longest, most complete, and detailed code possible to cover a wide range of possibilities and scenarios.
|
| 73 |
+
- Consider all the details of the request, expanding the response and improving the configuration.
|
| 74 |
+
- Use contextual information (such as the provider and infrastructure type) to generate the code.
|
| 75 |
- If possible, use incremental reasoning to add improvements, expansions, and considerations to your code.
|
|
|
|
| 76 |
- If the prompt asks to manage a service or process, consider different init systems (systemd, init, etc.).
|
| 77 |
+
- If the user asks to create something using modules, use official modules or public repositories, and if it is not possible, create a module with all parameters that it can have.
|
| 78 |
+
- If the description includes networking, create a secure and complete network infrastructure with all the necessary parameters, avoiding defaults.
|
| 79 |
+
- If the description includes storage, create secure and complete storage configurations, using all the available options from the provider.
|
| 80 |
+
- If the description includes compute resources, create the most secure and complete setup using the provider options.
|
| 81 |
|
| 82 |
"""
|
| 83 |
response = send_message_to_model(prompt, model_name, temperature, top_p, top_k, max_tokens)
|
| 84 |
return response
|
| 85 |
|
| 86 |
+
def parse_and_save_tf(ai_code, short_title, encoding, add_header):
|
| 87 |
+
"""Parses the markdown and saves the terraform code as .tf."""
|
| 88 |
+
match = re.search(r'```terraform\s*(.*?)\s*```', ai_code, re.DOTALL | re.IGNORECASE)
|
| 89 |
if match:
|
| 90 |
+
tf_code = match.group(1).strip()
|
| 91 |
else:
|
| 92 |
+
tf_code = ai_code.strip()
|
| 93 |
|
| 94 |
+
file_name = f"terraform_{short_title}.tf"
|
| 95 |
|
| 96 |
if add_header:
|
| 97 |
current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 98 |
+
header = f"""#===============================================================================
|
| 99 |
+
# Terraform Configuration Generated by Google Gemini 2 Terraform Gen Pro
|
|
|
|
| 100 |
# Date: {current_date}
|
| 101 |
# Author: Elias Andrade AKA Chaos4455
|
| 102 |
#===============================================================================\n"""
|
| 103 |
+
tf_code = header + tf_code
|
|
|
|
| 104 |
|
| 105 |
with open(file_name, "w", encoding=encoding) as f:
|
| 106 |
+
f.write(tf_code)
|
| 107 |
+
|
| 108 |
+
return file_name, tf_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def main():
|
| 111 |
+
st.title("βοΈ Gemini2 Terraform Gen Pro by [Elias Andrade](https://github.com/chaos4455)")
|
| 112 |
+
st.markdown("Generate advanced Terraform configurations with ease! π")
|
| 113 |
st.markdown("---")
|
| 114 |
|
| 115 |
# Layout in columns (sidebar and main area)
|
|
|
|
| 126 |
max_tokens = st.number_input("π Max Tokens", min_value=128, max_value=8192, value=8192, step=128, help="Adjust the maximum size of the response.")
|
| 127 |
|
| 128 |
with st.expander("π Prompt Settings"):
|
| 129 |
+
prompt_presets = st.selectbox("π― Predefined Prompts", ["None", "Create a VPC", "Create an EC2 instance", "Create a Kubernetes cluster", "Create a Docker container", "Create a storage account", "Create a virtual machine"], index=0, help="Choose a predefined prompt.")
|
| 130 |
prompt_detail = st.selectbox("π§ Prompt Detail", ["More descriptive", "Default", "Concise"], index=1, help="Defines the level of detail of the prompt")
|
| 131 |
|
| 132 |
+
with st.expander("βοΈ Terraform Settings"):
|
| 133 |
+
provider = st.selectbox(
|
| 134 |
+
"βοΈ Provider",
|
| 135 |
[
|
| 136 |
+
"aws", "azurerm", "google", "kubernetes", "docker", "vmware"
|
|
|
|
|
|
|
|
|
|
| 137 |
],
|
| 138 |
+
index=0, help="Choose the target provider."
|
| 139 |
)
|
| 140 |
+
infrastructure_type = st.selectbox(
|
| 141 |
+
"ποΈ Infrastructure Type",
|
| 142 |
+
[
|
| 143 |
+
"Networking", "Compute", "Storage", "Database", "Containers", "Virtual Machines", "Functions", "Other"
|
| 144 |
+
],
|
| 145 |
+
index=0, help="Choose the infrastructure type."
|
| 146 |
+
)
|
| 147 |
+
encoding = st.selectbox("π€ Encoding", ["utf-8", "ansi"], index=0, help="Choose the encoding of the .tf file.")
|
| 148 |
+
add_header = st.checkbox("π Add Header", value=True, help="Add a header with information in the .tf file.")
|
| 149 |
log_level = st.selectbox("ποΈ Logging Level", ["Detailed", "Default", "Minimum"], index=1, help="Defines the detail level of logs.")
|
|
|
|
|
|
|
|
|
|
| 150 |
security_level = st.radio("Security Level", ["High", "Medium", "Low"], index=1)
|
| 151 |
+
custom_requirements = st.text_input("Custom Requirements", placeholder="Ex: Use specific modules, parameters", help="Add specific requirements for the generated configuration.")
|
| 152 |
+
specific_details = st.text_input("Specific Details", placeholder="Ex: Specific configurations, edge cases", help="Add specific details for configuration generation.")
|
|
|
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
with col2:
|
| 156 |
+
# User's base prompt
|
| 157 |
+
prompt_base = st.text_input("Describe the Terraform Configuration:", placeholder="Ex: Create a VPC with public and private subnets in AWS", key="prompt_base")
|
| 158 |
|
| 159 |
if prompt_presets != "None":
|
| 160 |
if prompt_base:
|
| 161 |
prompt_base = f"{prompt_presets} , {prompt_base}"
|
| 162 |
else:
|
| 163 |
prompt_base = prompt_presets;
|
| 164 |
+
if st.button("β¨ Generate Terraform Configuration"):
|
| 165 |
if not prompt_base:
|
| 166 |
+
st.error("β οΈ Please enter a configuration description.")
|
| 167 |
return
|
| 168 |
|
| 169 |
+
with st.spinner("β³ Generating configuration..."):
|
| 170 |
+
ai_code = generate_terraform_code(
|
| 171 |
prompt_base,
|
| 172 |
detail_level,
|
| 173 |
+
provider,
|
| 174 |
security_level,
|
| 175 |
model_name,
|
| 176 |
temperature,
|
|
|
|
| 180 |
prompt_detail,
|
| 181 |
encoding,
|
| 182 |
add_header,
|
|
|
|
| 183 |
log_level,
|
| 184 |
+
infrastructure_type,
|
| 185 |
+
custom_requirements,
|
| 186 |
+
specific_details
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
if ai_code:
|
| 190 |
+
st.markdown("### β
Generated Configuration:")
|
| 191 |
+
st.code(ai_code, language="terraform")
|
| 192 |
|
| 193 |
short_title = prompt_base[:30].strip().replace(" ", "_").lower()
|
| 194 |
+
file_name_tf, tf_code = parse_and_save_tf(ai_code, short_title, encoding, add_header)
|
| 195 |
|
| 196 |
st.download_button(
|
| 197 |
+
label="β¬οΈ Download Configuration (.tf)",
|
| 198 |
+
data=tf_code,
|
| 199 |
+
file_name=file_name_tf,
|
| 200 |
+
mime="application/x-tf",
|
| 201 |
)
|
| 202 |
else:
|
| 203 |
+
st.error("β Error generating the configuration. Check the connection with the AI and try again.")
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
| 206 |
main()
|