File size: 1,483 Bytes
576bbd4
1c8cec8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576bbd4
a1b6f75
 
 
 
 
 
 
1c8cec8
a1b6f75
 
 
1c8cec8
 
a1b6f75
 
576bbd4
a1b6f75
576bbd4
a1b6f75
 
 
 
576bbd4
 
 
 
a1b6f75
576bbd4
 
a1b6f75
576bbd4
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# app.py
import subprocess
import sys
import importlib

# Paketleri kontrol et, yoksa yükle
def install_if_missing(package):
    try:
        importlib.import_module(package)
    except ImportError:
        print(f"{package} bulunamadı, yükleniyor...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])

# Gerekli paketler
install_if_missing("transformers")
install_if_missing("torch")
install_if_missing("gradio")

# Şimdi import edebiliriz
import gradio as gr
from transformers import LlamaForCausalLM, LlamaTokenizer
import torch

# Model ID Hugging Face üzerinde
MODEL_ID = "GoshawkVortexAI/ggml-alpaca-7b-q4"

# Tokenizer ve modeli yükle (CPU için)
print("Model yükleniyor... Bu biraz zaman alabilir.")
tokenizer = LlamaTokenizer.from_pretrained(MODEL_ID)
model = LlamaForCausalLM.from_pretrained(
    MODEL_ID,
    device_map="cpu",
    torch_dtype=torch.float32
)
print("Model yüklendi.")

# Tahmin fonksiyonu
def generate(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs, max_new_tokens=128)
    text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return text

# Gradio arayüzü
iface = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(lines=3, placeholder="Prompt yazın..."),
    outputs="text",
    title="Alpaca-7B Q4 CPU",
    description="Hugging Face üzerinden CPU'da çalışan Alpaca modeline prompt girin."
)

if __name__ == "__main__":
    iface.launch()