38233-vm/core/utils.py
2026-02-06 00:30:42 +00:00

76 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from pypdf import PdfReader
from ai.local_ai_api import LocalAIApi
import json
def extract_text_from_pdf(pdf_path):
try:
reader = PdfReader(pdf_path)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
if not text.strip():
# If no text extracted, maybe it's an image-based PDF
# In a real environment we would use OCR here
return None
return text
except Exception as e:
print(f"Error extracting text from PDF: {e}")
return None
def analyze_invoice_text(text):
prompt = f"""
Sen profesyonel bir fatura veri ayıklama sistemisin. Aşağıdaki fatura metnini analiz et ve bilgileri kesinlikle JSON formatında döndür.
Özellikle şu bilgileri bulmaya çalış:
- Satıcı Firma Bilgileri: Adı, Vergi Kimlik Numarası (VKN), MERSİS Numarası, Adresi.
- Fatura Bilgileri: Fatura No, Tarih, Ara Toplam, KDV Tutarı, Genel Toplam.
- Satır Kalemleri: Her bir ürün veya hizmetin adı, adedi, birim fiyatı, KDV oranı, KDV tutarı ve toplam tutarı.
Dikkat:
- Vergi numarası 10 haneli (VKN) veya 11 haneli (TCKN) olabilir.
- MERSİS no genellikle 16 hanelidir.
- Sayısal değerleri (tutarlar, adetler) sadece rakam ve nokta (ondalık için) olarak döndür.
- Tarihi YYYY-MM-DD formatına çevir.
- Eğer bir veri bulunamazsa null döndür.
İstenen JSON formatı:
{{
"firma_adi": "...",
"vergi_no": "...",
"mersis_no": "...",
"adres": "...",
"fatura_no": "...",
"tarih": "YYYY-MM-DD",
"ara_toplam": 0.00,
"kdv_toplam": 0.00,
"genel_toplam": 0.00,
"kalemler": [
{{
"urun_adi": "...",
"adet": 1,
"birim_fiyat": 0.00,
"kdv_orani": 20,
"kdv_tutari": 0.00,
"toplam_tutar": 0.00
}}
]
}}
Fatura Metni:
{text}
"""
response = LocalAIApi.create_response({
"input": [
{"role": "system", "content": "Sen sadece JSON döndüren, hata payı düşük bir fatura analiz uzmanısın. Yanıtında asla JSON dışında metin bulundurma."},
{"role": "user", "content": prompt},
],
"text": {"format": {"type": "json_object"}},
})
if response.get("success"):
return LocalAIApi.decode_json_from_response(response)
return None