File size: 2,574 Bytes
5792b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import fitz
import google.generativeai as genai
from functools import lru_cache
from typing import Optional, List

API_KEY = "YOUR GEMINI API KEY"
genai.configure(api_key=API_KEY)


try:
    model = genai.GenerativeModel('gemini-2.0-flash')
except Exception as e:
    print(f"Model başlatılamadı: {e}")
    exit()

@lru_cache(maxsize=10)
def extract_text_from_pdf(pdf_path: str) -> Optional[str]:
    if not os.path.exists(pdf_path):
        return None
    try:
        doc = fitz.open(pdf_path)
        text = "\n".join(page.get_text("text") for page in doc)
        return text.strip() if text else None
    except Exception as e:
        print(f"PDF işlenirken hata oluştu: {e}")
        return None

def extract_text_from_multiple_pdfs(pdf_paths: List[str]) -> Optional[str]:
    texts = []
    for path in pdf_paths:
        text = extract_text_from_pdf(path)
        if text:
            texts.append(text)
    return "\n".join(texts) if texts else None

def build_prompt(context: str, request: str) -> str:
    req_lower = request.lower()
    if "özetle" in req_lower:
        return f"Metni kısaca özetle:\n\n{context}"
    elif "çevir" in req_lower:
        return f"Metni İngilizce'ye çevir:\n\n{context}"
    else:
        return f"Bağlam: {context}\n\nKullanıcı: {request}\n\nLütfen uygun yanıtı oluştur."

def process_request(context: str, request: str) -> str:
    prompt = build_prompt(context, request)
    try:
        response = model.generate_content(prompt)
        return response.text.strip()
    except Exception as e:
        return f"Model hata: {e}"

def main():
    print("NotebookLM-ZaynAI")
    
    while True:
        pdf_input = input("PDF dosyasının yolunu girin: ").strip()
        if not pdf_input:
            print("Lütfen geçerli dosya yolu girin.")
            continue
        
        pdf_paths = [path.strip() for path in pdf_input.split(",")]
        context = extract_text_from_multiple_pdfs(pdf_paths)
        if context:
            break
        print("PDF dosyalarından metin çıkarılamadı, lütfen dosya yollarını kontrol edin.")
    
    print("Çık yazarak ZaynAI'dan çıkabilirsiniz.")
    
    while True:
        user_request = input("Soru/Talimat: ").strip()
        if user_request.lower() == "çık":
            print("Çıkılıyor...")
            break
        if not user_request:
            continue
        
        answer = process_request(context, user_request)
        print("\nYanıt:")
        print(answer)
        print()

if __name__ == "__main__":
    main()