Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,41 @@ from transformers import pipeline
|
|
| 3 |
import torch
|
| 4 |
from duckduckgo_search import DDGS
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
model_id = "
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def internetten_ara(soru):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def turkbot_cevap(mesaj, history):
|
| 16 |
bilgi = internetten_ara(mesaj)
|
|
|
|
| 17 |
prompt = f"<start_of_turn>user\nBilgi: {bilgi}\nSoru: {mesaj}<end_of_turn>\n<start_of_turn>model\n"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|
| 26 |
demo.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
from duckduckgo_search import DDGS
|
| 5 |
|
| 6 |
+
# GPU istemeyen, CPU üzerinde akıcı çalışan zeki model
|
| 7 |
+
model_id = "google/gemma-2-2b-it"
|
| 8 |
+
|
| 9 |
+
print("Turkbot CPU üzerinde uyanıyor... Lütfen bekleyin.")
|
| 10 |
+
|
| 11 |
+
# Device -1 demek 'Sadece CPU kullan' demektir, hata almanı engeller
|
| 12 |
+
pipe = pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=model_id,
|
| 15 |
+
device=-1,
|
| 16 |
+
torch_dtype=torch.float32 # CPU için en güvenli format
|
| 17 |
+
)
|
| 18 |
|
| 19 |
def internetten_ara(soru):
|
| 20 |
+
try:
|
| 21 |
+
with DDGS() as ddgs:
|
| 22 |
+
results = [r for r in ddgs.text(soru, max_results=2)]
|
| 23 |
+
return "\n".join([r['body'] for r in results])
|
| 24 |
+
except:
|
| 25 |
+
return ""
|
| 26 |
|
| 27 |
def turkbot_cevap(mesaj, history):
|
| 28 |
bilgi = internetten_ara(mesaj)
|
| 29 |
+
# RAG mantığıyla bilgiyi harmanlıyoruz
|
| 30 |
prompt = f"<start_of_turn>user\nBilgi: {bilgi}\nSoru: {mesaj}<end_of_turn>\n<start_of_turn>model\n"
|
| 31 |
|
| 32 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 33 |
+
cevap = outputs[0]['generated_text'].split("<start_of_turn>model\n")[-1]
|
| 34 |
+
return cevap.strip()
|
| 35 |
|
| 36 |
+
demo = gr.ChatInterface(
|
| 37 |
+
fn=turkbot_cevap,
|
| 38 |
+
title="🤖 TURKBOT CPU",
|
| 39 |
+
description="Ücretsiz sunucuda çalışan, internete bağlı akıllı asistan!"
|
| 40 |
+
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
demo.launch()
|