Spaces:
Sleeping
Sleeping
File size: 4,244 Bytes
6369021 2436096 31be601 6369021 b07117b 6369021 2436096 31be601 2436096 31be601 2436096 31be601 2436096 31be601 6369021 31be601 2436096 31be601 2436096 31be601 2436096 6369021 31be601 2436096 bf46e81 6369021 bf46e81 6369021 31be601 2436096 b07117b 2436096 31be601 6369021 57c3533 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import gradio as gr
from huggingface_hub import InferenceClient
#from transformers import pipeline
from huggingface_hub.inference._providers import PROVIDER_OR_POLICY_T
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
#hf_token: gr.OAuthToken,
):
"""
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
"""
#client = pipeline("text-generation", model="nosadaniel/llama3-1-8b-tuned")
#client = InferenceClient(token=hf_token.token, model="nosadaniel/llama3-1-8b-tuned")
model="meta-llama/Meta-Llama-3.1-8B-Instruct-LoRa:phishing-email-adJu"
base_url="https://api.tokenfactory.nebius.com/v1/"
api_key="v1.CmQKHHN0YXRpY2tleS1lMDBkMXh2ZDdheDAwNXhxMGgSIXNlcnZpY2VhY2NvdW50LWUwMGp0eHNrM3pubjdyYXQ0azIMCPHv7MgGEJ_k6PEBOgwI8PKElAcQwO2YywNAAloDZTAw.AAAAAAAAAAH-boLssQhDYJht_li9Ql7MN1rSmj_8DXmYlZ13NhdavV0NYylvY_HkVQrALXt2z9Pm5_aQn-tt--Mbc1W8G78E"
client = InferenceClient( base_url=base_url, api_key=api_key, provider=PROVIDER_OR_POLICY_T)
messages = [{"role": "system", "content": system_message}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(
model = model,
messages = messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
choices = message.choices
token = ""
if len(choices) and choices[0].delta.content:
token = choices[0].delta.content
response += token
yield response
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
system_prompt = """
# Parameters for email analysis
PARAMETER temperature 0.1
PARAMETER top_p 0.9
PARAMETER top_k 40
PARAMETER repeat_penalty 1.1
PARAMETER num_ctx 4096
# Enhanced system prompt for email phishing detection
SYSTEM
You are an advanced AI security analyst specialized in email threat detection. Analyze the provided email data and determine if it constitutes a phishing attempt.
Respond with exactly this JSON structure filled with real values (no backticks, no extra text):
""
{
"is_phishing": true or false,
"confidence_score": a float between 0.0 and 1.0,
"threat_type": "type of phishing attack",
"risk_level": "a number from 0 to 5",
"indicators": [
{
"category": "which part of the email is suspicious",
"finding": "concise finding",
"severity": "a number from 0 to 5",
"explanation": "short explanation referencing the email data"
}
],
"mitigation_recommendations": {
"immediate_actions": ["short actionable steps"],
"preventive_measures": ["short preventive steps"],
"reporting_guidance": "who/how to report if applicable"
},
"analysis_summary": "1-3 sentence summary of the assessment"
}
""
Only output the JSON object.
# Fallback model with enhanced prompting
# Base: Meta-Llama-3.1-8B-Instruct
"""
chatbot = gr.ChatInterface(
respond,
type="messages",
additional_inputs=[
gr.Textbox(value=system_prompt, label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
)
with gr.Blocks() as demo:
# with gr.Sidebar():
# gr.LoginButton()
chatbot.render()
if __name__ == "__main__":
demo.launch() |