Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,54 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
model_id = "eduard76/Llama3-8b-good-new"
|
| 6 |
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 8 |
-
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
-
device_map="auto",
|
| 12 |
-
torch_dtype=torch.float16,
|
| 13 |
trust_remote_code=True
|
| 14 |
)
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
covered_topics = {
|
| 19 |
"ospf", "bgp", "eigrp", "vxlan", "evpn", "network design", "acl", "routing",
|
| 20 |
"spine", "leaf", "underlay", "overlay", "mpls", "qos", "firewall",
|
| 21 |
"vpn", "vlan", "subnet", "cidr"
|
| 22 |
}
|
| 23 |
|
|
|
|
| 24 |
def chat(user_input):
|
| 25 |
-
prompt = f"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
iface.launch()
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
model_id = "eduard76/Llama3-8b-good-new"
|
| 6 |
|
| 7 |
+
# Load tokenizer and model
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
|
|
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
trust_remote_code=True
|
| 14 |
)
|
| 15 |
+
model.eval()
|
| 16 |
|
| 17 |
+
# Lista de topicuri acoperite
|
|
|
|
| 18 |
covered_topics = {
|
| 19 |
"ospf", "bgp", "eigrp", "vxlan", "evpn", "network design", "acl", "routing",
|
| 20 |
"spine", "leaf", "underlay", "overlay", "mpls", "qos", "firewall",
|
| 21 |
"vpn", "vlan", "subnet", "cidr"
|
| 22 |
}
|
| 23 |
|
| 24 |
+
# Funcția principală de chat
|
| 25 |
def chat(user_input):
|
| 26 |
+
prompt = f"""### Human: {user_input}\n### Assistant:"""
|
| 27 |
+
|
| 28 |
+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 29 |
+
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
output = model.generate(
|
| 32 |
+
**input_ids,
|
| 33 |
+
max_new_tokens=256,
|
| 34 |
+
do_sample=False,
|
| 35 |
+
temperature=0.0,
|
| 36 |
+
repetition_penalty=1.2,
|
| 37 |
+
no_repeat_ngram_size=5,
|
| 38 |
+
top_k=50,
|
| 39 |
+
top_p=0.9
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
+
# Scoate promptul inițial din răspuns
|
| 45 |
+
if "### Assistant:" in response:
|
| 46 |
+
response = response.split("### Assistant:")[-1].strip()
|
| 47 |
+
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
# Interfață Gradio
|
| 51 |
+
iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="Eduard's Virtual Architect – LLaMA3 Fine-Tuned")
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
iface.launch()
|