Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files- README.md +52 -39
- app.py +75 -53
- requirements.txt +3 -2
README.md
CHANGED
|
@@ -7,63 +7,76 @@ sdk: gradio
|
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_port: 7860
|
| 9 |
hardware: zero-gpu
|
| 10 |
-
tags:
|
| 11 |
-
- anycoder
|
| 12 |
---
|
| 13 |
# π€ VibeThinker-1.5B Chat Interface
|
| 14 |
|
| 15 |
-
A
|
| 16 |
|
| 17 |
-
## Model
|
| 18 |
-
- **Model ID**: WeiboAI/VibeThinker-1.5B
|
| 19 |
-
- **Parameters**: 1.
|
| 20 |
- **System Prompt**: "You are a concise solver. Respond briefly."
|
| 21 |
-
- **
|
| 22 |
|
| 23 |
-
## Features
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
-
-
|
| 27 |
-
-
|
|
|
|
| 28 |
|
| 29 |
## Example Prompts
|
|
|
|
| 30 |
- What is 2+2?
|
| 31 |
- Explain quantum physics briefly
|
| 32 |
- Write a short poem
|
| 33 |
- How do I make good decisions?
|
| 34 |
- What are the benefits of AI?
|
| 35 |
- Tell me about space exploration
|
|
|
|
| 36 |
|
| 37 |
-
##
|
| 38 |
-
Type your message in the chat box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
---
|
| 41 |
-
*
|
| 42 |
```
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
**Key Improvements:**
|
| 46 |
-
1. β
**Minimal API**: Uses only basic ChatInterface parameters
|
| 47 |
-
2. β
**Fixed None Handling**: Proper `str()` conversion for all inputs
|
| 48 |
-
3. β
**Clear Logging**: Console messages show exactly what the model is doing
|
| 49 |
-
4. β
**Longer Output**: Increased max_new_tokens to 1024
|
| 50 |
-
5. β
**Better Response Extraction**: Properly extracts assistant response
|
| 51 |
-
6. β
**Simple Setup**: No complex fallbacks or error handling
|
| 52 |
-
7. β
**ZeroGPU**: Uses @spaces.GPU decorator
|
| 53 |
|
| 54 |
-
**
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
-
|
| 65 |
-
-
|
| 66 |
-
-
|
| 67 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_port: 7860
|
| 9 |
hardware: zero-gpu
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
# π€ VibeThinker-1.5B Chat Interface
|
| 12 |
|
| 13 |
+
A lightweight chat application powered by the VibeThinker-1.5B language model with ZeroGPU acceleration.
|
| 14 |
|
| 15 |
+
## Model Information
|
| 16 |
+
- **Model ID**: [WeiboAI/VibeThinker-1.5B](https://huggingface.co/WeiboAI/VibeThinker-1.5B)
|
| 17 |
+
- **Parameters**: 1.5 Billion
|
| 18 |
- **System Prompt**: "You are a concise solver. Respond briefly."
|
| 19 |
+
- **Architecture**: Optimized for fast inference
|
| 20 |
|
| 21 |
+
## Key Features
|
| 22 |
+
- π **ZeroGPU Acceleration**: Browser-based inference for speed
|
| 23 |
+
- π¬ **Interactive Chat**: Natural conversation interface
|
| 24 |
+
- π± **Responsive Design**: Works on all devices
|
| 25 |
+
- π― **Concise Responses**: Model trained to be brief and helpful
|
| 26 |
+
- π **Session Memory**: Maintains conversation context
|
| 27 |
|
| 28 |
## Example Prompts
|
| 29 |
+
Try these to get started:
|
| 30 |
- What is 2+2?
|
| 31 |
- Explain quantum physics briefly
|
| 32 |
- Write a short poem
|
| 33 |
- How do I make good decisions?
|
| 34 |
- What are the benefits of AI?
|
| 35 |
- Tell me about space exploration
|
| 36 |
+
- Give me a quick recipe idea
|
| 37 |
|
| 38 |
+
## How It Works
|
| 39 |
+
1. Type your message in the chat box
|
| 40 |
+
2. Press Enter or click Send
|
| 41 |
+
3. The model processes your input using ZeroGPU
|
| 42 |
+
4. Receive a concise, thoughtful response
|
| 43 |
+
5. Continue the conversation naturally
|
| 44 |
+
|
| 45 |
+
## Technical Details
|
| 46 |
+
- **Framework**: Gradio 5.49.1
|
| 47 |
+
- **Model Loading**: AutoTokenizer + AutoModelForCausalLM
|
| 48 |
+
- **Deployment**: Hugging Face Spaces with ZeroGPU
|
| 49 |
+
- **Model Size**: ~3.55GB
|
| 50 |
+
- **Inference Type**: Browser-based using WebGPU
|
| 51 |
+
|
| 52 |
+
## Usage Tips
|
| 53 |
+
- The model is optimized for concise answers
|
| 54 |
+
- Keep prompts clear and specific
|
| 55 |
+
- Build on previous responses for context
|
| 56 |
+
- Ask follow-up questions naturally
|
| 57 |
|
| 58 |
---
|
| 59 |
+
*Powered by ZeroGPU technology for instant inference*
|
| 60 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
**Key Fixes:**
|
| 63 |
+
1. β
**Latest Gradio**: Updated to 5.49.1 in README.md
|
| 64 |
+
2. β
**Minimal API**: Most basic ChatInterface parameters
|
| 65 |
+
3. β
**Robust None Handling**: Comprehensive null checks
|
| 66 |
+
4. β
**Safe History Processing**: Validates history structure
|
| 67 |
+
5. β
**Clear Console Output**: Shows exactly what's happening
|
| 68 |
+
6. β
**Longer Responses**: Increased max_new_tokens to 800
|
| 69 |
+
7. β
**Proper Response Extraction**: Better parsing of model output
|
| 70 |
+
8. β
**Error Resilience**: Graceful handling of edge cases
|
| 71 |
|
| 72 |
+
**Console Output:**
|
| 73 |
+
- Loading model: WeiboAI/VibeThinker-1.5B
|
| 74 |
+
- Model loaded successfully!
|
| 75 |
+
- Processing: "What is 2+2?"
|
| 76 |
+
- Formatting input...
|
| 77 |
+
- Tokenizing...
|
| 78 |
+
- Generating...
|
| 79 |
+
- Decoding...
|
| 80 |
+
- Response: The answer is 4...
|
| 81 |
|
| 82 |
+
This should work reliably!
|
app.py
CHANGED
|
@@ -2,49 +2,68 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import spaces
|
| 5 |
-
import time
|
| 6 |
|
| 7 |
# Model configuration
|
| 8 |
MODEL_ID = "WeiboAI/VibeThinker-1.5B"
|
| 9 |
SYSTEM_PROMPT = "You are a concise solver. Respond briefly."
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
tokenizer =
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
|
| 25 |
-
# Handle None values
|
| 26 |
if message is None:
|
| 27 |
message = "Hello"
|
| 28 |
if history is None:
|
| 29 |
history = []
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 36 |
|
| 37 |
-
# Add history
|
| 38 |
-
for
|
| 39 |
-
if
|
|
|
|
|
|
|
| 40 |
messages.append({"role": "user", "content": str(user_msg)})
|
| 41 |
-
if assistant_msg is not None:
|
| 42 |
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 43 |
|
| 44 |
# Add current message
|
| 45 |
-
messages.append({"role": "user", "content":
|
| 46 |
|
| 47 |
-
print("
|
| 48 |
|
| 49 |
# Apply template
|
| 50 |
prompt = tokenizer.apply_chat_template(
|
|
@@ -53,62 +72,65 @@ def chat_fn(message, history):
|
|
| 53 |
add_generation_prompt=True
|
| 54 |
)
|
| 55 |
|
| 56 |
-
print("
|
| 57 |
|
| 58 |
-
#
|
| 59 |
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 60 |
|
| 61 |
-
print("
|
| 62 |
|
| 63 |
# Generate response
|
| 64 |
with torch.no_grad():
|
| 65 |
outputs = model.generate(
|
| 66 |
**inputs,
|
| 67 |
-
max_new_tokens=
|
| 68 |
do_sample=True,
|
| 69 |
temperature=0.7,
|
| 70 |
top_p=0.9,
|
| 71 |
pad_token_id=tokenizer.eos_token_id,
|
| 72 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
print(f"
|
| 87 |
-
return f"
|
| 88 |
|
| 89 |
-
def
|
| 90 |
-
"""Create
|
| 91 |
|
|
|
|
| 92 |
demo = gr.ChatInterface(
|
| 93 |
-
fn=
|
| 94 |
-
title="π€ VibeThinker
|
| 95 |
-
description=f"Chat with {MODEL_ID}. System: {SYSTEM_PROMPT}",
|
| 96 |
-
examples=[
|
| 97 |
-
"What is 2+2?",
|
| 98 |
-
"Explain quantum physics briefly",
|
| 99 |
-
"Write a short poem",
|
| 100 |
-
"How do I make good decisions?",
|
| 101 |
-
"What are the benefits of AI?",
|
| 102 |
-
"Tell me about space exploration"
|
| 103 |
-
],
|
| 104 |
)
|
| 105 |
|
| 106 |
return demo
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
print("
|
| 110 |
-
print(f"π¦ Model: {MODEL_ID}")
|
| 111 |
-
print(f"π¬ System: {SYSTEM_PROMPT}")
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import spaces
|
|
|
|
| 5 |
|
| 6 |
# Model configuration
|
| 7 |
MODEL_ID = "WeiboAI/VibeThinker-1.5B"
|
| 8 |
SYSTEM_PROMPT = "You are a concise solver. Respond briefly."
|
| 9 |
|
| 10 |
+
# Global variables
|
| 11 |
+
model = None
|
| 12 |
+
tokenizer = None
|
| 13 |
+
|
| 14 |
+
def load_model():
|
| 15 |
+
"""Load model and tokenizer"""
|
| 16 |
+
global model, tokenizer
|
| 17 |
+
try:
|
| 18 |
+
print(f"Loading model: {MODEL_ID}")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
)
|
| 25 |
+
print("Model loaded successfully!")
|
| 26 |
+
return True
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error loading model: {e}")
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
# Load model
|
| 32 |
+
load_success = load_model()
|
| 33 |
|
| 34 |
@spaces.GPU
|
| 35 |
+
def chat_function(message, history):
|
| 36 |
+
"""Chat function with robust error handling"""
|
| 37 |
|
| 38 |
+
# Handle None values
|
| 39 |
if message is None:
|
| 40 |
message = "Hello"
|
| 41 |
if history is None:
|
| 42 |
history = []
|
| 43 |
|
| 44 |
+
# Ensure strings
|
| 45 |
+
message = str(message)
|
| 46 |
+
if not isinstance(history, list):
|
| 47 |
+
history = []
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
print(f"Processing: {message}")
|
| 51 |
+
|
| 52 |
+
# Build messages
|
| 53 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 54 |
|
| 55 |
+
# Add history safely
|
| 56 |
+
for item in history:
|
| 57 |
+
if isinstance(item, (list, tuple)) and len(item) >= 2:
|
| 58 |
+
user_msg = item[0] if item[0] is not None else ""
|
| 59 |
+
assistant_msg = item[1] if item[1] is not None else ""
|
| 60 |
messages.append({"role": "user", "content": str(user_msg)})
|
|
|
|
| 61 |
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 62 |
|
| 63 |
# Add current message
|
| 64 |
+
messages.append({"role": "user", "content": message})
|
| 65 |
|
| 66 |
+
print("Formatting input...")
|
| 67 |
|
| 68 |
# Apply template
|
| 69 |
prompt = tokenizer.apply_chat_template(
|
|
|
|
| 72 |
add_generation_prompt=True
|
| 73 |
)
|
| 74 |
|
| 75 |
+
print("Tokenizing...")
|
| 76 |
|
| 77 |
+
# Prepare input
|
| 78 |
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 79 |
|
| 80 |
+
print("Generating...")
|
| 81 |
|
| 82 |
# Generate response
|
| 83 |
with torch.no_grad():
|
| 84 |
outputs = model.generate(
|
| 85 |
**inputs,
|
| 86 |
+
max_new_tokens=800,
|
| 87 |
do_sample=True,
|
| 88 |
temperature=0.7,
|
| 89 |
top_p=0.9,
|
| 90 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
+
print("Decoding...")
|
| 94 |
+
|
| 95 |
+
# Decode and extract response
|
| 96 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 97 |
|
| 98 |
+
# Find the assistant response part
|
| 99 |
+
if "assistant" in full_response:
|
| 100 |
+
response = full_response.split("assistant")[-1].strip()
|
| 101 |
+
else:
|
| 102 |
+
response = full_response
|
| 103 |
|
| 104 |
+
# Clean up
|
| 105 |
+
response = response.replace("<|endoftext|>", "").strip()
|
| 106 |
+
|
| 107 |
+
print(f"Response: {response[:100]}...")
|
| 108 |
+
return response
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
+
print(f"Error: {e}")
|
| 112 |
+
return f"Error: {str(e)}"
|
| 113 |
|
| 114 |
+
def create_demo():
|
| 115 |
+
"""Create demo interface"""
|
| 116 |
|
| 117 |
+
# Most basic ChatInterface that should work everywhere
|
| 118 |
demo = gr.ChatInterface(
|
| 119 |
+
fn=chat_function,
|
| 120 |
+
title="π€ VibeThinker Chat",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
return demo
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
+
print("Starting chat app...")
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
if load_success:
|
| 129 |
+
demo = create_demo()
|
| 130 |
+
demo.launch(share=False)
|
| 131 |
+
else:
|
| 132 |
+
print("Model failed to load!")
|
| 133 |
+
|
| 134 |
+
# Still create demo for debugging
|
| 135 |
+
demo = create_demo()
|
| 136 |
+
demo.launch(share=False)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
transformers>=4.
|
| 3 |
accelerate>=0.25.0
|
| 4 |
torch>=2.0.0
|
| 5 |
spaces>=0.19.4
|
|
|
|
|
|
| 1 |
+
gradio==5.49.1
|
| 2 |
+
transformers>=4.45.0
|
| 3 |
accelerate>=0.25.0
|
| 4 |
torch>=2.0.0
|
| 5 |
spaces>=0.19.4
|
| 6 |
+
uvicorn>=0.14.0
|