| | 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() |
| |
|