File size: 2,218 Bytes
d57db39
46b1ccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d57db39
46b1ccd
 
 
 
 
 
 
 
 
 
d57db39
46b1ccd
 
d57db39
46b1ccd
d57db39
46b1ccd
 
 
 
 
 
 
 
 
 
 
 
d57db39
46b1ccd
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import os

# --- Model Yükleme ---
# Hugging Face Hub'dan GGUF modelini indirin.
# 'repo_id' kısmına kendi modelinizin repo ID'sini yazın.
# 'filename' kısmına GGUF dosyanızın tam adını yazın.
model_path = hf_hub_download(
    repo_id="yagiztan/purderma", 
    filename="unsloth.Q4_K_M.gguf",
    # Eğer özel (private) bir repo ise token kullanmanız gerekir.
    # Space'in ayarlarından (Settings -> Repository secrets) HF_TOKEN adında
    # bir secret oluşturup Hugging Face token'ınızı oraya ekleyebilirsiniz.
    # token=os.environ.get("HF_TOKEN") 
)

# Modeli llama-cpp-python ile yükleyin.
# n_ctx: Modelin aynı anda ne kadar metin işleyebileceği (context window).
# n_gpu_layers: Eğer GPU'lu bir Space kullanıyorsanız, kaç katmanın GPU'ya yükleneceğini belirtir.
#                CPU üzerinde çalışıyorsanız 0 olarak bırakın.
llm = Llama(
    model_path=model_path,
    n_ctx=4096,         # Context penceresini modelinize göre ayarlayın
    n_threads=8,        # Kullanılabilir CPU çekirdek sayısına göre ayarlayın
    n_gpu_layers=0      # CPU'da çalıştığı için 0
)

# --- Metin Üretme Fonksiyonu ---
def generate_text(prompt):
    """
    Kullanıcıdan gelen prompt'a göre modelden metin üretir.
    """
    output = llm(
        prompt,
        max_tokens=512,  # Üretilecek maksimum token sayısı
        echo=False,      # Kullanıcının girdiği prompt'u tekrar gösterme
        stop=["Human:", "\n"], # Bu ifadelerden birini gördüğünde üretimi durdur
    )
    
    # Modelin çıktısını al ve temizle
    generated_text = output['choices'][0]['text'].strip()
    return generated_text

# --- Gradio Arayüzü ---
with gr.Blocks() as demo:
    gr.Markdown("# GGUF Model Chatbot Arayüzü")
    gr.Markdown("Bu arayüz, Hugging Face'e yüklediğiniz GGUF modelini denemenizi sağlar.")
    
    inp = gr.Textbox(label="Sorunuzu veya komutunuzu girin", lines=4)
    out = gr.Textbox(label="Modelin Cevabı", lines=4)
    
    btn = gr.Button("Gönder")
    btn.click(fn=generate_text, inputs=inp, outputs=out)

# Arayüzü başlat
demo.launch()