Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
# 📌 Hugging Face Token (Hacı)
|
7 |
+
HF_TOKEN = "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" # Senin tokenin
|
8 |
+
|
9 |
+
# 📌 Gerekli modülleri kontrol et ve yükle
|
10 |
+
required_modules = ["torch", "transformers"]
|
11 |
+
for module in required_modules:
|
12 |
+
try:
|
13 |
+
__import__(module)
|
14 |
+
print(f"✅ {module} zaten yüklü.")
|
15 |
+
except ImportError:
|
16 |
+
print(f"⚠️ {module} eksik! Kuruluyor... 🛠️")
|
17 |
+
subprocess.run(["pip", "install", module], check=True)
|
18 |
+
print(f"✅ {module} başarıyla kuruldu!")
|
19 |
+
|
20 |
+
# 📌 GPU Kontrolü
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
print(f"✅ Kullanılan Cihaz: {device}")
|
23 |
+
|
24 |
+
# 📌 Qwen Modeli Yükleme
|
25 |
+
MODEL_NAME = "Qwen/Qwen2.5-Math-1.5B"
|
26 |
+
|
27 |
+
try:
|
28 |
+
print(f"📌 {MODEL_NAME} modeli indiriliyor ve yükleniyor...")
|
29 |
+
|
30 |
+
# 🔥 trust_remote_code=True EKLEDİK! 🔥
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN, trust_remote_code=True)
|
32 |
+
|
33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
34 |
+
MODEL_NAME,
|
35 |
+
token=HF_TOKEN,
|
36 |
+
trust_remote_code=True, # 🔥 Bu kritik! 🔥
|
37 |
+
torch_dtype=torch.float16,
|
38 |
+
device_map="auto"
|
39 |
+
)
|
40 |
+
|
41 |
+
print("✅ Model başarıyla yüklendi!")
|
42 |
+
except Exception as e:
|
43 |
+
print(f"⚠️ Model yükleme başarısız! Hata: {e}")
|
44 |
+
print("📌 Çözüm: Hugging Face tokenini ve model erişimini kontrol et.")
|
45 |
+
|
46 |
+
# 📌 Model Etkileşimi
|
47 |
+
while True:
|
48 |
+
user_input = input("👤 Sen: ")
|
49 |
+
if user_input.lower() in ["exit", "çıkış"]:
|
50 |
+
print("🔄 Kapatılıyor...")
|
51 |
+
break
|
52 |
+
|
53 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
54 |
+
outputs = model.generate(**inputs, max_length=100)
|
55 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
56 |
+
print(f"🤖 Bot: {response}")
|