Tonic commited on
Commit
f7bc353
1 Parent(s): a8156ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ import transformers
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ import os
7
+
8
+ title = """# Welcome to 🌟Tonic's🐇🥷🏻Neo
9
+ WhiteRabbit🐇🥷🏻Neo is a model series that can be used for offensive and defensive cybersecurity. You can build with this endpoint using🐇🥷🏻Neo available here : [WhiteRabbitNeo/WhiteRabbitNeo-33B-v1.5](https://huggingface.co/WhiteRabbitNeo/WhiteRabbitNeo-33B-v1.5). You can also use 🐇🥷🏻Neo by cloning this space. Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/neo?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
10
+ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) Math 🔍 [introspector](https://huggingface.co/introspector) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [SciTonic](https://github.com/Tonic-AI/scitonic)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
11
+ """
12
+
13
+
14
+ default_system_prompt = """SYSTEM: You are an AI that code. Answer with code."""
15
+
16
+ model_path = "WhiteRabbitNeo/WhiteRabbitNeo-33B-v1.5"
17
+
18
+ quantization_config = BitsAndBytesConfig(
19
+ load_in_4bit=True,
20
+ bnb_4bit_use_double_quant=True,
21
+ bnb_4bit_compute_dtype=torch.bfloat16
22
+ )
23
+
24
+ hf_token = os.getenv("HF_TOKEN")
25
+ if not hf_token:
26
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
27
+
28
+ model = AutoModelForCausalLM.from_pretrained(
29
+ model_path,
30
+ device_map="auto",
31
+ trust_remote_code=True,
32
+ quantization_config=quantization_config,
33
+ token=hf_token,
34
+ )
35
+
36
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
37
+
38
+ @spaces.GPU
39
+ def generate_text(custom_prompt, user_input, temperature, generate_len, top_p, top_k):
40
+ system_prompt = custom_prompt if custom_prompt else default_system_prompt
41
+ llm_prompt = f"{system_prompt} \nUSER: {user_input} \nASSISTANT: "
42
+
43
+ tokens = tokenizer.encode(llm_prompt, return_tensors="pt")
44
+ tokens = tokens.to("cuda")
45
+
46
+ length = tokens.shape[1]
47
+ with torch.no_grad():
48
+ output = model.generate(
49
+ input_ids=tokens,
50
+ max_length=length + generate_len,
51
+ temperature=temperature,
52
+ top_p=top_p,
53
+ top_k=top_k,
54
+ num_return_sequences=1,
55
+ )
56
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
57
+ answer = generated_text[len(llm_prompt):].strip()
58
+
59
+ return answer
60
+
61
+ def gradio_app():
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown(title)
64
+ with gr.Row():
65
+ custom_prompt = gr.Textbox(label="🐇🥷🏻NeoCustom System Prompt (optional)", placeholder="Leave blank to use the default prompt...")
66
+ instruction = gr.Textbox(label="Your Instruction", placeholder="Type your question here...")
67
+ with gr.Row():
68
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.5, label="Temperature")
69
+ generate_len = gr.Slider(minimum=100, maximum=250, step=1, value=100, label="Generate Length")
70
+ top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=1.0, label="Top P")
71
+ top_k = gr.Slider(minimum=0, maximum=100, step=1, value=50, label="Top K")
72
+ with gr.Row():
73
+ generate_btn = gr.Button("Generate")
74
+ output = gr.Code(label="🐇🥷🏻Neo:", lines=10)
75
+
76
+ generate_btn.click(
77
+ fn=generate_text,
78
+ inputs=[custom_prompt, instruction, temperature, generate_len, top_p, top_k],
79
+ outputs=output
80
+ )
81
+
82
+ demo.launch()
83
+
84
+ if __name__ == "__main__":
85
+ gradio_app()