quangho-dev commited on
Commit
90f195f
·
1 Parent(s): 28b7154

Add application file

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re
3
+
4
+
5
+ def predict_house_price(description, location, area):
6
+ FastLanguageModel.for_inference(model) # Enable native 2x faster inference
7
+ messages = [ # Change below!
8
+ {
9
+ "role": "user",
10
+ "content": f"Mô tả: {description}\nĐịa chỉ: {location}\nDiện tích: {area}",
11
+ },
12
+ ]
13
+ input_ids = tokenizer.apply_chat_template(
14
+ messages,
15
+ add_generation_prompt=True,
16
+ return_tensors="pt",
17
+ ).to("cuda")
18
+
19
+ # from transformers import TextStreamer
20
+ # text_streamer = TextStreamer(tokenizer, skip_prompt = True)
21
+ # _ = model.generate(input_ids, streamer = text_streamer, max_new_tokens = 128, pad_token_id = tokenizer.eos_token_id)
22
+ # return extract_price(_)
23
+ output = model.generate(
24
+ input_ids, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id
25
+ )
26
+
27
+ decoded = tokenizer.decode(output[0], skip_special_tokens=True)
28
+ return f"{extract_price(decoded)} tỷ"
29
+
30
+
31
+ def extract_price(decoded):
32
+ # match number like 6.950.000.000, or 6950000000, or 6.95B, etc.
33
+ price_pattern = r"[\d\.\,]+"
34
+ matches = re.findall(price_pattern, decoded)
35
+ last_item = matches[-1]
36
+ return last_item
37
+
38
+
39
+ from unsloth import FastLanguageModel
40
+ import torch
41
+
42
+ max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
43
+ dtype = (
44
+ None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
45
+ )
46
+ load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
47
+
48
+ # 4bit pre quantized models we support for 4x faster downloading + no OOMs.
49
+ fourbit_models = [
50
+ "unsloth/mistral-7b-v0.3-bnb-4bit", # New Mistral v3 2x faster!
51
+ "unsloth/mistral-7b-instruct-v0.3-bnb-4bit",
52
+ "unsloth/llama-3-8b-bnb-4bit", # Llama-3 15 trillion tokens model 2x faster!
53
+ "unsloth/llama-3-8b-Instruct-bnb-4bit",
54
+ "unsloth/llama-3-70b-bnb-4bit",
55
+ "unsloth/Phi-3-mini-4k-instruct", # Phi-3 2x faster!
56
+ "unsloth/Phi-3-medium-4k-instruct",
57
+ "unsloth/mistral-7b-bnb-4bit",
58
+ "unsloth/gemma-7b-bnb-4bit", # Gemma 2.2x faster!
59
+ ] # More models at https://huggingface.co/unsloth
60
+
61
+ model, tokenizer = FastLanguageModel.from_pretrained(
62
+ # model_name = "unsloth/llama-3-8b-bnb-4bit",
63
+ model_name="QuangHoDev/lora_model",
64
+ max_seq_length=max_seq_length,
65
+ dtype=dtype,
66
+ load_in_4bit=load_in_4bit,
67
+ # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
68
+ )
69
+
70
+ model = FastLanguageModel.get_peft_model(
71
+ model,
72
+ r=16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
73
+ target_modules=[
74
+ "q_proj",
75
+ "k_proj",
76
+ "v_proj",
77
+ "o_proj",
78
+ "gate_proj",
79
+ "up_proj",
80
+ "down_proj",
81
+ ],
82
+ lora_alpha=16,
83
+ lora_dropout=0, # Supports any, but = 0 is optimized
84
+ bias="none", # Supports any, but = "none" is optimized
85
+ # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
86
+ use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
87
+ random_state=3407,
88
+ use_rslora=False, # We support rank stabilized LoRA
89
+ loftq_config=None, # And LoftQ
90
+ )
91
+
92
+ from datasets import load_dataset
93
+
94
+ dataset = load_dataset("QuangHoDev/house-prices-info", split="train")
95
+
96
+ from unsloth import to_sharegpt
97
+
98
+ dataset = to_sharegpt(
99
+ dataset,
100
+ merged_prompt="Tiêu đề: {title}\nMô tả: {description}\nĐịa chỉ: {location}\nDiện tích: {area}",
101
+ output_column_name="price",
102
+ conversation_extension=3, # Select more to handle longer conversations
103
+ )
104
+
105
+ from unsloth import standardize_sharegpt
106
+
107
+ dataset = standardize_sharegpt(dataset)
108
+
109
+ chat_template = """Dưới đây là thông tin về các bất động sản và giá của chúng.
110
+ Hãy đoán giá bất động sản theo mô tả.
111
+
112
+ >>> Mô tả bất động sản:
113
+ {INPUT}
114
+
115
+ >>> Giá là:
116
+ {OUTPUT}"""
117
+
118
+ from unsloth import apply_chat_template
119
+
120
+ dataset = apply_chat_template(
121
+ dataset,
122
+ tokenizer=tokenizer,
123
+ chat_template=chat_template,
124
+ # default_system_message = "You are a helpful assistant", << [OPTIONAL]
125
+ )
126
+
127
+ with gr.Blocks() as demo:
128
+ description = gr.Textbox(label="Mô tả nhà", lines=4)
129
+ location = gr.Textbox(label="Địa chỉ", lines=2)
130
+ area = gr.Number(label="Diện tích (m²)")
131
+ output = gr.Textbox(label="Giá nhà")
132
+ greet_btn = gr.Button("Đoán giá", variant="primary")
133
+ greet_btn.click(
134
+ fn=predict_house_price,
135
+ inputs=[description, location, area],
136
+ outputs=output,
137
+ api_name="greet",
138
+ )
139
+
140
+ gr.Examples(
141
+ examples=[
142
+ [
143
+ "Nhanh tay sở hữu căn nhà riêng (căn góc) 3 tầng tại Phường 10, Gò Vấp với thiết kế 3 phòng ngủ + 4toilet/PT + bếp...",
144
+ "261/, Đường Quang Trung, Phường 10, Quận Gò Vấp",
145
+ 40,
146
+ ],
147
+ ],
148
+ inputs=[description, location, area],
149
+ label="Ví dụ mẫu",
150
+ )
151
+
152
+ demo.launch()