ss900371tw commited on
Commit
5fcb72f
·
verified ·
1 Parent(s): 35ee0e7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +85 -1
src/streamlit_app.py CHANGED
@@ -1,3 +1,87 @@
 
1
  import gradio as gr
 
2
 
3
- gr.load("models/meta-llama/Meta-Llama-3-8B-Instruct").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
 
5
+ """
6
+ 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
7
+ """
8
+
9
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
+ client = InferenceClient(
11
+ "meta-llama/Meta-Llama-3-8B-Instruct",
12
+ token=os.environ.get("HF_TOKEN")
13
+ )
14
+
15
+
16
+ def respond(
17
+ message,
18
+ history: list[tuple[str, str]],
19
+ max_tokens,
20
+ temperature,
21
+ top_p,
22
+ ):
23
+
24
+ name = "Ernest"
25
+ system_message = f"""As a virtual mentor in cybersecurity called {name}, your role is to provide expert guidance and advice on protecting information and systems from cyber threats. You are an expert in:
26
+ 1) Information Security;
27
+ 2) Network Security;
28
+ 3) Application Security;
29
+ 4) Endpoint Security;
30
+ 5) Data Security;
31
+ 6) Identity and Access Management;
32
+ 7) Database and Infrastructure Security;
33
+ 8) Cloud Security;
34
+ 9) Disaster Recovery/Business Continuity Planning;
35
+ 10) Cyber Threat Intelligence;
36
+ 11) Legal, Regulations, Compliance, and Ethics;
37
+ 12) Operational Security (OpSec).
38
+ Your responses should be informed by current best practices in security protocols, risk management, and ethical hacking. Encourage a proactive security mindset, emphasizing the importance of continual learning, vigilance, and adaptation to new challenges in the cyber landscape. Offer clear, detailed explanations on complex topics such as network security, encryption, and compliance standards. Foster a responsible attitude towards data privacy and the ethical implications of cybersecurity measures. Your language should be precise and authoritative, suitable for educating both beginners and experienced professionals in the field."""
39
+
40
+ messages = [{"role": "system", "content": system_message}]
41
+
42
+ for val in history:
43
+ if val[0]:
44
+ messages.append({"role": "user", "content": val[0]})
45
+ if val[1]:
46
+ messages.append({"role": "assistant", "content": val[1]})
47
+
48
+ messages.append({"role": "user", "content": message})
49
+
50
+ response = ""
51
+
52
+ # Stream the model output safely
53
+ for msg in client.chat_completion(
54
+ messages,
55
+ max_tokens=max_tokens,
56
+ temperature=temperature,
57
+ top_p=top_p,
58
+ stream=True,
59
+ ):
60
+ if hasattr(msg, "choices") and msg.choices:
61
+ delta = msg.choices[0].delta
62
+ if hasattr(delta, "content") and delta.content:
63
+ response += delta.content
64
+ yield response
65
+ # Ignore any events that do not contain content
66
+
67
+ """
68
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
69
+ """
70
+ demo = gr.ChatInterface(
71
+ respond,
72
+ additional_inputs=[
73
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max new tokens"),
74
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
75
+ gr.Slider(
76
+ minimum=0.1,
77
+ maximum=1.0,
78
+ value=0.95,
79
+ step=0.05,
80
+ label="Top-p (nucleus sampling)",
81
+ ),
82
+ ],
83
+ )
84
+
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()