Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,25 +3,23 @@ import gradio as gr
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# -------------------------------
|
| 6 |
-
# Load
|
| 7 |
# -------------------------------
|
| 8 |
pipe = pipeline(
|
| 9 |
"text-generation",
|
| 10 |
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 11 |
-
torch_dtype=torch.float32,
|
| 12 |
-
device_map=
|
| 13 |
)
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
-
# Chat Function
|
| 17 |
# -------------------------------
|
| 18 |
def chat(user_message, history, system_prompt, temperature, max_tokens):
|
| 19 |
-
# Stronger personality: always prepend system prompt
|
| 20 |
messages = [{"role": "system", "content": system_prompt}]
|
| 21 |
|
| 22 |
-
|
| 23 |
-
messages.
|
| 24 |
-
messages.append({"role": "assistant", "content": a})
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": user_message})
|
| 27 |
|
|
@@ -33,14 +31,17 @@ def chat(user_message, history, system_prompt, temperature, max_tokens):
|
|
| 33 |
|
| 34 |
output = pipe(
|
| 35 |
prompt,
|
| 36 |
-
max_new_tokens=max_tokens,
|
| 37 |
temperature=temperature,
|
| 38 |
top_p=0.9,
|
| 39 |
do_sample=True,
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
return history
|
| 45 |
|
| 46 |
# -------------------------------
|
|
@@ -48,58 +49,53 @@ def chat(user_message, history, system_prompt, temperature, max_tokens):
|
|
| 48 |
# -------------------------------
|
| 49 |
with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
|
| 50 |
gr.Markdown("## π¦ TinyLLaMA Chatbot")
|
| 51 |
-
gr.Markdown("Select personality to change chatbot behavior and adjust generation settings.")
|
| 52 |
|
| 53 |
-
# Predefined personalities
|
| 54 |
preset_prompts = {
|
| 55 |
-
"Pirate π΄ββ οΈ": "You are a
|
| 56 |
-
"Teacher π¨βπ«": "You are a patient teacher
|
| 57 |
-
"Coder π¨βπ»": "You are a
|
| 58 |
-
"Friendly
|
| 59 |
}
|
| 60 |
|
| 61 |
-
|
| 62 |
choices=list(preset_prompts.keys()),
|
| 63 |
value="Pirate π΄ββ οΈ",
|
| 64 |
-
label="Choose Personality"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
| 68 |
value=preset_prompts["Pirate π΄ββ οΈ"],
|
| 69 |
-
label="System Prompt
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
chatbot = gr.Chatbot(height=400)
|
| 79 |
user_input = gr.Textbox(label="Your Message")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
max_tokens_slider = gr.Slider(32, 256, value=128, step=16, label="Max Tokens")
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
chat,
|
| 90 |
-
inputs=[user_input, chatbot,
|
| 91 |
outputs=chatbot
|
| 92 |
)
|
| 93 |
|
| 94 |
user_input.submit(
|
| 95 |
chat,
|
| 96 |
-
inputs=[user_input, chatbot,
|
| 97 |
outputs=chatbot
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
# -------------------------------
|
| 103 |
-
# Launch
|
| 104 |
-
# -------------------------------
|
| 105 |
-
demo.launch(share=True)
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# -------------------------------
|
| 6 |
+
# Load Model (CPU-safe)
|
| 7 |
# -------------------------------
|
| 8 |
pipe = pipeline(
|
| 9 |
"text-generation",
|
| 10 |
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 11 |
+
torch_dtype=torch.float32,
|
| 12 |
+
device_map=None,
|
| 13 |
)
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# Chat Function (messages format)
|
| 17 |
# -------------------------------
|
| 18 |
def chat(user_message, history, system_prompt, temperature, max_tokens):
|
|
|
|
| 19 |
messages = [{"role": "system", "content": system_prompt}]
|
| 20 |
|
| 21 |
+
if history:
|
| 22 |
+
messages.extend(history)
|
|
|
|
| 23 |
|
| 24 |
messages.append({"role": "user", "content": user_message})
|
| 25 |
|
|
|
|
| 31 |
|
| 32 |
output = pipe(
|
| 33 |
prompt,
|
| 34 |
+
max_new_tokens=int(max_tokens),
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=0.9,
|
| 37 |
do_sample=True,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
assistant_reply = output[0]["generated_text"].split("<|assistant|>")[-1].strip()
|
| 41 |
+
|
| 42 |
+
history.append({"role": "user", "content": user_message})
|
| 43 |
+
history.append({"role": "assistant", "content": assistant_reply})
|
| 44 |
+
|
| 45 |
return history
|
| 46 |
|
| 47 |
# -------------------------------
|
|
|
|
| 49 |
# -------------------------------
|
| 50 |
with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
|
| 51 |
gr.Markdown("## π¦ TinyLLaMA Chatbot")
|
|
|
|
| 52 |
|
|
|
|
| 53 |
preset_prompts = {
|
| 54 |
+
"Pirate π΄ββ οΈ": "You are a pirate chatbot. Speak like a pirate.",
|
| 55 |
+
"Teacher π¨βπ«": "You are a patient teacher.",
|
| 56 |
+
"Coder π¨βπ»": "You are a programming assistant.",
|
| 57 |
+
"Friendly π€": "You are a friendly assistant."
|
| 58 |
}
|
| 59 |
|
| 60 |
+
personality = gr.Dropdown(
|
| 61 |
choices=list(preset_prompts.keys()),
|
| 62 |
value="Pirate π΄ββ οΈ",
|
| 63 |
+
label="Choose Personality"
|
| 64 |
)
|
| 65 |
|
| 66 |
+
system_prompt = gr.Textbox(
|
| 67 |
value=preset_prompts["Pirate π΄ββ οΈ"],
|
| 68 |
+
label="System Prompt"
|
| 69 |
)
|
| 70 |
|
| 71 |
+
personality.change(
|
| 72 |
+
lambda x: preset_prompts[x],
|
| 73 |
+
inputs=personality,
|
| 74 |
+
outputs=system_prompt
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
chatbot = gr.Chatbot(type="messages", height=400)
|
| 78 |
user_input = gr.Textbox(label="Your Message")
|
| 79 |
|
| 80 |
+
temperature = gr.Slider(0.1, 1.2, value=0.85)
|
| 81 |
+
max_tokens = gr.Slider(32, 128, value=96, step=16)
|
|
|
|
| 82 |
|
| 83 |
+
send = gr.Button("Send π")
|
| 84 |
+
clear = gr.Button("Clear π§Ή")
|
| 85 |
|
| 86 |
+
send.click(
|
| 87 |
chat,
|
| 88 |
+
inputs=[user_input, chatbot, system_prompt, temperature, max_tokens],
|
| 89 |
outputs=chatbot
|
| 90 |
)
|
| 91 |
|
| 92 |
user_input.submit(
|
| 93 |
chat,
|
| 94 |
+
inputs=[user_input, chatbot, system_prompt, temperature, max_tokens],
|
| 95 |
outputs=chatbot
|
| 96 |
)
|
| 97 |
|
| 98 |
+
clear.click(lambda: [], outputs=chatbot)
|
| 99 |
+
|
| 100 |
+
demo.launch()
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|