Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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(
|
|
@@ -22,29 +90,30 @@ pipe = pipeline(
|
|
| 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 |
-
#
|
| 31 |
response = pipe(
|
| 32 |
history_text,
|
| 33 |
-
max_new_tokens=
|
| 34 |
-
do_sample=
|
| 35 |
-
temperature=
|
| 36 |
-
top_p=
|
| 37 |
)[0]["generated_text"]
|
| 38 |
|
| 39 |
-
#
|
| 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 |
|
|
@@ -54,8 +123,9 @@ with gr.Blocks() as demo:
|
|
| 54 |
return "", chat_history
|
| 55 |
|
| 56 |
msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
|
| 57 |
-
clear.click(lambda:
|
| 58 |
|
| 59 |
# --- Launch ---
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|
|
|
|
|
|
| 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(
|
| 17 |
+
# "text-generation",
|
| 18 |
+
# model=model,
|
| 19 |
+
# tokenizer=tokenizer,
|
| 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__":
|
| 61 |
+
# demo.launch()
|
| 62 |
+
|
| 63 |
+
|
| 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 |
|
| 84 |
pipe = pipeline(
|
|
|
|
| 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 |
|
|
|
|
| 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 |
+
|