Pic2Terraform / app.py
yasserrmd's picture
Update app.py
070e4f1 verified
from flask import Flask, render_template, request, jsonify, send_file
from groq import Groq
import os
import base64
import markdown
app = Flask(__name__)
client = Groq()
# Home route - renders HTML page
@app.route('/')
def index():
return render_template('index.html')
# API route to handle image upload and Groq processing
@app.route('/generate', methods=['POST'])
def generate_yaml():
image = request.files['image']
if image:
# Convert image to base64 without saving the file
base64_image = base64.b64encode(image.read()).decode('utf-8')
# Image description using Groq API with base64-encoded image
description_completion = client.chat.completions.create(
model="llama-3.2-11b-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Analyze the provided image of the cloud architecture diagram. Describe each component in detail, including types of services (compute, storage, networking), the connections between them, and how they interact. Identify key cloud resources like virtual machines, containers, serverless functions, databases, load balancers, security services, and monitoring tools. Highlight any redundancy mechanisms, multi-region setups, security implementations (firewalls, IAM roles, encryption), and traffic routing mechanisms like CDN or API gateways. Also, explain how external systems, such as on-premises infrastructure or user endpoints, connect to the cloud environment."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
]
},
{"role": "assistant", "content": ""}
],
temperature=1,
max_tokens=1024,
top_p=1,
stream=False,
stop=None
)
description = description_completion.choices[0].message.content
print(description)
description=markdown.markdown(description)
# Generate Terraform YAML using Groq API
yaml_completion = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{"role": "system", "content": "Your task is to generate a Terraform configuration using YAML format. The configuration should define cloud infrastructure based on the provided description, including resources such as compute instances (virtual machines or containers), serverless functions, databases (SQL and NoSQL), load balancers, and networking components (VPCs, subnets, security groups). Ensure to include security configurations like IAM roles and policies, redundancy mechanisms like auto-scaling and availability zones, and any traffic routing services such as API gateways. Ensure the YAML output is correctly formatted and adheres strictly to Terraform's syntax conventions, with proper indentation and sections for each resource type. The output must contain only valid YAML code for Terraform and avoid any explanations or additional commentary."},
{"role": "user", "content": f"Create a Terraform YAML configuration using the following cloud architecture description. Ensure the YAML strictly follows Terraform syntax and includes resource definitions for compute instances (virtual machines or containers), serverless functions, load balancers, and databases (SQL and NoSQL). Include definitions for networking components like VPCs, subnets, and security groups with firewall rules. Also, define API gateways for traffic management, IAM roles and policies for security, and any redundancy mechanisms like availability zones and auto-scaling groups. Add configurations for storage, such as S3 or equivalent, and monitoring/logging tools. The Terraform YAML should be complete and structured according to best practices, with clear sections for each resource type and proper indentation. Avoid any unnecessary details and focus only on the infrastructure setup.: {description}"}
],
temperature=0.5,
max_tokens=4000,
top_p=1,
stream=False,
stop=None
)
yaml_output = yaml_completion.choices[0].message.content
print(yaml_output)
yaml_output=yaml_output.replace("```yml", "").replace("```", "").strip()
# Return both description and YAML output
return jsonify({
'description': description,
'yaml': yaml_output
})
if __name__ == '__main__':
app.run(debug=True)