saadkhi commited on
Commit
a663164
·
verified ·
1 Parent(s): 979ad48

Update app.py

Browse files

Updated to reduce delay in response generation.

Files changed (1) hide show
  1. app.py +85 -17
app.py CHANGED
@@ -1,3 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
@@ -9,32 +58,51 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
9
  base_model = "unsloth/Phi-3-mini-4k-instruct-bnb-4bit"
10
  finetuned_model = "saadkhi/SQL_Chat_finetuned_model"
11
 
12
- tokenizer = AutoTokenizer.from_pretrained(base_model)
13
 
14
- bnb = BitsAndBytesConfig(load_in_4bit=True)
15
 
16
  model = AutoModelForCausalLM.from_pretrained(
17
  base_model,
18
- quantization_config=bnb,
19
  torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
20
- device_map="auto"
 
21
  )
22
-
23
- model = PeftModel.from_pretrained(model, finetuned_model).to(device)
24
  model.eval()
25
 
26
- def chat(prompt):
27
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
28
-
 
 
 
 
 
 
 
 
29
  with torch.inference_mode():
30
- output = model.generate(
31
- **inputs,
32
- max_new_tokens=60,
33
- temperature=0.1,
34
- do_sample=False
 
 
35
  )
 
 
 
 
 
 
36
 
37
- return tokenizer.decode(output[0], skip_special_tokens=True)
 
 
 
 
38
 
39
- iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="SQL Chatbot")
40
- iface.launch()
 
1
+ # import torch
2
+ # import gradio as gr
3
+ # from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ # from peft import PeftModel
5
+ # from transformers import BitsAndBytesConfig
6
+
7
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ # base_model = "unsloth/Phi-3-mini-4k-instruct-bnb-4bit"
10
+ # finetuned_model = "saadkhi/SQL_Chat_finetuned_model"
11
+
12
+ # tokenizer = AutoTokenizer.from_pretrained(base_model)
13
+
14
+ # bnb = BitsAndBytesConfig(load_in_4bit=True)
15
+
16
+ # model = AutoModelForCausalLM.from_pretrained(
17
+ # base_model,
18
+ # quantization_config=bnb,
19
+ # torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
20
+ # device_map="auto"
21
+ # )
22
+
23
+ # model = PeftModel.from_pretrained(model, finetuned_model).to(device)
24
+ # model.eval()
25
+
26
+ # def chat(prompt):
27
+ # inputs = tokenizer(prompt, return_tensors="pt").to(device)
28
+
29
+ # with torch.inference_mode():
30
+ # output = model.generate(
31
+ # **inputs,
32
+ # max_new_tokens=60,
33
+ # temperature=0.1,
34
+ # do_sample=False
35
+ # )
36
+
37
+ # return tokenizer.decode(output[0], skip_special_tokens=True)
38
+
39
+ # iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="SQL Chatbot")
40
+ # iface.launch()
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
  import torch
51
  import gradio as gr
52
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
58
  base_model = "unsloth/Phi-3-mini-4k-instruct-bnb-4bit"
59
  finetuned_model = "saadkhi/SQL_Chat_finetuned_model"
60
 
61
+ tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
62
 
63
+ bnb_config = BitsAndBytesConfig(load_in_4bit=True)
64
 
65
  model = AutoModelForCausalLM.from_pretrained(
66
  base_model,
67
+ quantization_config=bnb_config,
68
  torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
69
+ device_map="auto",
70
+ trust_remote_code=True,
71
  )
72
+ model = PeftModel.from_pretrained(model, finetuned_model)
 
73
  model.eval()
74
 
75
+ def chat(user_prompt):
76
+ # Proper Phi-3 chat format
77
+ messages = [{"role": "user", "content": user_prompt}]
78
+
79
+ inputs = tokenizer.apply_chat_template(
80
+ messages,
81
+ tokenize=True,
82
+ add_generation_prompt=True,
83
+ return_tensors="pt"
84
+ ).to(device)
85
+
86
  with torch.inference_mode():
87
+ outputs = model.generate(
88
+ inputs,
89
+ max_new_tokens=256, # Increased a bit for full SQL
90
+ temperature=0.7,
91
+ do_sample=True, # Better for creativity, faster
92
+ top_p=0.9,
93
+ repetition_penalty=1.1,
94
  )
95
+
96
+ # Clean response
97
+ response = tokenizer.decode(outputs[0], skip_special_tokens=False)
98
+ response = response.split("<|assistant|>")[-1].split("<|end|>")[0].strip()
99
+
100
+ return response
101
 
102
+ iface = gr.ChatInterface(
103
+ fn=chat,
104
+ title="Fast SQL Chatbot",
105
+ description="Ask SQL questions (e.g., 'delete duplicate rows based on email')"
106
+ )
107
 
108
+ iface.launch()