felya97 commited on
Commit
9699c41
·
verified ·
1 Parent(s): 7f36eb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4
+
5
+ model_id = "TildeAI/TildeOpen-30b"
6
+
7
+ # Load tokenizer
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
9
+
10
+ # Load model in 4-bit quantization
11
+ bnb_config = BitsAndBytesConfig(
12
+ load_in_4bit=True,
13
+ bnb_4bit_compute_dtype=torch.bfloat16,
14
+ bnb_4bit_use_double_quant=True,
15
+ bnb_4bit_quant_type="nf4",
16
+ )
17
+
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ model_id,
20
+ quantization_config=bnb_config,
21
+ device_map="auto",
22
+ torch_dtype=torch.bfloat16
23
+ )
24
+
25
+ def chat(message, max_new_tokens=256):
26
+ inputs = tokenizer(message, return_tensors="pt").to(model.device)
27
+ outputs = model.generate(
28
+ **inputs,
29
+ max_new_tokens=max_new_tokens,
30
+ repetition_penalty=1.2,
31
+ do_sample=True,
32
+ temperature=0.7,
33
+ top_p=0.9,
34
+ )
35
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
36
+
37
+ # Gradio UI
38
+ demo = gr.Interface(
39
+ fn=chat,
40
+ inputs=[gr.Textbox(label="Ask something"), gr.Slider(50, 1024, 256)],
41
+ outputs="text",
42
+ title="TildeOpen-30b Chat (HF Space)"
43
+ )
44
+
45
+ if __name__ == "__main__":
46
+ demo.launch()