Tiitta commited on
Commit
368e317
·
verified ·
1 Parent(s): 28e27e2

Delete Main&Gradio.py

Browse files
Files changed (1) hide show
  1. Main&Gradio.py +0 -70
Main&Gradio.py DELETED
@@ -1,70 +0,0 @@
1
- import transformers
2
- import torch
3
- import gradio as gr
4
- import json
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
-
18
- # Load the dataset from the local JSON file
19
- with open("cisco_cli_commands.json", "r") as file:
20
- cisco_commands = json.load(file)
21
-
22
- # Function to search the dataset for a matching command
23
- def search_dataset(user_input):
24
- # Check if any command in the dataset matches the user input
25
- for entry in cisco_commands:
26
- if entry["command"] in user_input.lower(): # Match the command with user input (case-insensitive)
27
- return f"**Command:** {entry['command']}\n\n**Description:** {entry['description']}"
28
- return None # No match found
29
-
30
- # Function to generate response using the dataset or fallback to the pipeline
31
- def generate_response(user_input, chat_history):
32
- # First, try to find a match in the dataset
33
- dataset_response = search_dataset(user_input)
34
-
35
- if dataset_response:
36
- # Add user and assistant responses to the chat history
37
- chat_history.append({"role": "user", "content": user_input})
38
- chat_history.append({"role": "assistant", "content": dataset_response})
39
- return chat_history
40
-
41
- # If no match, fallback to the LLM
42
- outputs = pipeline(user_input, max_new_tokens=256)
43
-
44
- # Generate the assistant's response
45
- assistant_response = outputs[0]["generated_text"]
46
-
47
- # Add user and assistant responses to the chat history
48
- chat_history.append({"role": "user", "content": user_input})
49
- chat_history.append({"role": "assistant", "content": assistant_response})
50
-
51
- return chat_history
52
-
53
- # Create Gradio interface with chatbot and textbox
54
- with gr.Blocks(theme=gr.themes.Ocean()) as iface:
55
- gr.Markdown("<h1 style='text-align: center;'>Cisco Configuration Assistant</h1>")
56
- chatbot = gr.Chatbot(label="Cisco Configuration Chatbot", type="messages")
57
- user_input = gr.Textbox(placeholder="Enter your Cisco switch/router question here...", label="Your Input")
58
- clear_btn = gr.Button("Clear")
59
-
60
- def user(query, history):
61
- # Generate a response and update the history
62
- history = generate_response(query, history)
63
- return history, "" # Return updated history and clear the input box
64
- # Submit user input and update the chat history
65
- user_input.submit(user, [user_input, chatbot], [chatbot, user_input])
66
-
67
- clear_btn.click(lambda: [], None, chatbot, queue=False)
68
-
69
- # Launch the Gradio app
70
- iface.launch()