Spaces:
Sleeping
Sleeping
Create app.py
Browse filesgradio==4.22.0
transformers==4.39.3
torch==2.2.2
accelerate==0.28.0
setuptools>=68.0.0
wheel>=0.41.0
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
import torch
|
| 4 |
+
import threading
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # small enough for CPU Basic
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype=torch.float32, # stay on CPU
|
| 12 |
+
low_cpu_mem_usage=True
|
| 13 |
+
)
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
SYSTEM_PROMPT = "You are a helpful assistant."
|
| 17 |
+
|
| 18 |
+
def chat_fn(history, max_new_tokens=256, temperature=0.7, top_p=0.9):
|
| 19 |
+
# history: list of [user, assistant]
|
| 20 |
+
messages = []
|
| 21 |
+
messages.append({"role": "system", "content": SYSTEM_PROMPT})
|
| 22 |
+
for u, a in history:
|
| 23 |
+
messages.append({"role": "user", "content": u})
|
| 24 |
+
if a:
|
| 25 |
+
messages.append({"role": "assistant", "content": a})
|
| 26 |
+
|
| 27 |
+
# Simple prompt format for TinyLlama chat
|
| 28 |
+
prompt = ""
|
| 29 |
+
for m in messages:
|
| 30 |
+
if m["role"] == "system":
|
| 31 |
+
prompt += f"<|system|>\n{m['content']}\n"
|
| 32 |
+
elif m["role"] == "user":
|
| 33 |
+
prompt += f"<|user|>\n{m['content']}\n"
|
| 34 |
+
elif m["role"] == "assistant":
|
| 35 |
+
prompt += f"<|assistant|>\n{m['content']}\n"
|
| 36 |
+
prompt += "<|assistant|>\n"
|
| 37 |
+
|
| 38 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 39 |
+
input_ids = inputs["input_ids"]
|
| 40 |
+
|
| 41 |
+
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
| 42 |
+
generation_kwargs = dict(
|
| 43 |
+
input_ids=input_ids,
|
| 44 |
+
max_new_tokens=max_new_tokens,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
top_p=top_p,
|
| 48 |
+
streamer=streamer
|
| 49 |
+
)
|
| 50 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 51 |
+
thread.start()
|
| 52 |
+
|
| 53 |
+
partial = ""
|
| 54 |
+
for new_text in streamer:
|
| 55 |
+
partial += new_text
|
| 56 |
+
yield partial
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(title="TinyLlama Chat (CPU Free)") as demo:
|
| 59 |
+
gr.Markdown("## TinyLlama Chat — Free CPU Space")
|
| 60 |
+
chatbot = gr.Chatbot(height=400)
|
| 61 |
+
with gr.Row():
|
| 62 |
+
msg = gr.Textbox(placeholder="Type your question...")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
| 65 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, label="top_p")
|
| 66 |
+
max_new_tokens = gr.Slider(32, 512, value=256, step=8, label="Max new tokens")
|
| 67 |
+
send = gr.Button("Send")
|
| 68 |
+
|
| 69 |
+
def user_submit(user_message, chat_history):
|
| 70 |
+
chat_history = chat_history + [[user_message, None]]
|
| 71 |
+
return "", chat_history
|
| 72 |
+
|
| 73 |
+
def bot_respond(chat_history, max_new_tokens, temperature, top_p):
|
| 74 |
+
user_message = chat_history[-1][0]
|
| 75 |
+
gen = chat_fn(chat_history[:-1] + [[user_message, ""]], max_new_tokens, temperature, top_p)
|
| 76 |
+
partial = ""
|
| 77 |
+
for chunk in gen:
|
| 78 |
+
partial = chunk
|
| 79 |
+
chat_history[-1][1] = partial
|
| 80 |
+
yield chat_history
|
| 81 |
+
|
| 82 |
+
send.click(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 83 |
+
bot_respond, [chatbot, max_new_tokens, temperature, top_p], [chatbot]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
demo.queue().launch()
|