asimcodeml commited on
Commit
b11dcfc
ยท
verified ยท
1 Parent(s): 95717ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -44
app.py CHANGED
@@ -1,16 +1,16 @@
 
1
  # import gradio as gr
2
  # from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
  # # --- Load Model ---
5
- # MODEL_PATH = "./tinyllama-jobskills-final_update_4" # Model files are in the repo root
6
 
7
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
8
  # model = AutoModelForCausalLM.from_pretrained(
9
  # MODEL_PATH,
10
  # trust_remote_code=True,
11
- # device_map="auto",
12
- # low_cpu_mem_usage=True
13
-
14
  # )
15
 
16
  # pipe = pipeline(
@@ -20,41 +20,47 @@
20
  # device_map="auto"
21
  # )
22
 
23
- # # --- Define Chat Function ---
24
  # def chat_fn(message, history):
25
- # history_text = ""
26
- # for user, bot in history:
27
- # history_text += f"User: {user}\nAssistant: {bot}\n"
28
- # history_text += f"User: {message}\nAssistant:"
29
 
30
- # # generate response
31
  # response = pipe(
32
- # history_text,
33
- # max_new_tokens=256,
34
- # do_sample=True,
35
  # temperature=0.7,
36
- # top_p=0.9
37
  # )[0]["generated_text"]
38
 
39
- # # extract assistant reply
40
- # reply = response.split("Assistant:")[-1].strip()
41
- # return reply
 
 
 
 
 
 
 
42
 
43
  # # --- Gradio UI ---
44
  # with gr.Blocks() as demo:
45
- # gr.Markdown("## ๐Ÿš€ Chat with My Custom Model")
46
 
47
- # chatbot = gr.Chatbot()
48
- # msg = gr.Textbox(label="Type your message")
49
  # clear = gr.Button("Clear")
50
 
51
  # def user_fn(user_message, chat_history):
52
  # bot_message = chat_fn(user_message, chat_history)
53
- # chat_history.append((user_message, bot_message))
 
54
  # return "", chat_history
55
 
56
  # msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
57
- # clear.click(lambda: None, None, chatbot, queue=False)
58
 
59
  # # --- Launch ---
60
  # if __name__ == "__main__":
@@ -63,66 +69,80 @@
63
 
64
 
65
 
66
-
67
  import gradio as gr
68
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
69
 
70
  # --- Load Model ---
71
- MODEL_PATH = "./tinyllama-jobskills-final_update_4" # Path to your model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
74
  model = AutoModelForCausalLM.from_pretrained(
75
  MODEL_PATH,
76
  trust_remote_code=True,
77
- device_map="auto", # Use GPU if available
78
  low_cpu_mem_usage=True,
79
- load_in_4bit=True
80
  )
81
 
82
  pipe = pipeline(
83
  "text-generation",
84
  model=model,
85
  tokenizer=tokenizer,
86
- device_map="auto"
87
  )
88
 
89
  # --- Chat Function ---
90
- def chat_fn(message, history):
91
- # Use the same format as training data
92
  prompt = f"### Question:\n{message}\n\n### Answer:\n"
93
 
94
- # Generate response
95
  response = pipe(
96
  prompt,
97
- max_new_tokens=32, # allow longer output
98
  do_sample=False,
99
- temperature=0.7,
100
- top_p=1.0
 
 
101
  )[0]["generated_text"]
102
 
103
- # Extract only the answer part
104
  reply = response.split("### Answer:")[-1].strip()
105
 
106
- # Format into bullet points
107
  skills = [s.strip() for s in reply.replace(",", "\n").split("\n") if s.strip()]
108
  formatted_reply = "\n".join([f"- {s}" for s in skills])
109
 
110
  return formatted_reply
111
 
112
 
113
-
114
  # --- Gradio UI ---
115
  with gr.Blocks() as demo:
116
  gr.Markdown("## ๐Ÿš€ Chat with My AI Skills Model")
117
 
118
- chatbot = gr.Chatbot(type="messages")
119
- msg = gr.Textbox(label="Type your question here...")
120
  clear = gr.Button("Clear")
121
 
122
  def user_fn(user_message, chat_history):
123
- bot_message = chat_fn(user_message, chat_history)
124
- chat_history.append({"role": "user", "content": user_message})
125
- chat_history.append({"role": "assistant", "content": bot_message})
 
 
 
126
  return "", chat_history
127
 
128
  msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
@@ -132,5 +152,3 @@ with gr.Blocks() as demo:
132
  if __name__ == "__main__":
133
  demo.launch()
134
 
135
-
136
-
 
1
+
2
  # import gradio as gr
3
  # from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
 
5
  # # --- Load Model ---
6
+ # MODEL_PATH = "./tinyllama-jobskills-final_update_4" # Path to your model
7
 
8
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
9
  # model = AutoModelForCausalLM.from_pretrained(
10
  # MODEL_PATH,
11
  # trust_remote_code=True,
12
+ # device_map="auto", # Use GPU if available
13
+ # low_cpu_mem_usage=True,
 
14
  # )
15
 
16
  # pipe = pipeline(
 
20
  # device_map="auto"
21
  # )
22
 
23
+ # # --- Chat Function ---
24
  # def chat_fn(message, history):
25
+ # # Use the same format as training data
26
+ # prompt = f"### Question:\n{message}\n\n### Answer:\n"
 
 
27
 
28
+ # # Generate response
29
  # response = pipe(
30
+ # prompt,
31
+ # max_new_tokens=32, # allow longer output
32
+ # do_sample=False,
33
  # temperature=0.7,
34
+ # top_p=1.0
35
  # )[0]["generated_text"]
36
 
37
+ # # Extract only the answer part
38
+ # reply = response.split("### Answer:")[-1].strip()
39
+
40
+ # # Format into bullet points
41
+ # skills = [s.strip() for s in reply.replace(",", "\n").split("\n") if s.strip()]
42
+ # formatted_reply = "\n".join([f"- {s}" for s in skills])
43
+
44
+ # return formatted_reply
45
+
46
+
47
 
48
  # # --- Gradio UI ---
49
  # with gr.Blocks() as demo:
50
+ # gr.Markdown("## ๐Ÿš€ Chat with My AI Skills Model")
51
 
52
+ # chatbot = gr.Chatbot(type="messages")
53
+ # msg = gr.Textbox(label="Type your question here...")
54
  # clear = gr.Button("Clear")
55
 
56
  # def user_fn(user_message, chat_history):
57
  # bot_message = chat_fn(user_message, chat_history)
58
+ # chat_history.append({"role": "user", "content": user_message})
59
+ # chat_history.append({"role": "assistant", "content": bot_message})
60
  # return "", chat_history
61
 
62
  # msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
63
+ # clear.click(lambda: [], None, chatbot, queue=False)
64
 
65
  # # --- Launch ---
66
  # if __name__ == "__main__":
 
69
 
70
 
71
 
 
72
  import gradio as gr
73
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
74
+ import torch # Needed for torch.bfloat16
75
 
76
  # --- Load Model ---
77
+ MODEL_PATH = "./tinyllama-jobskills-final_update_4"
78
+ # --- Define Quantization Configuration ---
79
+ # This is the new way to specify 4-bit or 8-bit loading
80
+ # For 4-bit:
81
+ bnb_config = BitsAndBytesConfig(
82
+ load_in_4bit=True,
83
+ bnb_4bit_quant_type="nf4", # Or "fp4"
84
+ bnb_4bit_use_double_quant=True,
85
+ bnb_4bit_compute_dtype=torch.bfloat16, # Or torch.float16 if not using bfloat16
86
+ )
87
+
88
+ # For 8-bit (if preferred, though 4-bit is smaller and often good enough)
89
+ # bnb_config = BitsAndBytesConfig(
90
+ # load_in_8bit=True
91
+ # )
92
 
93
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
94
  model = AutoModelForCausalLM.from_pretrained(
95
  MODEL_PATH,
96
  trust_remote_code=True,
97
+ device_map="auto",
98
  low_cpu_mem_usage=True,
99
+ quantization_config=bnb_config, # <--- Pass the BitsAndBytesConfig object here
100
  )
101
 
102
  pipe = pipeline(
103
  "text-generation",
104
  model=model,
105
  tokenizer=tokenizer,
106
+ device_map="auto" # Redundant if model is already on device, but harmless
107
  )
108
 
109
  # --- Chat Function ---
110
+ def chat_fn(message):
 
111
  prompt = f"### Question:\n{message}\n\n### Answer:\n"
112
 
 
113
  response = pipe(
114
  prompt,
115
+ max_new_tokens=32,
116
  do_sample=False,
117
+ # temperature and top_p are ignored when do_sample=False, so remove them:
118
+ # temperature=0.7,
119
+ # top_p=1.0,
120
+ return_full_text=False # Get only the newly generated text
121
  )[0]["generated_text"]
122
 
 
123
  reply = response.split("### Answer:")[-1].strip()
124
 
 
125
  skills = [s.strip() for s in reply.replace(",", "\n").split("\n") if s.strip()]
126
  formatted_reply = "\n".join([f"- {s}" for s in skills])
127
 
128
  return formatted_reply
129
 
130
 
 
131
  # --- Gradio UI ---
132
  with gr.Blocks() as demo:
133
  gr.Markdown("## ๐Ÿš€ Chat with My AI Skills Model")
134
 
135
+ chatbot = gr.Chatbot(label="Chat History")
136
+ msg = gr.Textbox(label="Type your question here...", placeholder="Ask about job skills...")
137
  clear = gr.Button("Clear")
138
 
139
  def user_fn(user_message, chat_history):
140
+ chat_history = chat_history or []
141
+ chat_history.append([user_message, None])
142
+
143
+ bot_message = chat_fn(user_message)
144
+
145
+ chat_history[-1][1] = bot_message
146
  return "", chat_history
147
 
148
  msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
 
152
  if __name__ == "__main__":
153
  demo.launch()
154