aayushpuri01 commited on
Commit
7df9b2b
·
verified ·
1 Parent(s): c694feb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -41
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
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
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
- response += token
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
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
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