Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import re
|
|
| 6 |
# Configurações de chave secreta e API do Google Gemini
|
| 7 |
API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 8 |
|
| 9 |
-
|
| 10 |
# Estilos CSS para o Streamlit
|
| 11 |
st.markdown(
|
| 12 |
"""
|
|
@@ -14,61 +13,54 @@ st.markdown(
|
|
| 14 |
body {
|
| 15 |
background-color: #f0f2f6;
|
| 16 |
color: #262730;
|
| 17 |
-
font-family: 'Arial', sans-serif;
|
| 18 |
}
|
| 19 |
.stApp {
|
| 20 |
max-width: 1200px;
|
| 21 |
margin: 0 auto;
|
| 22 |
padding: 20px;
|
| 23 |
display: flex;
|
| 24 |
-
flex-wrap: wrap;
|
| 25 |
-
|
| 26 |
}
|
| 27 |
-
|
| 28 |
-
/* Barra Lateral */
|
| 29 |
.stSidebar {
|
| 30 |
background-color: #e9ecef;
|
| 31 |
padding: 20px;
|
| 32 |
border-radius: 10px;
|
| 33 |
margin-bottom: 20px;
|
| 34 |
-
width: 250px;
|
| 35 |
-
|
| 36 |
-
|
| 37 |
}
|
| 38 |
-
|
| 39 |
.stSidebar {
|
| 40 |
width: 100%;
|
| 41 |
margin-bottom: 10px;
|
| 42 |
-
|
| 43 |
}
|
| 44 |
}
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
}
|
| 53 |
.stTextInput>div>div>input {
|
| 54 |
-
|
| 55 |
border-radius: 8px;
|
| 56 |
padding: 12px;
|
| 57 |
border: 1px solid #ced4da;
|
| 58 |
font-size: 1rem;
|
| 59 |
}
|
| 60 |
-
|
| 61 |
border-radius: 8px;
|
| 62 |
padding: 10px;
|
| 63 |
border: 1px solid #ced4da;
|
| 64 |
font-size: 1rem;
|
| 65 |
margin-bottom: 10px;
|
| 66 |
-
|
| 67 |
}
|
| 68 |
.stRadio>div{
|
| 69 |
display:flex;
|
| 70 |
flex-direction: row;
|
| 71 |
-
|
| 72 |
}
|
| 73 |
.stRadio>div>div{
|
| 74 |
margin-right:15px
|
|
@@ -86,54 +78,52 @@ st.markdown(
|
|
| 86 |
.stButton>button:hover {
|
| 87 |
background-color: #0056b3;
|
| 88 |
}
|
| 89 |
-
|
| 90 |
-
|
| 91 |
color: white;
|
| 92 |
border: none;
|
| 93 |
border-radius: 8px;
|
| 94 |
padding: 12px 24px;
|
| 95 |
cursor: pointer;
|
| 96 |
transition: background-color 0.3s ease;
|
| 97 |
-
|
| 98 |
}
|
| 99 |
.stDownloadButton>button:hover {
|
| 100 |
background-color: #1e7e34;
|
| 101 |
}
|
| 102 |
-
|
| 103 |
.stAlert {
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
}
|
| 110 |
.stAlert.stAlert-danger {
|
| 111 |
color: #721c24;
|
| 112 |
background-color: #f8d7da;
|
| 113 |
border-color: #f5c6cb;
|
| 114 |
}
|
| 115 |
-
|
| 116 |
display: flex;
|
| 117 |
justify-content: center;
|
| 118 |
align-items: center;
|
| 119 |
margin-top: 20px;
|
| 120 |
}
|
| 121 |
.stSpinner div {
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
}
|
| 129 |
-
|
| 130 |
@keyframes spin {
|
| 131 |
0% { transform: rotate(0deg); }
|
| 132 |
100% { transform: rotate(360deg); }
|
| 133 |
}
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
overflow-x: auto;
|
| 137 |
background-color: #f5f5f5;
|
| 138 |
color: #333;
|
| 139 |
padding: 15px;
|
|
@@ -144,11 +134,9 @@ st.markdown(
|
|
| 144 |
unsafe_allow_html=True,
|
| 145 |
)
|
| 146 |
|
| 147 |
-
|
| 148 |
def enviar_mensagem_para_modelo(mensagem):
|
| 149 |
"""Envia uma mensagem para o modelo de IA e retorna a resposta."""
|
| 150 |
try:
|
| 151 |
-
|
| 152 |
resposta = MODELO.start_chat(history=[]).send_message(mensagem)
|
| 153 |
return resposta.text
|
| 154 |
except Exception as e:
|
|
@@ -167,8 +155,10 @@ def gerar_comando_powershell(prompt_base, nivel_detalhe, tipo_script, nivel_segu
|
|
| 167 |
**Nível de Segurança:** {nivel_seguranca}
|
| 168 |
|
| 169 |
**Formato da resposta:**
|
| 170 |
-
- Responda
|
| 171 |
-
-
|
|
|
|
|
|
|
| 172 |
- Se for gerar um comando com uma cadeia de comandos, não use ponto e virgula no final ou inicio.
|
| 173 |
- Se precisar use pipe "|" para encadear.
|
| 174 |
- Não use nenhum tipo de formatação especial no resultado, apenas o código.
|
|
@@ -184,21 +174,19 @@ def gerar_comando_powershell(prompt_base, nivel_detalhe, tipo_script, nivel_segu
|
|
| 184 |
return resposta
|
| 185 |
|
| 186 |
def parsear_e_salvar_ps1(codigo_ia, titulo_curto):
|
| 187 |
-
"""Parseia o
|
| 188 |
-
# Regex para encontrar blocos de código Powershell (ignorando case)
|
| 189 |
match = re.search(r'```powershell\s*(.*?)\s*```', codigo_ia, re.DOTALL | re.IGNORECASE)
|
| 190 |
if match:
|
| 191 |
codigo_ps1 = match.group(1).strip()
|
| 192 |
else:
|
| 193 |
-
codigo_ps1 = codigo_ia.strip()
|
| 194 |
-
|
| 195 |
nome_arquivo = f"comando_{titulo_curto}.ps1"
|
| 196 |
with open(nome_arquivo, "w", encoding="utf-8") as f:
|
| 197 |
f.write(codigo_ps1)
|
| 198 |
return nome_arquivo, codigo_ps1
|
| 199 |
|
| 200 |
def main():
|
| 201 |
-
|
| 202 |
# Configuração do modelo de IA
|
| 203 |
CONFIGURACAO_GERACAO = {
|
| 204 |
"temperature": 0.2,
|
|
@@ -207,24 +195,21 @@ def main():
|
|
| 207 |
"response_mime_type": "text/plain",
|
| 208 |
}
|
| 209 |
NOME_MODELO = "gemini-2.0-flash-exp"
|
| 210 |
-
|
| 211 |
-
#
|
| 212 |
st.set_page_config(page_title="Google Gemini 2 PowerShell Command Gen", page_icon="🤖", layout="wide")
|
| 213 |
|
| 214 |
-
|
| 215 |
# Configuração do modelo de IA
|
| 216 |
global MODELO
|
| 217 |
MODELO = genai.GenerativeModel(
|
| 218 |
-
|
| 219 |
-
|
| 220 |
)
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
st.title("🤖 Google Gemini 2 PowerShell Command Gen by [Elias Andrade](https://github.com/chaos4455)")
|
| 225 |
st.markdown("Crie comandos PowerShell de forma fácil e rápida!")
|
| 226 |
st.markdown("---")
|
| 227 |
-
|
| 228 |
# Layout em colunas (barra lateral e principal)
|
| 229 |
col1, col2 = st.columns([1, 3])
|
| 230 |
|
|
@@ -237,8 +222,8 @@ def main():
|
|
| 237 |
tipo_script = st.sidebar.selectbox("Tipo de Script", ["Mais automático", "Mais interativo"], index=0)
|
| 238 |
nivel_seguranca = st.sidebar.radio("Nível de Segurança", ["Alto", "Médio", "Baixo"], index=1)
|
| 239 |
with col2:
|
| 240 |
-
|
| 241 |
-
|
| 242 |
prompt_base = st.text_input("Descreva o Comando PowerShell:", placeholder="Ex: Liste todos os processos em execução", key="prompt_base")
|
| 243 |
|
| 244 |
if st.button("✨ Gerar Comando PowerShell"):
|
|
@@ -249,13 +234,12 @@ def main():
|
|
| 249 |
with st.spinner("⏳ Gerando comando..."):
|
| 250 |
codigo_ia = gerar_comando_powershell(prompt_base, nivel_detalhe, tipo_script, nivel_seguranca)
|
| 251 |
if codigo_ia:
|
| 252 |
-
|
| 253 |
st.markdown("### ✅ Comando Gerado:")
|
| 254 |
st.code(codigo_ia, language="powershell")
|
| 255 |
-
|
| 256 |
titulo_curto = prompt_base[:30].strip().replace(" ", "_").lower()
|
| 257 |
-
nome_arquivo_ps1, codigo_ps1
|
| 258 |
-
|
| 259 |
st.download_button(
|
| 260 |
label="⬇️ Baixar Comando (.ps1)",
|
| 261 |
data=codigo_ps1,
|
|
@@ -263,7 +247,7 @@ def main():
|
|
| 263 |
mime="application/powershell",
|
| 264 |
)
|
| 265 |
else:
|
| 266 |
-
|
| 267 |
-
|
| 268 |
if __name__ == "__main__":
|
| 269 |
main()
|
|
|
|
| 6 |
# Configurações de chave secreta e API do Google Gemini
|
| 7 |
API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 8 |
|
|
|
|
| 9 |
# Estilos CSS para o Streamlit
|
| 10 |
st.markdown(
|
| 11 |
"""
|
|
|
|
| 13 |
body {
|
| 14 |
background-color: #f0f2f6;
|
| 15 |
color: #262730;
|
| 16 |
+
font-family: 'Arial', sans-serif;
|
| 17 |
}
|
| 18 |
.stApp {
|
| 19 |
max-width: 1200px;
|
| 20 |
margin: 0 auto;
|
| 21 |
padding: 20px;
|
| 22 |
display: flex;
|
| 23 |
+
flex-wrap: wrap;
|
|
|
|
| 24 |
}
|
|
|
|
|
|
|
| 25 |
.stSidebar {
|
| 26 |
background-color: #e9ecef;
|
| 27 |
padding: 20px;
|
| 28 |
border-radius: 10px;
|
| 29 |
margin-bottom: 20px;
|
| 30 |
+
width: 250px;
|
| 31 |
+
min-width:250px;
|
|
|
|
| 32 |
}
|
| 33 |
+
@media (max-width: 768px) {
|
| 34 |
.stSidebar {
|
| 35 |
width: 100%;
|
| 36 |
margin-bottom: 10px;
|
| 37 |
+
min-width:auto;
|
| 38 |
}
|
| 39 |
}
|
| 40 |
+
.main{
|
| 41 |
+
flex: 1;
|
| 42 |
+
padding: 20px;
|
| 43 |
+
display: flex;
|
| 44 |
+
flex-direction:column;
|
| 45 |
+
}
|
|
|
|
|
|
|
| 46 |
.stTextInput>div>div>input {
|
| 47 |
+
width: 100%;
|
| 48 |
border-radius: 8px;
|
| 49 |
padding: 12px;
|
| 50 |
border: 1px solid #ced4da;
|
| 51 |
font-size: 1rem;
|
| 52 |
}
|
| 53 |
+
.stSelectbox>div>div>div {
|
| 54 |
border-radius: 8px;
|
| 55 |
padding: 10px;
|
| 56 |
border: 1px solid #ced4da;
|
| 57 |
font-size: 1rem;
|
| 58 |
margin-bottom: 10px;
|
|
|
|
| 59 |
}
|
| 60 |
.stRadio>div{
|
| 61 |
display:flex;
|
| 62 |
flex-direction: row;
|
| 63 |
+
margin-bottom: 10px;
|
| 64 |
}
|
| 65 |
.stRadio>div>div{
|
| 66 |
margin-right:15px
|
|
|
|
| 78 |
.stButton>button:hover {
|
| 79 |
background-color: #0056b3;
|
| 80 |
}
|
| 81 |
+
.stDownloadButton>button{
|
| 82 |
+
background-color: #28a745;
|
| 83 |
color: white;
|
| 84 |
border: none;
|
| 85 |
border-radius: 8px;
|
| 86 |
padding: 12px 24px;
|
| 87 |
cursor: pointer;
|
| 88 |
transition: background-color 0.3s ease;
|
| 89 |
+
font-size: 1rem;
|
| 90 |
}
|
| 91 |
.stDownloadButton>button:hover {
|
| 92 |
background-color: #1e7e34;
|
| 93 |
}
|
|
|
|
| 94 |
.stAlert {
|
| 95 |
+
padding: 15px;
|
| 96 |
+
margin-bottom: 20px;
|
| 97 |
+
border: 1px solid transparent;
|
| 98 |
+
border-radius: 4px;
|
| 99 |
+
font-size: 1rem;
|
| 100 |
}
|
| 101 |
.stAlert.stAlert-danger {
|
| 102 |
color: #721c24;
|
| 103 |
background-color: #f8d7da;
|
| 104 |
border-color: #f5c6cb;
|
| 105 |
}
|
| 106 |
+
.stSpinner {
|
| 107 |
display: flex;
|
| 108 |
justify-content: center;
|
| 109 |
align-items: center;
|
| 110 |
margin-top: 20px;
|
| 111 |
}
|
| 112 |
.stSpinner div {
|
| 113 |
+
border: 8px solid #f3f3f3;
|
| 114 |
+
border-top: 8px solid #3498db;
|
| 115 |
+
border-radius: 50%;
|
| 116 |
+
width: 60px;
|
| 117 |
+
height: 60px;
|
| 118 |
+
animation: spin 2s linear infinite;
|
| 119 |
}
|
|
|
|
| 120 |
@keyframes spin {
|
| 121 |
0% { transform: rotate(0deg); }
|
| 122 |
100% { transform: rotate(360deg); }
|
| 123 |
}
|
| 124 |
+
code.language-powershell {
|
| 125 |
+
display: block;
|
| 126 |
+
overflow-x: auto;
|
| 127 |
background-color: #f5f5f5;
|
| 128 |
color: #333;
|
| 129 |
padding: 15px;
|
|
|
|
| 134 |
unsafe_allow_html=True,
|
| 135 |
)
|
| 136 |
|
|
|
|
| 137 |
def enviar_mensagem_para_modelo(mensagem):
|
| 138 |
"""Envia uma mensagem para o modelo de IA e retorna a resposta."""
|
| 139 |
try:
|
|
|
|
| 140 |
resposta = MODELO.start_chat(history=[]).send_message(mensagem)
|
| 141 |
return resposta.text
|
| 142 |
except Exception as e:
|
|
|
|
| 155 |
**Nível de Segurança:** {nivel_seguranca}
|
| 156 |
|
| 157 |
**Formato da resposta:**
|
| 158 |
+
- Responda em formato Markdown, incluindo um bloco de código Powershell com a formatação original, sem quebrar linhas.
|
| 159 |
+
- O bloco de código Powershell deve estar delimitado por ```powershell e ``` .
|
| 160 |
+
- Não inclua comentários, explicações ou qualquer outro texto fora do bloco de código.
|
| 161 |
+
- O código Powershell deve manter a formatação vertical completa, respeitando indentação e quebras de linha.
|
| 162 |
- Se for gerar um comando com uma cadeia de comandos, não use ponto e virgula no final ou inicio.
|
| 163 |
- Se precisar use pipe "|" para encadear.
|
| 164 |
- Não use nenhum tipo de formatação especial no resultado, apenas o código.
|
|
|
|
| 174 |
return resposta
|
| 175 |
|
| 176 |
def parsear_e_salvar_ps1(codigo_ia, titulo_curto):
|
| 177 |
+
"""Parseia o markdown e salva o código Powershell como .ps1."""
|
|
|
|
| 178 |
match = re.search(r'```powershell\s*(.*?)\s*```', codigo_ia, re.DOTALL | re.IGNORECASE)
|
| 179 |
if match:
|
| 180 |
codigo_ps1 = match.group(1).strip()
|
| 181 |
else:
|
| 182 |
+
codigo_ps1 = codigo_ia.strip()
|
| 183 |
+
|
| 184 |
nome_arquivo = f"comando_{titulo_curto}.ps1"
|
| 185 |
with open(nome_arquivo, "w", encoding="utf-8") as f:
|
| 186 |
f.write(codigo_ps1)
|
| 187 |
return nome_arquivo, codigo_ps1
|
| 188 |
|
| 189 |
def main():
|
|
|
|
| 190 |
# Configuração do modelo de IA
|
| 191 |
CONFIGURACAO_GERACAO = {
|
| 192 |
"temperature": 0.2,
|
|
|
|
| 195 |
"response_mime_type": "text/plain",
|
| 196 |
}
|
| 197 |
NOME_MODELO = "gemini-2.0-flash-exp"
|
| 198 |
+
|
| 199 |
+
# Move st.set_page_config para o começo da função
|
| 200 |
st.set_page_config(page_title="Google Gemini 2 PowerShell Command Gen", page_icon="🤖", layout="wide")
|
| 201 |
|
|
|
|
| 202 |
# Configuração do modelo de IA
|
| 203 |
global MODELO
|
| 204 |
MODELO = genai.GenerativeModel(
|
| 205 |
+
model_name=NOME_MODELO,
|
| 206 |
+
generation_config=CONFIGURACAO_GERACAO,
|
| 207 |
)
|
| 208 |
|
|
|
|
|
|
|
| 209 |
st.title("🤖 Google Gemini 2 PowerShell Command Gen by [Elias Andrade](https://github.com/chaos4455)")
|
| 210 |
st.markdown("Crie comandos PowerShell de forma fácil e rápida!")
|
| 211 |
st.markdown("---")
|
| 212 |
+
|
| 213 |
# Layout em colunas (barra lateral e principal)
|
| 214 |
col1, col2 = st.columns([1, 3])
|
| 215 |
|
|
|
|
| 222 |
tipo_script = st.sidebar.selectbox("Tipo de Script", ["Mais automático", "Mais interativo"], index=0)
|
| 223 |
nivel_seguranca = st.sidebar.radio("Nível de Segurança", ["Alto", "Médio", "Baixo"], index=1)
|
| 224 |
with col2:
|
| 225 |
+
|
| 226 |
+
# Prompt base do usuário
|
| 227 |
prompt_base = st.text_input("Descreva o Comando PowerShell:", placeholder="Ex: Liste todos os processos em execução", key="prompt_base")
|
| 228 |
|
| 229 |
if st.button("✨ Gerar Comando PowerShell"):
|
|
|
|
| 234 |
with st.spinner("⏳ Gerando comando..."):
|
| 235 |
codigo_ia = gerar_comando_powershell(prompt_base, nivel_detalhe, tipo_script, nivel_seguranca)
|
| 236 |
if codigo_ia:
|
|
|
|
| 237 |
st.markdown("### ✅ Comando Gerado:")
|
| 238 |
st.code(codigo_ia, language="powershell")
|
| 239 |
+
|
| 240 |
titulo_curto = prompt_base[:30].strip().replace(" ", "_").lower()
|
| 241 |
+
nome_arquivo_ps1, codigo_ps1 = parsear_e_salvar_ps1(codigo_ia, titulo_curto)
|
| 242 |
+
|
| 243 |
st.download_button(
|
| 244 |
label="⬇️ Baixar Comando (.ps1)",
|
| 245 |
data=codigo_ps1,
|
|
|
|
| 247 |
mime="application/powershell",
|
| 248 |
)
|
| 249 |
else:
|
| 250 |
+
st.error("❌ Erro ao gerar o comando. Verifique a conexão com a IA e tente novamente.")
|
| 251 |
+
|
| 252 |
if __name__ == "__main__":
|
| 253 |
main()
|