Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, login
|
| 3 |
+
from transformers import TextStreamer
|
| 4 |
+
import torch
|
| 5 |
+
from unsloth import FastLanguageModel
|
| 6 |
|
| 7 |
"""
|
| 8 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 9 |
"""
|
| 10 |
+
login()
|
| 11 |
|
| 12 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 13 |
+
model_name = "aayushpuri01/Llama-3.1-8B-Threat-Intelligent-v2",
|
| 14 |
+
max_seq_length = 2048,
|
| 15 |
+
device_map = "auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
FastLanguageModel.for_inference(model)
|
| 20 |
+
|
| 21 |
+
prompt_style = """You are a cybersecurity genius and expert threat hunter and analyst who can answer about any level of cybersecurity scenarios.
|
| 22 |
+
Based on the given Instruction and Input, generate appropriate Output.
|
| 23 |
|
| 24 |
+
### Instruction:
|
| 25 |
+
Please analyse the given scenario, provide diagnosis of the situation in between <diagnosis></diagnosis>. Write Solutions in between <solution></solution>.
|
| 26 |
+
|
| 27 |
+
### Input:
|
| 28 |
+
{}
|
| 29 |
+
|
| 30 |
+
### Output:
|
| 31 |
+
{}
|
| 32 |
+
"""
|
| 33 |
|
| 34 |
+
def generate_response(scenario):
|
| 35 |
+
formatted_prompt = prompt_style.format(scenario, "")
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
inputs = tokenizer(
|
| 38 |
+
[formatted_prompt],
|
| 39 |
+
return_tensors = "pt",
|
| 40 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 41 |
|
| 42 |
+
text_streamer = TextStreamer(tokenizer)
|
| 43 |
|
| 44 |
+
outputs = model.generate(**inputs, streamer=text_streamer, max_new_tokens=1028)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 47 |
|
| 48 |
+
output_start = response.find("### Output:") + len("### Output:\n")
|
| 49 |
+
output_text = response[output_start:].strip()
|
| 50 |
|
| 51 |
+
return output_text
|
| 52 |
"""
|
| 53 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 54 |
"""
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
+
fn = generate_response,
|
| 57 |
+
inputs=gr.Textbox(
|
| 58 |
+
label="Cyberthreat scenario",
|
| 59 |
+
placeholder="Enter a scene (e.g, 'Cryptowall 2.0 began using the Tor anonymity network...')",
|
| 60 |
+
lines=5),
|
| 61 |
+
outputs = gr.Markdown(label="Analysis and Solutions"),
|
| 62 |
+
title = "Threat Intelligence with Llama-3.1-8B",
|
| 63 |
+
description = "Enter a cybersecurity scenario to get a detailed analysis and solutions from a fine tuned LLM model",
|
| 64 |
+
theme = "huggingface"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
|