Spaces:
Sleeping
Sleeping
Habib U Rehman commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Safely pull the API key from Hugging Face Space Settings -> Variables and secrets
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
def generate_network_script(user_goal, library, device_type):
|
| 9 |
+
# Check if the user forgot to add the API key in settings
|
| 10 |
+
if not GROQ_API_KEY:
|
| 11 |
+
return "# Error \nGROQ_API_KEY is missing. Please add it in your Space Settings -> Variables and secrets."
|
| 12 |
+
|
| 13 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
+
|
| 15 |
+
# System Prompt telling AI to act as an expert and output ONLY code
|
| 16 |
+
system_prompt = """You are a Senior Network Automation Architect.
|
| 17 |
+
Your task is to generate production-ready Python automation scripts.
|
| 18 |
+
Output ONLY the raw Python code. Do not include markdown formatting like ```python, and do not include any conversational text before or after the code."""
|
| 19 |
+
|
| 20 |
+
user_prompt = f"Write a Python script using the '{library}' library for '{device_type}' devices to accomplish the following: {user_goal}"
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
chat_completion = client.chat.completions.create(
|
| 24 |
+
messages=[
|
| 25 |
+
{"role": "system", "content": system_prompt},
|
| 26 |
+
{"role": "user", "content": user_prompt}
|
| 27 |
+
],
|
| 28 |
+
model="llama-3.3-70b-versatile",
|
| 29 |
+
temperature=0.1, # Low temperature for accurate coding
|
| 30 |
+
)
|
| 31 |
+
return chat_completion.choices[0].message.content
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"# Error \n{str(e)}"
|
| 34 |
+
|
| 35 |
+
# Custom CSS for a premium SaaS look
|
| 36 |
+
custom_css = """
|
| 37 |
+
.gradio-container {font-family: 'Inter', sans-serif;}
|
| 38 |
+
.header-box {text-align: center; padding: 20px; background: linear-gradient(90deg, #1e3a8a, #3b82f6); color: white; border-radius: 10px; margin-bottom: 20px;}
|
| 39 |
+
.header-box h1 {margin: 0; font-size: 28px; font-weight: bold; color: white !important;}
|
| 40 |
+
.header-box p {margin: 5px 0 0 0; opacity: 0.9;}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
# Build the Gradio UI
|
| 44 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"), css=custom_css) as app:
|
| 45 |
+
|
| 46 |
+
# Custom HTML Header
|
| 47 |
+
gr.HTML("""
|
| 48 |
+
<div class="header-box">
|
| 49 |
+
<h1>⚙️ AutoNet Architect</h1>
|
| 50 |
+
<p>AI-Powered Python Script Generator for Network Engineers</p>
|
| 51 |
+
</div>
|
| 52 |
+
""")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
# Left side inputs
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
with gr.Group():
|
| 58 |
+
library = gr.Dropdown(
|
| 59 |
+
choices=["Netmiko", "NAPALM", "Nornir"],
|
| 60 |
+
value="Netmiko",
|
| 61 |
+
label="1. Select Python Library",
|
| 62 |
+
info="Which automation framework do you want to use?"
|
| 63 |
+
)
|
| 64 |
+
device_type = gr.Dropdown(
|
| 65 |
+
choices=["cisco_ios", "cisco_nxos", "arista_eos", "juniper_junos"],
|
| 66 |
+
value="cisco_ios",
|
| 67 |
+
label="2. Select Device OS",
|
| 68 |
+
info="What type of devices are you connecting to?"
|
| 69 |
+
)
|
| 70 |
+
user_goal = gr.Textbox(
|
| 71 |
+
lines=5,
|
| 72 |
+
label="3. Describe the Task",
|
| 73 |
+
placeholder="e.g., Log into 50 switches, backup the running-config, and save them as text files with the hostname and date."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
generate_btn = gr.Button("✨ Generate Code", variant="primary", size="lg")
|
| 77 |
+
|
| 78 |
+
# Right side output
|
| 79 |
+
with gr.Column(scale=2):
|
| 80 |
+
output_code = gr.Code(
|
| 81 |
+
label="Production-Ready Python Script",
|
| 82 |
+
language="python",
|
| 83 |
+
interactive=False
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Connect UI to logic
|
| 87 |
+
generate_btn.click(
|
| 88 |
+
fn=generate_network_script,
|
| 89 |
+
inputs=[user_goal, library, device_type],
|
| 90 |
+
outputs=output_code
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Launch app (Hugging Face handles the web server routing automatically)
|
| 94 |
+
app.launch()
|