File size: 4,116 Bytes
8223b74 |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import os
import json
import pytesseract
from PIL import Image
from pdf2image import convert_from_path
import docx
from datetime import datetime
from sqlalchemy.orm import Session
from app.db.models import Document
from app.agent import create_agent
# Desteklenen dosya türleri
SUPPORTED_CONTENT_TYPES = {
'application/pdf': 'pdf',
'image/jpeg': 'image',
'image/png': 'image',
'image/tiff': 'image',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx'
}
# Dosya yükleme dizini
UPLOAD_DIR = os.path.join(os.getcwd(), 'uploads')
os.makedirs(UPLOAD_DIR, exist_ok=True)
def save_uploaded_file(file, filename):
"""Yüklenen dosyayı kaydet"""
file_path = os.path.join(UPLOAD_DIR, filename)
with open(file_path, "wb") as buffer:
buffer.write(file.file.read())
return file_path
def extract_text_from_pdf(file_path):
"""PDF dosyasından metin çıkar"""
try:
# PDF'i görüntülere dönüştür
images = convert_from_path(file_path)
text = ""
# Her sayfadan metin çıkar
for img in images:
text += pytesseract.image_to_string(img, lang='tur') + "\n"
return text
except Exception as e:
print(f"PDF işleme hatası: {str(e)}")
return ""
def extract_text_from_image(file_path):
"""Görüntüden metin çıkar"""
try:
img = Image.open(file_path)
text = pytesseract.image_to_string(img, lang='tur')
return text
except Exception as e:
print(f"Görüntü işleme hatası: {str(e)}")
return ""
def extract_text_from_docx(file_path):
"""DOCX dosyasından metin çıkar"""
try:
doc = docx.Document(file_path)
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
return text
except Exception as e:
print(f"DOCX işleme hatası: {str(e)}")
return ""
def process_document(file_path, content_type):
"""Belge türüne göre metin çıkar"""
file_type = SUPPORTED_CONTENT_TYPES.get(content_type)
if not file_type:
return "Desteklenmeyen dosya türü"
if file_type == 'pdf':
return extract_text_from_pdf(file_path)
elif file_type == 'image':
return extract_text_from_image(file_path)
elif file_type == 'docx':
return extract_text_from_docx(file_path)
return ""
def analyze_document_content(content, db: Session):
"""Belge içeriğini analiz et"""
try:
# Agent oluştur
agent_executor = create_agent(db)
# Analiz için prompt
prompt = f"""Bu belgeyi analiz et ve aşağıdaki bilgileri çıkar:
1. Belgedeki maliyet hesaplamalarıyla ilgili tüm bilgiler
2. İşçilik maliyetleri
3. Malzeme maliyetleri
4. Kar marjı bilgileri
5. Toplam maliyet
Belge içeriği:
{content}
"""
# Agent'ı çalıştır
result = agent_executor.invoke({"input": prompt})
analysis = result["output"]
# Analiz sonucunu JSON formatında döndür
return json.dumps({
"analysis": analysis,
"analyzed_at": datetime.now().isoformat()
}, ensure_ascii=False)
except Exception as e:
print(f"Analiz hatası: {str(e)}")
return json.dumps({
"error": f"Analiz sırasında hata oluştu: {str(e)}",
"analyzed_at": datetime.now().isoformat()
}, ensure_ascii=False)
def save_document_to_db(db: Session, filename, content_type, file_path, file_size, content_text=None, analysis_result=None):
"""Belge bilgilerini veritabanına kaydet"""
document = Document(
filename=filename,
content_type=content_type,
file_path=file_path,
file_size=file_size,
content_text=content_text,
analysis_result=analysis_result
)
if analysis_result:
document.analyzed_at = datetime.now()
db.add(document)
db.commit()
db.refresh(document)
return document |