asimcodeml commited on
Commit
6cb5a1c
·
verified ·
1 Parent(s): a6be578

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -64,20 +64,17 @@
64
 
65
 
66
 
67
-
68
-
69
-
70
  import gradio as gr
71
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
72
 
73
  # --- Load Model ---
74
- MODEL_PATH = "./tinyllama-jobskills-final_update_4" # Model files path
75
 
76
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
77
  model = AutoModelForCausalLM.from_pretrained(
78
  MODEL_PATH,
79
  trust_remote_code=True,
80
- device_map="auto", # Will use GPU if available
81
  low_cpu_mem_usage=True
82
  )
83
 
@@ -88,44 +85,54 @@ pipe = pipeline(
88
  device_map="auto"
89
  )
90
 
91
- # --- Define Chat Function ---
92
  def chat_fn(message, history):
93
- # Convert history to text
94
  history_text = ""
95
- for user, bot in history[-5:]: # Use only last 5 exchanges for speed
96
- history_text += f"User: {user}\nAssistant: {bot}\n"
 
97
  history_text += f"User: {message}\nAssistant:"
98
 
99
- # Generate response
100
  response = pipe(
101
  history_text,
102
- max_new_tokens=64, # Reduced for CPU
103
- do_sample=False, # Greedy decoding for speed
104
- temperature=1.0,
105
  top_p=1.0
106
  )[0]["generated_text"]
107
 
108
  # Extract assistant reply
109
  reply = response.split("Assistant:")[-1].strip()
110
- return reply
 
 
 
 
 
 
111
 
112
  # --- Gradio UI ---
113
  with gr.Blocks() as demo:
114
- gr.Markdown("## 🚀 Chat with My Custom Model (CPU-Friendly)")
115
 
116
- chatbot = gr.Chatbot(type="messages") # updated for future versions
117
- msg = gr.Textbox(label="Type your message")
118
  clear = gr.Button("Clear")
119
 
120
  def user_fn(user_message, chat_history):
121
  bot_message = chat_fn(user_message, chat_history)
122
- chat_history.append((user_message, bot_message))
 
123
  return "", chat_history
124
 
125
  msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
126
- clear.click(lambda: [], None, chatbot, queue=False) # clears chat
127
 
128
  # --- Launch ---
129
  if __name__ == "__main__":
130
  demo.launch()
131
 
 
 
 
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
  )
80
 
 
85
  device_map="auto"
86
  )
87
 
88
+ # --- Chat Function ---
89
  def chat_fn(message, history):
90
+ # Convert history into text for prompt
91
  history_text = ""
92
+ for msg in history[-10:]:
93
+ role = "User" if msg["role"] == "user" else "Assistant"
94
+ history_text += f"{role}: {msg['content']}\n"
95
  history_text += f"User: {message}\nAssistant:"
96
 
97
+ # Generate response from model
98
  response = pipe(
99
  history_text,
100
+ max_new_tokens=16,
101
+ do_sample=False,
102
+ temperature=0.7,
103
  top_p=1.0
104
  )[0]["generated_text"]
105
 
106
  # Extract assistant reply
107
  reply = response.split("Assistant:")[-1].strip()
108
+
109
+ # Format reply into bullet points
110
+ skills = [s.strip() for s in reply.replace(",", "\n").split("\n") if s.strip()]
111
+ formatted_reply = "\n".join([f"- {s}" for s in skills])
112
+
113
+ return formatted_reply
114
+
115
 
116
  # --- Gradio UI ---
117
  with gr.Blocks() as demo:
118
+ gr.Markdown("## 🚀 Chat with My AI Skills Model")
119
 
120
+ chatbot = gr.Chatbot(type="messages")
121
+ msg = gr.Textbox(label="Type your question here...")
122
  clear = gr.Button("Clear")
123
 
124
  def user_fn(user_message, chat_history):
125
  bot_message = chat_fn(user_message, chat_history)
126
+ chat_history.append({"role": "user", "content": user_message})
127
+ chat_history.append({"role": "assistant", "content": bot_message})
128
  return "", chat_history
129
 
130
  msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
131
+ clear.click(lambda: [], None, chatbot, queue=False)
132
 
133
  # --- Launch ---
134
  if __name__ == "__main__":
135
  demo.launch()
136
 
137
+
138
+