FraRy commited on
Commit
0d46e81
·
verified ·
1 Parent(s): f90532f

Upload Main.py

Browse files
Files changed (1) hide show
  1. main.py +58 -0
main.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ import torch
3
+ import gradio as gr
4
+ from datasets import load_dataset
5
+
6
+ # Load the model once when the script starts
7
+ model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
8
+
9
+ # Load the model into memory (on GPU if available)
10
+ pipeline = transformers.pipeline(
11
+ "text-generation",
12
+ model=model_id,
13
+ model_kwargs={"torch_dtype": torch.bfloat16},
14
+ device_map="auto", # Auto-detect GPU
15
+ )
16
+
17
+ # Load the dataset from Hugging Face
18
+ dataset = load_dataset("quantumminds/cisco_cli_commands")
19
+
20
+ # Function to search the dataset for a matching command
21
+ def search_dataset(user_input):
22
+ # Check if any command in the dataset matches the user input
23
+ for entry in dataset['train']: # assuming the dataset is in the 'train' split
24
+ if entry["command"].lower() in user_input.lower(): # Match the command with user input (case-insensitive)
25
+ return f"**Command:** {entry['command']}\n\n**Description:** {entry['description']}\n\n**Example:** {entry['examples'][0]['example_command'] if 'examples' in entry else 'No example available'}"
26
+ return None # No match found
27
+
28
+ # Function to generate response using the dataset or fallback to the pipeline
29
+ def generate_response(user_input):
30
+ # First, try to find a match in the dataset
31
+ dataset_response = search_dataset(user_input)
32
+
33
+ if dataset_response:
34
+ return dataset_response
35
+
36
+ # If no match, fallback to the LLM
37
+ messages = [
38
+ {"role": "system", "content": "You are a pirate chatbot who specializes in Cisco switch and router configurations"},
39
+ {"role": "user", "content": user_input},
40
+ ]
41
+
42
+ # Generate the response from the LLM
43
+ outputs = pipeline(messages, max_new_tokens=256)
44
+
45
+ # Return the generated text
46
+ return outputs[0]["generated_text"]
47
+
48
+ # Create Gradio interface
49
+ iface = gr.Interface(
50
+ fn=generate_response, # Function to call
51
+ inputs=gr.Textbox(lines=2, placeholder="Enter your Cisco switch/router question here..."), # Updated Textbox input
52
+ outputs="text", # Text output
53
+ title="Cisco Configuration Assistant", # Title for the UI
54
+ description="Ask the chatbot questions about Cisco switch/router configurations", # Description
55
+ )
56
+
57
+ # Launch the Gradio app
58
+ iface.launch()