turkbot commited on
Commit
5c39c45
·
verified ·
1 Parent(s): 2843ff4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -3,24 +3,41 @@ from transformers import pipeline
3
  import torch
4
  from duckduckgo_search import DDGS
5
 
6
- # Modeli yükle (Hugging Face sunucularında çalışacak)
7
- model_id = "unsloth/gemma-2-9b-it-bnb-4bit"
8
- pipe = pipeline("text-generation", model=model_id, device_map="auto", model_kwargs={"torch_dtype": torch.bfloat16})
 
 
 
 
 
 
 
 
 
9
 
10
  def internetten_ara(soru):
11
- with DDGS() as ddgs:
12
- results = [r for r in ddgs.text(soru, max_results=3)]
13
- return "\n".join([r['body'] for r in results])
 
 
 
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
- cevap = pipe(prompt, max_new_tokens=300)[0]['generated_text']
20
- return cevap.split("<start_of_turn>model\n")[-1]
 
21
 
22
- # Harika bir kullanıcı arayüzü
23
- demo = gr.ChatInterface(fn=turkbot_cevap, title="TURKBOT v1.0", description="İnternete bağlı, canlı Turkbot!")
 
 
 
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()