Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,17 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
|
| 4 |
from threading import Thread
|
| 5 |
from transformers import TextIteratorStreamer
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Language-specific system prompts (extracted from training set)
|
| 8 |
LANGUAGE_SYSTEM_PROMPTS = {
|
| 9 |
# Germanic languages
|
|
@@ -71,10 +82,15 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 71 |
device_map="auto",
|
| 72 |
)
|
| 73 |
|
| 74 |
-
print("Model loaded!")
|
| 75 |
|
|
|
|
| 76 |
def chat(message, history, temperature, language):
|
| 77 |
-
"""Generate response with configurable temperature and language.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Gradio ChatInterface passes history with content as string or list[dict]
|
| 79 |
# Extract text content for apply_chat_template
|
| 80 |
def extract_text(content):
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
from transformers import TextIteratorStreamer
|
| 6 |
|
| 7 |
+
# ZeroGPU support for Hugging Face Spaces (effect-free if not in Spaces)
|
| 8 |
+
try:
|
| 9 |
+
import spaces
|
| 10 |
+
GPU_DECORATOR = spaces.GPU(duration=30)
|
| 11 |
+
print("[SUCCESS] ZeroGPU support enabled")
|
| 12 |
+
except ImportError:
|
| 13 |
+
# Fallback for local execution
|
| 14 |
+
def GPU_DECORATOR(func):
|
| 15 |
+
return func
|
| 16 |
+
print("[WARNING] Running without ZeroGPU (local mode)")
|
| 17 |
+
|
| 18 |
# Language-specific system prompts (extracted from training set)
|
| 19 |
LANGUAGE_SYSTEM_PROMPTS = {
|
| 20 |
# Germanic languages
|
|
|
|
| 82 |
device_map="auto",
|
| 83 |
)
|
| 84 |
|
| 85 |
+
print("[SUCCESS] Model loaded!")
|
| 86 |
|
| 87 |
+
@GPU_DECORATOR
|
| 88 |
def chat(message, history, temperature, language):
|
| 89 |
+
"""Generate response with configurable temperature and language.
|
| 90 |
+
|
| 91 |
+
This function is decorated with @spaces.GPU for dynamic GPU allocation
|
| 92 |
+
in Hugging Face Spaces (30 second duration). Effect-free locally.
|
| 93 |
+
"""
|
| 94 |
# Gradio ChatInterface passes history with content as string or list[dict]
|
| 95 |
# Extract text content for apply_chat_template
|
| 96 |
def extract_text(content):
|