Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,37 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import subprocess
|
| 3 |
-
import sys
|
| 4 |
-
import importlib
|
| 5 |
-
|
| 6 |
-
# Gerekli paketleri kontrol et, yoksa yükle
|
| 7 |
-
def install_if_missing(package):
|
| 8 |
-
try:
|
| 9 |
-
importlib.import_module(package)
|
| 10 |
-
except ImportError:
|
| 11 |
-
print(f"{package} bulunamadı, yükleniyor...")
|
| 12 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 13 |
-
|
| 14 |
-
install_if_missing("gdown")
|
| 15 |
-
install_if_missing("gradio")
|
| 16 |
-
|
| 17 |
import gradio as gr
|
| 18 |
-
import
|
| 19 |
-
import
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
def generate(prompt):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"-p", prompt,
|
| 40 |
-
"-n", "128", # max token
|
| 41 |
-
"-t", "4" # CPU thread sayısı
|
| 42 |
-
]
|
| 43 |
-
|
| 44 |
-
try:
|
| 45 |
-
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 46 |
-
output = result.stdout
|
| 47 |
-
return output
|
| 48 |
-
except Exception as e:
|
| 49 |
-
return f"Hata: {e}"
|
| 50 |
|
| 51 |
# Gradio arayüzü
|
| 52 |
iface = gr.Interface(
|
| 53 |
fn=generate,
|
| 54 |
-
inputs=gr.Textbox(lines=3, placeholder="Prompt
|
| 55 |
outputs="text",
|
| 56 |
title="Alpaca-7B Q4 CPU",
|
| 57 |
-
description="
|
| 58 |
)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
if __name__ == "__main__":
|
| 62 |
iface.launch()
|
|
|
|
| 1 |
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Model ID Hugging Face üzerinde
|
| 7 |
+
MODEL_ID = "GoshawkVortexAI/ggml-alpaca-7b-q4"
|
| 8 |
+
|
| 9 |
+
# Tokenizer ve modeli yükle (CPU için)
|
| 10 |
+
print("Model yükleniyor... Bu işlem biraz zaman alabilir.")
|
| 11 |
+
tokenizer = LlamaTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
+
model = LlamaForCausalLM.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
device_map="cpu", # CPU kullan
|
| 15 |
+
torch_dtype=torch.float32 # CPU için uygun dtype
|
| 16 |
+
)
|
| 17 |
+
print("Model yüklendi.")
|
| 18 |
|
| 19 |
+
# Tahmin fonksiyonu
|
| 20 |
def generate(prompt):
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 22 |
+
outputs = model.generate(**inputs, max_new_tokens=128)
|
| 23 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Gradio arayüzü
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=generate,
|
| 29 |
+
inputs=gr.Textbox(lines=3, placeholder="Prompt yazın..."),
|
| 30 |
outputs="text",
|
| 31 |
title="Alpaca-7B Q4 CPU",
|
| 32 |
+
description="Hugging Face üzerinden CPU'da çalışan Alpaca modeline prompt girin."
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Başlat
|
| 36 |
if __name__ == "__main__":
|
| 37 |
iface.launch()
|