Spaces:
Running
on
Zero
Running
on
Zero
Deploy Gradio app with multiple files
Browse files- app.py +118 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class VibeThinker:
|
| 7 |
+
def __init__(self, model_path):
|
| 8 |
+
self.model_path = model_path
|
| 9 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
self.model_path,
|
| 11 |
+
low_cpu_mem_usage=True,
|
| 12 |
+
torch_dtype=torch.bfloat16,
|
| 13 |
+
device_map="auto"
|
| 14 |
+
)
|
| 15 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_path, trust_remote_code=True)
|
| 16 |
+
|
| 17 |
+
def infer_text(self, messages):
|
| 18 |
+
text = self.tokenizer.apply_chat_template(
|
| 19 |
+
messages,
|
| 20 |
+
tokenize=False,
|
| 21 |
+
add_generation_prompt=True
|
| 22 |
+
)
|
| 23 |
+
model_inputs = self.tokenizer([text], return_tensors="pt").to(self.model.device)
|
| 24 |
+
|
| 25 |
+
generation_config = dict(
|
| 26 |
+
max_new_tokens=4096,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
temperature=0.6,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
top_k=-1
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
generated_ids = self.model.generate(
|
| 34 |
+
**model_inputs,
|
| 35 |
+
generation_config=GenerationConfig(**generation_config)
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
generated_ids = [
|
| 39 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
response = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 43 |
+
return response
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Initialize the model
|
| 47 |
+
print("Loading VibeThinker model...")
|
| 48 |
+
vibe_model = VibeThinker('WeiboAI/VibeThinker-1.5B')
|
| 49 |
+
print("Model loaded successfully!")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def respond(message, history):
|
| 53 |
+
"""
|
| 54 |
+
Generate response for the chatbot.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
message: The user's current message
|
| 58 |
+
history: List of previous conversation messages in [user, assistant] format
|
| 59 |
+
"""
|
| 60 |
+
# Convert history to messages format
|
| 61 |
+
messages = []
|
| 62 |
+
for user_msg, assistant_msg in history:
|
| 63 |
+
messages.append({"role": "user", "content": user_msg})
|
| 64 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 65 |
+
|
| 66 |
+
# Add current message
|
| 67 |
+
messages.append({"role": "user", "content": message})
|
| 68 |
+
|
| 69 |
+
# Generate response
|
| 70 |
+
response = vibe_model.infer_text(messages)
|
| 71 |
+
|
| 72 |
+
return response
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Create the Gradio interface
|
| 76 |
+
with gr.Blocks(
|
| 77 |
+
theme=gr.themes.Soft(),
|
| 78 |
+
css="""
|
| 79 |
+
.header-link { text-decoration: none; color: inherit; }
|
| 80 |
+
.header-link:hover { text-decoration: underline; }
|
| 81 |
+
"""
|
| 82 |
+
) as demo:
|
| 83 |
+
gr.Markdown(
|
| 84 |
+
"""
|
| 85 |
+
# 💭 VibeThinker Chatbot
|
| 86 |
+
Chat with [WeiboAI/VibeThinker-1.5B](https://huggingface.co/WeiboAI/VibeThinker-1.5B) - a powerful conversational AI model.
|
| 87 |
+
|
| 88 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" class="header-link">Built with anycoder</a>
|
| 89 |
+
"""
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
chatbot = gr.ChatInterface(
|
| 93 |
+
fn=respond,
|
| 94 |
+
type="messages",
|
| 95 |
+
title="",
|
| 96 |
+
description="Ask me anything! I'm powered by VibeThinker.",
|
| 97 |
+
examples=[
|
| 98 |
+
"What is the meaning of life?",
|
| 99 |
+
"Explain quantum computing in simple terms",
|
| 100 |
+
"Write a short poem about artificial intelligence",
|
| 101 |
+
"How can I improve my productivity?",
|
| 102 |
+
],
|
| 103 |
+
cache_examples=False,
|
| 104 |
+
retry_btn=None,
|
| 105 |
+
undo_btn=None,
|
| 106 |
+
clear_btn="Clear Chat",
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
gr.Markdown(
|
| 110 |
+
"""
|
| 111 |
+
### About VibeThinker
|
| 112 |
+
VibeThinker is a 1.5B parameter conversational AI model designed for engaging and thoughtful conversations.
|
| 113 |
+
The model uses temperature sampling (0.6) for balanced creativity and coherence.
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|
| 5 |
+
bitsandbytes
|
| 6 |
+
sentencepiece
|
| 7 |
+
protobuf
|