Uploaded Main version with gradio.
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import transformers
import torch
import gradio as gr
import json
# Load the model once when the script starts
model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
# Load the model into memory (on GPU if available)
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto", # Auto-detect GPU
)
# Load the dataset from the local JSON file
with open("cisco_cli_commands.json", "r") as file:
cisco_commands = json.load(file)
# Function to search the dataset for a matching command
def search_dataset(user_input):
# Check if any command in the dataset matches the user input
for entry in cisco_commands:
if entry["command"] in user_input.lower(): # Match the command with user input (case-insensitive)
return f"**Command:** {entry['command']}\n\n**Description:** {entry['description']}"
return None # No match found
# Function to generate response using the dataset or fallback to the pipeline
def generate_response(user_input, chat_history):
# First, try to find a match in the dataset
dataset_response = search_dataset(user_input)
if dataset_response:
# Add user and assistant responses to the chat history
chat_history.append({"role": "user", "content": user_input})
chat_history.append({"role": "assistant", "content": dataset_response})
return chat_history
# If no match, fallback to the LLM
outputs = pipeline(user_input, max_new_tokens=256)
# Generate the assistant's response
assistant_response = outputs[0]["generated_text"]
# Add user and assistant responses to the chat history
chat_history.append({"role": "user", "content": user_input})
chat_history.append({"role": "assistant", "content": assistant_response})
return chat_history
# Create Gradio interface with chatbot and textbox
with gr.Blocks(theme=gr.themes.Ocean()) as iface:
gr.Markdown("<h1 style='text-align: center;'>Cisco Configuration Assistant</h1>")
chatbot = gr.Chatbot(label="Cisco Configuration Chatbot", type="messages")
user_input = gr.Textbox(placeholder="Enter your Cisco switch/router question here...", label="Your Input")
clear_btn = gr.Button("Clear")
def user(query, history):
# Generate a response and update the history
history = generate_response(query, history)
return history, "" # Return updated history and clear the input box
# Submit user input and update the chat history
user_input.submit(user, [user_input, chatbot], [chatbot, user_input])
clear_btn.click(lambda: [], None, chatbot, queue=False)
# Launch the Gradio app
iface.launch()Main&Gradio.pyAdded+70