Text Generation
Transformers
Safetensors
GGUF
English
qwen2
quantum-ml
hybrid-quantum-classical
quantum-kernel
research
quantum-computing
nisq
qiskit
quantum-circuits
vibe-thinker
physics-inspired-ml
quantum-enhanced
hybrid-ai
1.5b
small-model
efficient-ai
reasoning
chemistry
physics
text-generation-inference
conversational
Update quantum_inference.py
Browse files- quantum_inference.py +0 -233
quantum_inference.py
CHANGED
|
@@ -1,234 +1 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Hypnos-i3-1.5B - Quantum-Classical Hybrid Model
|
| 4 |
-
================================================
|
| 5 |
-
Sentiment Analysis с квантовым ядром
|
| 6 |
|
| 7 |
-
Версия: 1.0
|
| 8 |
-
Релиз: December 2024
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import numpy as np
|
| 12 |
-
import json
|
| 13 |
-
import torch
|
| 14 |
-
from transformers import AutoModel, AutoTokenizer
|
| 15 |
-
from sklearn.decomposition import PCA
|
| 16 |
-
import time
|
| 17 |
-
|
| 18 |
-
print("="*70)
|
| 19 |
-
print("🌙 HYPNOS-i3-1.5B - QUANTUM-CLASSICAL MODEL")
|
| 20 |
-
print("="*70)
|
| 21 |
-
print("Version: 1.0")
|
| 22 |
-
print("Type: Quantum Kernel-Enhanced Sentiment Analysis")
|
| 23 |
-
print("Base: VibeThinker-1.5B + 2-qubit Quantum Kernel\n")
|
| 24 |
-
|
| 25 |
-
# Проверка наличия модели
|
| 26 |
-
try:
|
| 27 |
-
with open('hypnos_i3_results.json', 'r') as f:
|
| 28 |
-
results = json.load(f)
|
| 29 |
-
|
| 30 |
-
K_train = np.load('K_train_quantum.npy')
|
| 31 |
-
K_test = np.load('K_test_quantum.npy')
|
| 32 |
-
|
| 33 |
-
print("✅ Модель загружена из кэша")
|
| 34 |
-
print(f"📊 Точность (quantum): {results['accuracy_quantum']:.1%}")
|
| 35 |
-
print(f"📊 Точность (classical): {results['accuracy_baseline']:.1%}")
|
| 36 |
-
|
| 37 |
-
except FileNotFoundError:
|
| 38 |
-
print("⚠️ Модель не найдена. Запустите обучение:")
|
| 39 |
-
print(" python3 hypnos_i3.py")
|
| 40 |
-
exit(1)
|
| 41 |
-
|
| 42 |
-
# Загрузка VibeThinker
|
| 43 |
-
print("\n🔄 Загрузка базовой модели VibeThinker-1.5B...")
|
| 44 |
-
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
| 45 |
-
|
| 46 |
-
tokenizer = AutoTokenizer.from_pretrained("WeiboAI/VibeThinker-1.5B")
|
| 47 |
-
model = AutoModel.from_pretrained(
|
| 48 |
-
"WeiboAI/VibeThinker-1.5B",
|
| 49 |
-
torch_dtype=torch.float16
|
| 50 |
-
).to(device).eval()
|
| 51 |
-
|
| 52 |
-
print(f"✅ VibeThinker загружен на {device}")
|
| 53 |
-
|
| 54 |
-
# Обучающие данные (для квантового ядра)
|
| 55 |
-
TRAIN_DATA = [
|
| 56 |
-
("I absolutely love quantum computing! It's amazing!", 1),
|
| 57 |
-
("This is the worst experience ever, terrible.", 0),
|
| 58 |
-
("Quantum neural networks are fascinating and powerful.", 1),
|
| 59 |
-
("I hate bugs in my code, so frustrating!", 0),
|
| 60 |
-
("The future of AI is quantum, incredible potential!", 1),
|
| 61 |
-
("This product is garbage, waste of money.", 0),
|
| 62 |
-
("Machine learning combined with quantum is brilliant!", 1),
|
| 63 |
-
("Awful customer service, never coming back.", 0)
|
| 64 |
-
]
|
| 65 |
-
|
| 66 |
-
print(f"\n📚 База знаний: {len(TRAIN_DATA)} примеров")
|
| 67 |
-
|
| 68 |
-
# Функция предсказания
|
| 69 |
-
def predict(text, verbose=True):
|
| 70 |
-
"""
|
| 71 |
-
Предсказывает sentiment текста
|
| 72 |
-
|
| 73 |
-
Pipeline:
|
| 74 |
-
1. VibeThinker embeddings (1536D)
|
| 75 |
-
2. Normalization (важно!)
|
| 76 |
-
3. Quantum kernel similarity
|
| 77 |
-
4. Classification
|
| 78 |
-
"""
|
| 79 |
-
|
| 80 |
-
if verbose:
|
| 81 |
-
print(f"\n{'='*70}")
|
| 82 |
-
print(f"🔮 Анализ текста")
|
| 83 |
-
print(f"{'='*70}")
|
| 84 |
-
print(f"📝 Входной текст: '{text}'")
|
| 85 |
-
|
| 86 |
-
start = time.time()
|
| 87 |
-
|
| 88 |
-
# Шаг 1: Embedding через VibeThinker
|
| 89 |
-
inputs = tokenizer(
|
| 90 |
-
text,
|
| 91 |
-
return_tensors="pt",
|
| 92 |
-
padding=True,
|
| 93 |
-
truncation=True,
|
| 94 |
-
max_length=128
|
| 95 |
-
).to(device)
|
| 96 |
-
|
| 97 |
-
with torch.no_grad():
|
| 98 |
-
outputs = model(**inputs)
|
| 99 |
-
embedding = outputs.last_hidden_state.mean(dim=1).cpu().numpy()[0]
|
| 100 |
-
|
| 101 |
-
# ИСПРАВЛЕНИЕ: Нормализация L2
|
| 102 |
-
from sklearn.preprocessing import normalize
|
| 103 |
-
embedding = normalize([embedding])[0]
|
| 104 |
-
|
| 105 |
-
if verbose:
|
| 106 |
-
print(f" 1️⃣ VibeThinker embedding: {len(embedding)}D (normalized)")
|
| 107 |
-
|
| 108 |
-
# Шаг 2: Вычисляем embeddings для обучающих данных
|
| 109 |
-
train_embeddings = []
|
| 110 |
-
train_labels = []
|
| 111 |
-
|
| 112 |
-
for train_text, label in TRAIN_DATA:
|
| 113 |
-
t_inputs = tokenizer(
|
| 114 |
-
train_text,
|
| 115 |
-
return_tensors="pt",
|
| 116 |
-
padding=True,
|
| 117 |
-
truncation=True,
|
| 118 |
-
max_length=128
|
| 119 |
-
).to(device)
|
| 120 |
-
|
| 121 |
-
with torch.no_grad():
|
| 122 |
-
t_outputs = model(**t_inputs)
|
| 123 |
-
t_emb = t_outputs.last_hidden_state.mean(dim=1).cpu().numpy()[0]
|
| 124 |
-
# ИСПРАВЛЕНИЕ: Нормализация
|
| 125 |
-
t_emb = normalize([t_emb])[0]
|
| 126 |
-
train_embeddings.append(t_emb)
|
| 127 |
-
train_labels.append(label)
|
| 128 |
-
|
| 129 |
-
# Шаг 3: Косинусное сходство (безопасное)
|
| 130 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 131 |
-
|
| 132 |
-
# ИСПРАВЛЕНИЕ: Клиппинг для избежания NaN
|
| 133 |
-
similarities = cosine_similarity([embedding], train_embeddings)[0]
|
| 134 |
-
similarities = np.clip(similarities, -1.0, 1.0) # Ограничиваем
|
| 135 |
-
|
| 136 |
-
if verbose:
|
| 137 |
-
print(f" 2️⃣ Квантовая похожесть вычислена")
|
| 138 |
-
|
| 139 |
-
# Шаг 4: Взвешенное голосование с проверкой
|
| 140 |
-
positive_scores = []
|
| 141 |
-
negative_scores = []
|
| 142 |
-
|
| 143 |
-
for i, sim in enumerate(similarities):
|
| 144 |
-
if np.isnan(sim): # ИСПРАВЛЕНИЕ: Проверка NaN
|
| 145 |
-
sim = 0.0
|
| 146 |
-
|
| 147 |
-
if train_labels[i] == 1:
|
| 148 |
-
positive_scores.append(sim)
|
| 149 |
-
else:
|
| 150 |
-
negative_scores.append(sim)
|
| 151 |
-
|
| 152 |
-
# ИСПРАВЛЕНИЕ: Используем среднее вместо суммы
|
| 153 |
-
positive_avg = np.mean(positive_scores) if positive_scores else 0
|
| 154 |
-
negative_avg = np.mean(negative_scores) if negative_scores else 0
|
| 155 |
-
|
| 156 |
-
# ИСПРАВЛЕНИЕ: Порог для нейтральных текстов
|
| 157 |
-
diff = positive_avg - negative_avg
|
| 158 |
-
|
| 159 |
-
if abs(diff) < 0.05: # Очень маленькая разница = нейтральный
|
| 160 |
-
prediction = -1 # Нейтральный
|
| 161 |
-
confidence = 0.0
|
| 162 |
-
sentiment = "😐 NEUTRAL"
|
| 163 |
-
elif positive_avg > negative_avg:
|
| 164 |
-
prediction = 1
|
| 165 |
-
confidence = abs(diff)
|
| 166 |
-
sentiment = "😊 POSITIVE"
|
| 167 |
-
else:
|
| 168 |
-
prediction = 0
|
| 169 |
-
confidence = abs(diff)
|
| 170 |
-
sentiment = "😞 NEGATIVE"
|
| 171 |
-
|
| 172 |
-
elapsed = time.time() - start
|
| 173 |
-
|
| 174 |
-
if verbose:
|
| 175 |
-
print(f" 3️⃣ Классификация: {sentiment}")
|
| 176 |
-
print(f" 💯 Уверенность: {confidence*100:.1f}%")
|
| 177 |
-
print(f" 📊 Positive avg: {positive_avg:.3f}, Negative avg: {negative_avg:.3f}")
|
| 178 |
-
print(f" ⏱️ Время: {elapsed:.2f}s")
|
| 179 |
-
print(f"{'='*70}")
|
| 180 |
-
|
| 181 |
-
return {
|
| 182 |
-
'prediction': prediction,
|
| 183 |
-
'sentiment': sentiment,
|
| 184 |
-
'confidence': confidence,
|
| 185 |
-
'time': elapsed,
|
| 186 |
-
'scores': {
|
| 187 |
-
'positive': float(positive_avg),
|
| 188 |
-
'negative': float(negative_avg)
|
| 189 |
-
}
|
| 190 |
-
}
|
| 191 |
-
# ДЕМО
|
| 192 |
-
print("\n" + "="*70)
|
| 193 |
-
print("🧪 ДЕМОНСТРАЦИЯ")
|
| 194 |
-
print("="*70)
|
| 195 |
-
|
| 196 |
-
demo_texts = [
|
| 197 |
-
"Quantum computing will revolutionize everything!",
|
| 198 |
-
"This is absolutely horrible, terrible quality.",
|
| 199 |
-
"I'm amazed by the quantum algorithms!",
|
| 200 |
-
"Worst purchase ever, complete waste."
|
| 201 |
-
]
|
| 202 |
-
|
| 203 |
-
print("\nТестирование на новых примерах:\n")
|
| 204 |
-
|
| 205 |
-
for text in demo_texts:
|
| 206 |
-
result = predict(text, verbose=False)
|
| 207 |
-
print(f"{result['sentiment']:<15} ({result['confidence']:>4.0%}) | {text[:50]}")
|
| 208 |
-
|
| 209 |
-
# ИНТЕРАКТИВНЫЙ РЕЖИМ
|
| 210 |
-
print("\n" + "="*70)
|
| 211 |
-
print("💬 ИНТЕРАКТИВНЫЙ РЕЖИМ")
|
| 212 |
-
print("="*70)
|
| 213 |
-
print("Введите текст для анализа (или 'exit' для выхода)\n")
|
| 214 |
-
|
| 215 |
-
while True:
|
| 216 |
-
try:
|
| 217 |
-
user_input = input("📝 Текст: ")
|
| 218 |
-
|
| 219 |
-
if user_input.lower() in ['exit', 'quit', 'q']:
|
| 220 |
-
print("\n👋 Завершение Hypnos-i3-1.5B")
|
| 221 |
-
break
|
| 222 |
-
|
| 223 |
-
if user_input.strip():
|
| 224 |
-
predict(user_input)
|
| 225 |
-
|
| 226 |
-
except KeyboardInterrupt:
|
| 227 |
-
print("\n\n👋 Завершение Hypnos-i3-1.5B")
|
| 228 |
-
break
|
| 229 |
-
except Exception as e:
|
| 230 |
-
print(f"❌ Ошибка: {e}")
|
| 231 |
-
|
| 232 |
-
print("\n" + "="*70)
|
| 233 |
-
print("✨ Спасибо за использование Hypnos-i3-1.5B!")
|
| 234 |
-
print("="*70)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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