ybelkada commited on
Commit
d67d5cf
1 Parent(s): 8517635

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
+ import gradio as gr
5
+ from threading import Thread
6
+
7
+ MODEL = "tiiuae/falcon3-7b-1.58bit"
8
+
9
+ TITLE = "<h1><center>Falcon3-1.58 bit playground</center></h1>"
10
+ SUB_TITLE = """<center>This interface has been created for quick validation purposes, do not use it for production. Bear also in mind the model is a pretrained model.</center>"""
11
+
12
+ CSS = """
13
+ .duplicate-button {
14
+ margin: auto !important;
15
+ color: white !important;
16
+ background: black !important;
17
+ border-radius: 100vh !important;
18
+ }
19
+ h3 {
20
+ text-align: center;
21
+ }
22
+ """
23
+
24
+ END_MESSAGE = """
25
+ \n
26
+ **The conversation has reached to its end, please press "Clear" to restart a new conversation**
27
+ """
28
+
29
+ device = "cuda" # for GPU usage or "cpu" for CPU usage
30
+
31
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
32
+ model = AutoModelForCausalLM.from_pretrained(
33
+ MODEL,
34
+ torch_dtype=torch.bfloat16,
35
+ ).to(device)
36
+
37
+ if device == "cuda":
38
+ model = torch.compile(model)
39
+
40
+
41
+ def stream_chat(
42
+ message: str,
43
+ history: list,
44
+ temperature: float = 0.3,
45
+ max_new_tokens: int = 128,
46
+ top_p: float = 1.0,
47
+ top_k: int = 20,
48
+ penalty: float = 1.2,
49
+ ):
50
+ print(f'message: {message}')
51
+ inputs = tokenizer.encode(message, return_tensors="pt").to(device)
52
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
53
+
54
+ generate_kwargs = dict(
55
+ input_ids=inputs,
56
+ max_new_tokens = max_new_tokens,
57
+ do_sample = False if temperature == 0 else True,
58
+ top_p = top_p,
59
+ top_k = top_k,
60
+ temperature = temperature,
61
+ streamer=streamer,
62
+ pad_token_id = 10,
63
+ )
64
+
65
+ with torch.no_grad():
66
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
67
+ thread.start()
68
+
69
+ buffer = ""
70
+ for new_text in streamer:
71
+ buffer += new_text
72
+ yield buffer
73
+
74
+
75
+ print(f'response: {buffer}')
76
+
77
+ chatbot = gr.Chatbot(height=600)
78
+
79
+ with gr.Blocks(css=CSS, theme="soft") as demo:
80
+ gr.HTML(TITLE)
81
+ gr.HTML(SUB_TITLE)
82
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
83
+ gr.ChatInterface(
84
+ fn=stream_chat,
85
+ chatbot=chatbot,
86
+ fill_height=True,
87
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
88
+ additional_inputs=[
89
+ gr.Slider(
90
+ minimum=0,
91
+ maximum=1,
92
+ step=0.1,
93
+ value=0.3,
94
+ label="Temperature",
95
+ render=False,
96
+ ),
97
+ gr.Slider(
98
+ minimum=20,
99
+ maximum=256,
100
+ step=1,
101
+ value=128,
102
+ label="Max new tokens",
103
+ render=False,
104
+ ),
105
+ gr.Slider(
106
+ minimum=0.0,
107
+ maximum=1.0,
108
+ step=0.1,
109
+ value=1.0,
110
+ label="top_p",
111
+ render=False,
112
+ ),
113
+ gr.Slider(
114
+ minimum=1,
115
+ maximum=20,
116
+ step=1,
117
+ value=20,
118
+ label="top_k",
119
+ render=False,
120
+ ),
121
+ gr.Slider(
122
+ minimum=0.0,
123
+ maximum=2.0,
124
+ step=0.1,
125
+ value=1.2,
126
+ label="Repetition penalty",
127
+ render=False,
128
+ ),
129
+ ],
130
+ examples=[
131
+ ["Hello there, can you suggest few places to visit in UAE?"],
132
+ ["What UAE is known for?"],
133
+ ],
134
+ cache_examples=False,
135
+ )
136
+
137
+
138
+ if __name__ == "__main__":
139
+ demo.launch()