Update zayn.py
Browse files
zayn.py
CHANGED
|
@@ -1,85 +1,85 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import fitz
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
from functools import lru_cache
|
| 5 |
-
from typing import Optional, List
|
| 6 |
-
|
| 7 |
-
API_KEY = "
|
| 8 |
-
genai.configure(api_key=API_KEY)
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 13 |
-
except Exception as e:
|
| 14 |
-
print(f"Model başlatılamadı: {e}")
|
| 15 |
-
exit()
|
| 16 |
-
|
| 17 |
-
@lru_cache(maxsize=10)
|
| 18 |
-
def extract_text_from_pdf(pdf_path: str) -> Optional[str]:
|
| 19 |
-
if not os.path.exists(pdf_path):
|
| 20 |
-
return None
|
| 21 |
-
try:
|
| 22 |
-
doc = fitz.open(pdf_path)
|
| 23 |
-
text = "\n".join(page.get_text("text") for page in doc)
|
| 24 |
-
return text.strip() if text else None
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"PDF işlenirken hata oluştu: {e}")
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
def extract_text_from_multiple_pdfs(pdf_paths: List[str]) -> Optional[str]:
|
| 30 |
-
texts = []
|
| 31 |
-
for path in pdf_paths:
|
| 32 |
-
text = extract_text_from_pdf(path)
|
| 33 |
-
if text:
|
| 34 |
-
texts.append(text)
|
| 35 |
-
return "\n".join(texts) if texts else None
|
| 36 |
-
|
| 37 |
-
def build_prompt(context: str, request: str) -> str:
|
| 38 |
-
req_lower = request.lower()
|
| 39 |
-
if "özetle" in req_lower:
|
| 40 |
-
return f"Metni kısaca özetle:\n\n{context}"
|
| 41 |
-
elif "çevir" in req_lower:
|
| 42 |
-
return f"Metni İngilizce'ye çevir:\n\n{context}"
|
| 43 |
-
else:
|
| 44 |
-
return f"Bağlam: {context}\n\nKullanıcı: {request}\n\nLütfen uygun yanıtı oluştur."
|
| 45 |
-
|
| 46 |
-
def process_request(context: str, request: str) -> str:
|
| 47 |
-
prompt = build_prompt(context, request)
|
| 48 |
-
try:
|
| 49 |
-
response = model.generate_content(prompt)
|
| 50 |
-
return response.text.strip()
|
| 51 |
-
except Exception as e:
|
| 52 |
-
return f"Model hata: {e}"
|
| 53 |
-
|
| 54 |
-
def main():
|
| 55 |
-
print("NotebookLM-ZaynAI")
|
| 56 |
-
|
| 57 |
-
while True:
|
| 58 |
-
pdf_input = input("PDF dosyasının yolunu girin: ").strip()
|
| 59 |
-
if not pdf_input:
|
| 60 |
-
print("Lütfen geçerli dosya yolu girin.")
|
| 61 |
-
continue
|
| 62 |
-
|
| 63 |
-
pdf_paths = [path.strip() for path in pdf_input.split(",")]
|
| 64 |
-
context = extract_text_from_multiple_pdfs(pdf_paths)
|
| 65 |
-
if context:
|
| 66 |
-
break
|
| 67 |
-
print("PDF dosyalarından metin çıkarılamadı, lütfen dosya yollarını kontrol edin.")
|
| 68 |
-
|
| 69 |
-
print("Çık yazarak ZaynAI'dan çıkabilirsiniz.")
|
| 70 |
-
|
| 71 |
-
while True:
|
| 72 |
-
user_request = input("Soru/Talimat: ").strip()
|
| 73 |
-
if user_request.lower() == "çık":
|
| 74 |
-
print("Çıkılıyor...")
|
| 75 |
-
break
|
| 76 |
-
if not user_request:
|
| 77 |
-
continue
|
| 78 |
-
|
| 79 |
-
answer = process_request(context, user_request)
|
| 80 |
-
print("\nYanıt:")
|
| 81 |
-
print(answer)
|
| 82 |
-
print()
|
| 83 |
-
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
-
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import fitz
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from functools import lru_cache
|
| 5 |
+
from typing import Optional, List
|
| 6 |
+
|
| 7 |
+
API_KEY = "YOUR GEMINI API KEY"
|
| 8 |
+
genai.configure(api_key=API_KEY)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Model başlatılamadı: {e}")
|
| 15 |
+
exit()
|
| 16 |
+
|
| 17 |
+
@lru_cache(maxsize=10)
|
| 18 |
+
def extract_text_from_pdf(pdf_path: str) -> Optional[str]:
|
| 19 |
+
if not os.path.exists(pdf_path):
|
| 20 |
+
return None
|
| 21 |
+
try:
|
| 22 |
+
doc = fitz.open(pdf_path)
|
| 23 |
+
text = "\n".join(page.get_text("text") for page in doc)
|
| 24 |
+
return text.strip() if text else None
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"PDF işlenirken hata oluştu: {e}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
def extract_text_from_multiple_pdfs(pdf_paths: List[str]) -> Optional[str]:
|
| 30 |
+
texts = []
|
| 31 |
+
for path in pdf_paths:
|
| 32 |
+
text = extract_text_from_pdf(path)
|
| 33 |
+
if text:
|
| 34 |
+
texts.append(text)
|
| 35 |
+
return "\n".join(texts) if texts else None
|
| 36 |
+
|
| 37 |
+
def build_prompt(context: str, request: str) -> str:
|
| 38 |
+
req_lower = request.lower()
|
| 39 |
+
if "özetle" in req_lower:
|
| 40 |
+
return f"Metni kısaca özetle:\n\n{context}"
|
| 41 |
+
elif "çevir" in req_lower:
|
| 42 |
+
return f"Metni İngilizce'ye çevir:\n\n{context}"
|
| 43 |
+
else:
|
| 44 |
+
return f"Bağlam: {context}\n\nKullanıcı: {request}\n\nLütfen uygun yanıtı oluştur."
|
| 45 |
+
|
| 46 |
+
def process_request(context: str, request: str) -> str:
|
| 47 |
+
prompt = build_prompt(context, request)
|
| 48 |
+
try:
|
| 49 |
+
response = model.generate_content(prompt)
|
| 50 |
+
return response.text.strip()
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Model hata: {e}"
|
| 53 |
+
|
| 54 |
+
def main():
|
| 55 |
+
print("NotebookLM-ZaynAI")
|
| 56 |
+
|
| 57 |
+
while True:
|
| 58 |
+
pdf_input = input("PDF dosyasının yolunu girin: ").strip()
|
| 59 |
+
if not pdf_input:
|
| 60 |
+
print("Lütfen geçerli dosya yolu girin.")
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
pdf_paths = [path.strip() for path in pdf_input.split(",")]
|
| 64 |
+
context = extract_text_from_multiple_pdfs(pdf_paths)
|
| 65 |
+
if context:
|
| 66 |
+
break
|
| 67 |
+
print("PDF dosyalarından metin çıkarılamadı, lütfen dosya yollarını kontrol edin.")
|
| 68 |
+
|
| 69 |
+
print("Çık yazarak ZaynAI'dan çıkabilirsiniz.")
|
| 70 |
+
|
| 71 |
+
while True:
|
| 72 |
+
user_request = input("Soru/Talimat: ").strip()
|
| 73 |
+
if user_request.lower() == "çık":
|
| 74 |
+
print("Çıkılıyor...")
|
| 75 |
+
break
|
| 76 |
+
if not user_request:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
answer = process_request(context, user_request)
|
| 80 |
+
print("\nYanıt:")
|
| 81 |
+
print(answer)
|
| 82 |
+
print()
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|