Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
-
🚀 Nova AI - Hugging Face Spaces
|
| 3 |
Teknova tarafından geliştirilen özgün yapay zeka modeli
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
@@ -8,6 +9,7 @@ import torch
|
|
| 8 |
import os
|
| 9 |
import logging
|
| 10 |
import time
|
|
|
|
| 11 |
from typing import List, Tuple
|
| 12 |
|
| 13 |
# Logging setup
|
|
@@ -83,9 +85,10 @@ def chat_response(message: str, history: List[List[str]], max_length: int, tempe
|
|
| 83 |
history.append([message, response])
|
| 84 |
return "", history
|
| 85 |
|
|
|
|
| 86 |
def generate_real_response(message: str, max_length: int, temperature: float) -> str:
|
| 87 |
"""
|
| 88 |
-
Gerçek model ile yanıt oluştur
|
| 89 |
"""
|
| 90 |
if not model or not tokenizer:
|
| 91 |
return generate_demo_response(message)
|
|
@@ -116,41 +119,44 @@ def generate_real_response(message: str, max_length: int, temperature: float) ->
|
|
| 116 |
logger.error(f"Model generation error: {e}")
|
| 117 |
return generate_demo_response(message)
|
| 118 |
|
|
|
|
| 119 |
def load_model():
|
| 120 |
"""
|
| 121 |
-
Teknova Nova AI modelini yükle
|
| 122 |
"""
|
| 123 |
global model, tokenizer
|
| 124 |
|
| 125 |
try:
|
| 126 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 127 |
|
| 128 |
-
# Model path -
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
quantization_config = BitsAndBytesConfig(
|
| 135 |
-
load_in_4bit=True,
|
| 136 |
-
bnb_4bit_compute_dtype=torch.float16,
|
| 137 |
-
bnb_4bit_use_double_quant=True,
|
| 138 |
-
bnb_4bit_quant_type="nf4"
|
| 139 |
-
)
|
| 140 |
|
| 141 |
# Tokenizer yükle
|
| 142 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 143 |
-
|
| 144 |
-
trust_remote_code=True
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
model = AutoModelForCausalLM.from_pretrained(
|
| 149 |
-
|
| 150 |
-
quantization_config=quantization_config,
|
| 151 |
device_map="auto",
|
| 152 |
trust_remote_code=True,
|
| 153 |
-
torch_dtype=torch.float16
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
logger.info(f"✅ Nova AI model yüklendi ({device})")
|
|
@@ -158,16 +164,18 @@ def load_model():
|
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
logger.error(f"Model yükleme hatası: {e}")
|
| 161 |
-
return f"❌ Model yüklenemedi: {str(e)}"
|
| 162 |
|
|
|
|
| 163 |
def chat_with_nova(message: str, history: List[Tuple[str, str]]) -> str:
|
| 164 |
"""
|
| 165 |
-
Nova AI ile sohbet et
|
| 166 |
"""
|
| 167 |
global model, tokenizer
|
| 168 |
|
| 169 |
if model is None or tokenizer is None:
|
| 170 |
-
|
|
|
|
| 171 |
|
| 172 |
if not message.strip():
|
| 173 |
return "❓ Lütfen bir mesaj yazın."
|
|
@@ -176,7 +184,8 @@ def chat_with_nova(message: str, history: List[Tuple[str, str]]) -> str:
|
|
| 176 |
# Konuşma geçmişini hazırla
|
| 177 |
conversation = ""
|
| 178 |
for user_msg, bot_msg in history:
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
# Yeni mesajı ekle
|
| 182 |
conversation += f"Kullanıcı: {message}\nNova AI:"
|
|
@@ -186,8 +195,13 @@ def chat_with_nova(message: str, history: List[Tuple[str, str]]) -> str:
|
|
| 186 |
conversation,
|
| 187 |
return_tensors="pt",
|
| 188 |
truncation=True,
|
| 189 |
-
max_length=2048
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
# Yanıt üret
|
| 193 |
with torch.no_grad():
|
|
@@ -197,8 +211,9 @@ def chat_with_nova(message: str, history: List[Tuple[str, str]]) -> str:
|
|
| 197 |
temperature=0.7,
|
| 198 |
top_p=0.9,
|
| 199 |
do_sample=True,
|
| 200 |
-
pad_token_id=tokenizer.
|
| 201 |
-
eos_token_id=tokenizer.eos_token_id
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
# Yanıtı decode et
|
|
@@ -221,7 +236,7 @@ logger.info(f"Model durumu: {load_status}")
|
|
| 221 |
# Gradio arayüzü
|
| 222 |
with gr.Blocks(
|
| 223 |
theme=gr.themes.Soft(),
|
| 224 |
-
title="Teknova Nova AI",
|
| 225 |
css="""
|
| 226 |
.gradio-container {
|
| 227 |
max-width: 900px;
|
|
@@ -248,7 +263,7 @@ with gr.Blocks(
|
|
| 248 |
Türkiye'nin Özgün Yapay Zeka Modeli
|
| 249 |
</p>
|
| 250 |
<div style="background: rgba(255,255,255,0.2); padding: 8px 16px; border-radius: 20px; display: inline-block; font-size: 0.9rem;">
|
| 251 |
-
🧠
|
| 252 |
</div>
|
| 253 |
</div>
|
| 254 |
""")
|
|
@@ -293,7 +308,7 @@ with gr.Blocks(
|
|
| 293 |
gr.HTML("""
|
| 294 |
<div style="text-align: center; padding: 15px; color: #666; font-size: 0.9rem;">
|
| 295 |
<p>🌟 <strong>Teknova Nova AI</strong> - Tamamen özgün Türkçe dil modeli</p>
|
| 296 |
-
<p>
|
| 297 |
<p style="color: #999;">⚡ Teknova Innovation ile güçlendirilmiştir</p>
|
| 298 |
</div>
|
| 299 |
""")
|
|
@@ -302,14 +317,18 @@ with gr.Blocks(
|
|
| 302 |
def user_message(message, history):
|
| 303 |
return "", history + [[message, None]]
|
| 304 |
|
|
|
|
| 305 |
def bot_message(history):
|
|
|
|
| 306 |
if history and history[-1][1] is None:
|
| 307 |
user_msg = history[-1][0]
|
| 308 |
bot_response = chat_with_nova(user_msg, history[:-1])
|
| 309 |
history[-1][1] = bot_response
|
| 310 |
return history
|
| 311 |
|
|
|
|
| 312 |
def retry_last(history):
|
|
|
|
| 313 |
if history and history[-1][1] is not None:
|
| 314 |
user_msg = history[-1][0]
|
| 315 |
bot_response = chat_with_nova(user_msg, history[:-1])
|
|
|
|
| 1 |
"""
|
| 2 |
+
🚀 Nova AI - Hugging Face Spaces ZeroGPU
|
| 3 |
Teknova tarafından geliştirilen özgün yapay zeka modeli
|
| 4 |
+
ZeroGPU ile hızlandırılmış
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
| 9 |
import os
|
| 10 |
import logging
|
| 11 |
import time
|
| 12 |
+
import spaces
|
| 13 |
from typing import List, Tuple
|
| 14 |
|
| 15 |
# Logging setup
|
|
|
|
| 85 |
history.append([message, response])
|
| 86 |
return "", history
|
| 87 |
|
| 88 |
+
@spaces.GPU
|
| 89 |
def generate_real_response(message: str, max_length: int, temperature: float) -> str:
|
| 90 |
"""
|
| 91 |
+
Gerçek model ile yanıt oluştur - ZeroGPU destekli
|
| 92 |
"""
|
| 93 |
if not model or not tokenizer:
|
| 94 |
return generate_demo_response(message)
|
|
|
|
| 119 |
logger.error(f"Model generation error: {e}")
|
| 120 |
return generate_demo_response(message)
|
| 121 |
|
| 122 |
+
@spaces.GPU
|
| 123 |
def load_model():
|
| 124 |
"""
|
| 125 |
+
Teknova Nova AI modelini yükle - ZeroGPU destekli
|
| 126 |
"""
|
| 127 |
global model, tokenizer
|
| 128 |
|
| 129 |
try:
|
| 130 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 131 |
|
| 132 |
+
# Model path - Local model kullan
|
| 133 |
+
model_path = "./nova-ai-small"
|
| 134 |
|
| 135 |
+
# Eğer local model yoksa, demo modunda çalış
|
| 136 |
+
if not os.path.exists(model_path):
|
| 137 |
+
logger.info("⚠️ Local model bulunamadı, demo modunda çalışacak")
|
| 138 |
+
return "⚠️ Demo modunda çalışıyor"
|
| 139 |
|
| 140 |
+
logger.info(f"🚀 Teknova Nova AI modeli yükleniyor: {model_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# Tokenizer yükle
|
| 143 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 144 |
+
model_path,
|
| 145 |
+
trust_remote_code=True,
|
| 146 |
+
use_fast=True
|
| 147 |
)
|
| 148 |
|
| 149 |
+
# Pad token ayarla
|
| 150 |
+
if tokenizer.pad_token is None:
|
| 151 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 152 |
+
|
| 153 |
+
# Model yükle - ZeroGPU optimizasyonu
|
| 154 |
model = AutoModelForCausalLM.from_pretrained(
|
| 155 |
+
model_path,
|
|
|
|
| 156 |
device_map="auto",
|
| 157 |
trust_remote_code=True,
|
| 158 |
+
torch_dtype=torch.float16,
|
| 159 |
+
low_cpu_mem_usage=True
|
| 160 |
)
|
| 161 |
|
| 162 |
logger.info(f"✅ Nova AI model yüklendi ({device})")
|
|
|
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
logger.error(f"Model yükleme hatası: {e}")
|
| 167 |
+
return f"❌ Model yüklenemedi: {str(e)} - Demo modunda çalışacak"
|
| 168 |
|
| 169 |
+
@spaces.GPU
|
| 170 |
def chat_with_nova(message: str, history: List[Tuple[str, str]]) -> str:
|
| 171 |
"""
|
| 172 |
+
Nova AI ile sohbet et - ZeroGPU destekli
|
| 173 |
"""
|
| 174 |
global model, tokenizer
|
| 175 |
|
| 176 |
if model is None or tokenizer is None:
|
| 177 |
+
# Demo yanıt ver
|
| 178 |
+
return generate_demo_response(message) + "\n\n💡 *Not: Demo modunda çalışıyor.*"
|
| 179 |
|
| 180 |
if not message.strip():
|
| 181 |
return "❓ Lütfen bir mesaj yazın."
|
|
|
|
| 184 |
# Konuşma geçmişini hazırla
|
| 185 |
conversation = ""
|
| 186 |
for user_msg, bot_msg in history:
|
| 187 |
+
if user_msg and bot_msg:
|
| 188 |
+
conversation += f"Kullanıcı: {user_msg}\nNova AI: {bot_msg}\n"
|
| 189 |
|
| 190 |
# Yeni mesajı ekle
|
| 191 |
conversation += f"Kullanıcı: {message}\nNova AI:"
|
|
|
|
| 195 |
conversation,
|
| 196 |
return_tensors="pt",
|
| 197 |
truncation=True,
|
| 198 |
+
max_length=2048,
|
| 199 |
+
padding=True
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# GPU'ya taşı
|
| 203 |
+
if torch.cuda.is_available():
|
| 204 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 205 |
|
| 206 |
# Yanıt üret
|
| 207 |
with torch.no_grad():
|
|
|
|
| 211 |
temperature=0.7,
|
| 212 |
top_p=0.9,
|
| 213 |
do_sample=True,
|
| 214 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 215 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 216 |
+
repetition_penalty=1.1
|
| 217 |
)
|
| 218 |
|
| 219 |
# Yanıtı decode et
|
|
|
|
| 236 |
# Gradio arayüzü
|
| 237 |
with gr.Blocks(
|
| 238 |
theme=gr.themes.Soft(),
|
| 239 |
+
title="Teknova Nova AI - ZeroGPU",
|
| 240 |
css="""
|
| 241 |
.gradio-container {
|
| 242 |
max-width: 900px;
|
|
|
|
| 263 |
Türkiye'nin Özgün Yapay Zeka Modeli
|
| 264 |
</p>
|
| 265 |
<div style="background: rgba(255,255,255,0.2); padding: 8px 16px; border-radius: 20px; display: inline-block; font-size: 0.9rem;">
|
| 266 |
+
🧠 Nova AI • 🇹�� Türkçe Optimize • ⚡ ZeroGPU Hızlı
|
| 267 |
</div>
|
| 268 |
</div>
|
| 269 |
""")
|
|
|
|
| 308 |
gr.HTML("""
|
| 309 |
<div style="text-align: center; padding: 15px; color: #666; font-size: 0.9rem;">
|
| 310 |
<p>🌟 <strong>Teknova Nova AI</strong> - Tamamen özgün Türkçe dil modeli</p>
|
| 311 |
+
<p>🚀 ZeroGPU teknolojisi ile hızlandırılmış</p>
|
| 312 |
<p style="color: #999;">⚡ Teknova Innovation ile güçlendirilmiştir</p>
|
| 313 |
</div>
|
| 314 |
""")
|
|
|
|
| 317 |
def user_message(message, history):
|
| 318 |
return "", history + [[message, None]]
|
| 319 |
|
| 320 |
+
@spaces.GPU
|
| 321 |
def bot_message(history):
|
| 322 |
+
"""Bot yanıtı oluştur - ZeroGPU destekli"""
|
| 323 |
if history and history[-1][1] is None:
|
| 324 |
user_msg = history[-1][0]
|
| 325 |
bot_response = chat_with_nova(user_msg, history[:-1])
|
| 326 |
history[-1][1] = bot_response
|
| 327 |
return history
|
| 328 |
|
| 329 |
+
@spaces.GPU
|
| 330 |
def retry_last(history):
|
| 331 |
+
"""Son mesajı yeniden dene - ZeroGPU destekli"""
|
| 332 |
if history and history[-1][1] is not None:
|
| 333 |
user_msg = history[-1][0]
|
| 334 |
bot_response = chat_with_nova(user_msg, history[:-1])
|