File size: 1,316 Bytes
ab317cb
 
 
 
 
5c39c45
 
 
 
 
 
 
 
 
 
 
 
ab317cb
 
5c39c45
 
 
 
 
 
ab317cb
 
 
5c39c45
ab317cb
 
5c39c45
 
 
ab317cb
5c39c45
 
 
 
 
ab317cb
 
 
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
import gradio as gr
from transformers import pipeline
import torch
from duckduckgo_search import DDGS

# GPU istemeyen, CPU üzerinde akıcı çalışan zeki model
model_id = "google/gemma-2-2b-it" 

print("Turkbot CPU üzerinde uyanıyor... Lütfen bekleyin.")

# Device -1 demek 'Sadece CPU kullan' demektir, hata almanı engeller
pipe = pipeline(
    "text-generation", 
    model=model_id, 
    device=-1, 
    torch_dtype=torch.float32 # CPU için en güvenli format
)

def internetten_ara(soru):
    try:
        with DDGS() as ddgs:
            results = [r for r in ddgs.text(soru, max_results=2)]
            return "\n".join([r['body'] for r in results])
    except:
        return ""

def turkbot_cevap(mesaj, history):
    bilgi = internetten_ara(mesaj)
    # RAG mantığıyla bilgiyi harmanlıyoruz
    prompt = f"<start_of_turn>user\nBilgi: {bilgi}\nSoru: {mesaj}<end_of_turn>\n<start_of_turn>model\n"
    
    outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
    cevap = outputs[0]['generated_text'].split("<start_of_turn>model\n")[-1]
    return cevap.strip()

demo = gr.ChatInterface(
    fn=turkbot_cevap, 
    title="🤖 TURKBOT CPU", 
    description="Ücretsiz sunucuda çalışan, internete bağlı akıllı asistan!"
)

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