Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,11 @@ model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0
|
|
| 12 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 13 |
model = model.to(device)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Defining a custom stopping criteria class for the model's text generation.
|
| 17 |
class StopOnTokens(StoppingCriteria):
|
|
@@ -24,13 +29,19 @@ class StopOnTokens(StoppingCriteria):
|
|
| 24 |
|
| 25 |
|
| 26 |
# Function to generate model predictions.
|
| 27 |
-
def predict(message, history):
|
|
|
|
|
|
|
|
|
|
| 28 |
history_transformer_format = history + [[message, ""]]
|
| 29 |
stop = StopOnTokens()
|
| 30 |
|
| 31 |
-
# Formatting the input for the model
|
| 32 |
-
|
|
|
|
| 33 |
for item in history_transformer_format])
|
|
|
|
|
|
|
| 34 |
model_inputs = tokenizer([messages], return_tensors="pt").to(device)
|
| 35 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
| 36 |
generate_kwargs = dict(
|
|
@@ -54,9 +65,88 @@ def predict(message, history):
|
|
| 54 |
yield partial_message
|
| 55 |
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 13 |
model = model.to(device)
|
| 14 |
|
| 15 |
+
# System content - Define the assistant's personality and capabilities
|
| 16 |
+
SYSTEM_CONTENT = """You are TinyLlama, a friendly and helpful AI assistant.
|
| 17 |
+
You are based on the TinyLlama-1.1B-Chat model and you excel at providing clear,
|
| 18 |
+
concise answers to various questions. You are knowledgeable about general topics
|
| 19 |
+
and always strive to be helpful and accurate in your responses."""
|
| 20 |
|
| 21 |
# Defining a custom stopping criteria class for the model's text generation.
|
| 22 |
class StopOnTokens(StoppingCriteria):
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
# Function to generate model predictions.
|
| 32 |
+
def predict(message, history, system_content=None):
|
| 33 |
+
# Use custom system content if provided, otherwise use default
|
| 34 |
+
current_system_content = system_content if system_content else SYSTEM_CONTENT
|
| 35 |
+
|
| 36 |
history_transformer_format = history + [[message, ""]]
|
| 37 |
stop = StopOnTokens()
|
| 38 |
|
| 39 |
+
# Formatting the input for the model with system content
|
| 40 |
+
system_prompt = f"<|system|>\n{current_system_content}</s>"
|
| 41 |
+
conversation = "</s>".join(["</s>".join(["\n<|user|>:" + item[0], "\n<|assistant|>:" + item[1]])
|
| 42 |
for item in history_transformer_format])
|
| 43 |
+
|
| 44 |
+
messages = system_prompt + conversation
|
| 45 |
model_inputs = tokenizer([messages], return_tensors="pt").to(device)
|
| 46 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
| 47 |
generate_kwargs = dict(
|
|
|
|
| 65 |
yield partial_message
|
| 66 |
|
| 67 |
|
| 68 |
+
# Custom function to handle system content updates
|
| 69 |
+
def update_system_content(system_content):
|
| 70 |
+
global SYSTEM_CONTENT
|
| 71 |
+
if system_content.strip():
|
| 72 |
+
SYSTEM_CONTENT = system_content
|
| 73 |
+
return "System content updated successfully!"
|
| 74 |
+
else:
|
| 75 |
+
return "Please enter valid system content."
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# Additional function to reset system content to default
|
| 79 |
+
def reset_system_content():
|
| 80 |
+
global SYSTEM_CONTENT
|
| 81 |
+
default_content = """You are TinyLlama, a friendly and helpful AI assistant.
|
| 82 |
+
You are based on the TinyLlama-1.1B-Chat model and you excel at providing clear,
|
| 83 |
+
concise answers to various questions. You are knowledgeable about general topics
|
| 84 |
+
and always strive to be helpful and accurate in your responses."""
|
| 85 |
+
SYSTEM_CONTENT = default_content
|
| 86 |
+
return default_content, "System content reset to default!"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Create the interface with additional components for system content
|
| 90 |
+
with gr.Blocks(title="TinyLlama ChatBot with System Content", theme=gr.themes.Soft()) as demo:
|
| 91 |
+
gr.Markdown("# 🦙 TinyLlama ChatBot")
|
| 92 |
+
gr.Markdown("Chat with TinyLlama-1.1B-Chat model. Customize the system content to change how the assistant behaves.")
|
| 93 |
+
|
| 94 |
+
# System content configuration section
|
| 95 |
+
with gr.Accordion("⚙️ System Content Configuration", open=False):
|
| 96 |
+
gr.Markdown("Customize the assistant's personality and behavior:")
|
| 97 |
+
system_content_input = gr.Textbox(
|
| 98 |
+
label="System Content",
|
| 99 |
+
value=SYSTEM_CONTENT,
|
| 100 |
+
lines=4,
|
| 101 |
+
placeholder="Enter system content that defines the assistant's behavior...",
|
| 102 |
+
info="This content will shape how the AI assistant responds to your questions."
|
| 103 |
+
)
|
| 104 |
+
with gr.Row():
|
| 105 |
+
update_btn = gr.Button("Update System Content")
|
| 106 |
+
reset_btn = gr.Button("Reset to Default")
|
| 107 |
+
system_status = gr.Textbox(label="Status", interactive=False)
|
| 108 |
+
|
| 109 |
+
# Chat interface section
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column(scale=2):
|
| 112 |
+
gr.Markdown("### 💬 Chat with TinyLlama")
|
| 113 |
+
chat_interface = gr.ChatInterface(
|
| 114 |
+
predict,
|
| 115 |
+
examples=['How to cook a fish?', 'Who is the president of US now?', 'Explain quantum computing simply'],
|
| 116 |
+
cache_examples=False
|
| 117 |
+
)
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
gr.Markdown("### ℹ️ About")
|
| 120 |
+
gr.Markdown("""
|
| 121 |
+
**Model:** TinyLlama-1.1B-Chat-v1.0
|
| 122 |
+
**Parameters:** 1.1 Billion
|
| 123 |
+
**Context Window:** 2048 tokens
|
| 124 |
+
|
| 125 |
+
**Capabilities:**
|
| 126 |
+
- General conversation
|
| 127 |
+
- Question answering
|
| 128 |
+
- Creative writing
|
| 129 |
+
- Code generation
|
| 130 |
+
- And much more!
|
| 131 |
+
|
| 132 |
+
**Tips:**
|
| 133 |
+
- Use the system content to customize behavior
|
| 134 |
+
- Be specific in your questions
|
| 135 |
+
- The model works best with clear, concise prompts
|
| 136 |
+
""")
|
| 137 |
+
|
| 138 |
+
# Event handlers for system content updates
|
| 139 |
+
update_btn.click(
|
| 140 |
+
update_system_content,
|
| 141 |
+
inputs=[system_content_input],
|
| 142 |
+
outputs=[system_status]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
reset_btn.click(
|
| 146 |
+
reset_system_content,
|
| 147 |
+
outputs=[system_content_input, system_status]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|