Spaces:
Sleeping
Sleeping
File size: 3,285 Bytes
78fae79 a974597 78fae79 f94072f 8ab61e6 f94072f 78fae79 f94072f 16ce0fe 78fae79 f94072f 8ab61e6 f94072f 8ab61e6 f94072f a974597 f94072f a974597 f94072f 16ce0fe f94072f a974597 16ce0fe f94072f a974597 f94072f 16ce0fe f94072f 16ce0fe f94072f 8ab61e6 f94072f 8ab61e6 f94072f a974597 f94072f a974597 f94072f a974597 f94072f a974597 f94072f a974597 78fae79 16ce0fe af7ae1f |
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 |
import gradio as gr
import google.generativeai as genai
import os
import PyPDF2
from docx import Document
import markdown
from datetime import datetime
import tempfile
# Gemini API anahtarını ortam değişkeninden al
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
# PDF metni çıkar
def extract_text_from_pdf(pdf_file):
text = ""
try:
pdf_reader = PyPDF2.PdfReader(pdf_file)
for page in pdf_reader.pages:
text += page.extract_text() or ""
return text
except Exception as e:
return f"Hata: PDF okunamadı. {str(e)}"
# Gemini ile analiz et
def analyze(text, questions):
generation_config = {
"temperature": 0.2,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config
)
# Özet çıkar
summary_prompt = f"Aşağıdaki metni özetle:\n\n{text[:15000]}\n\nÖzet:"
summary = model.generate_content(summary_prompt).text
# Soruları cevapla
qna = []
for question in questions:
if question.strip() == "":
continue
prompt = f"Belge:\n{text[:15000]}\n\nSoru: {question}\nCevap:"
answer = model.generate_content(prompt).text
qna.append((question, answer))
return summary, qna
# Word belgesi oluştur
def create_doc(summary, qna):
doc = Document()
doc.add_heading("PDF Raporu", 0)
doc.add_paragraph(f"Oluşturulma Tarihi: {datetime.now().strftime('%d.%m.%Y %H:%M')}")
doc.add_heading("Özet", level=1)
doc.add_paragraph(summary)
doc.add_heading("Soru-Cevap", level=1)
for i, (question, answer) in enumerate(qna, 1):
doc.add_heading(f"Soru {i}: {question}", level=2)
doc.add_paragraph(answer)
temp_dir = tempfile.gettempdir()
file_path = os.path.join(temp_dir, f"rapor_{datetime.now().strftime('%Y%m%d%H%M%S')}.docx")
doc.save(file_path)
return file_path
# Gradio fonksiyonu
def process(pdf_file, questions_text):
text = extract_text_from_pdf(pdf_file)
if text.startswith("Hata"):
return text, None
questions = questions_text.strip().split("\n")
summary, qna = analyze(text, questions)
html_output = f"""
<h2>Özet</h2><p>{markdown.markdown(summary)}</p>
<h2>Soru ve Cevaplar</h2>
"""
for i, (q, a) in enumerate(qna, 1):
html_output += f"<b>Soru {i}:</b> {q}<br><b>Cevap:</b> {a}<br><br>"
doc_path = create_doc(summary, qna)
return html_output, doc_path
default_questions = """Belgenin ana konusu nedir?
Belgedeki önemli bulgular nelerdir?
Yazarlar kimlerdir?
Belge hangi tarihte yayınlanmıştır?"""
# Gradio Arayüzü
demo = gr.Interface(
fn=process,
inputs=[
gr.File(label="PDF Yükle", file_types=[".pdf"]),
gr.Textbox(label="Sorular (her satıra bir soru)", value=default_questions, lines=6)
],
outputs=[
gr.HTML(label="Cevaplar"),
gr.File(label="Word Raporu")
],
title="📄 PDF'ten Soru-Cevap ve Word Raporu",
description="Gemini API kullanarak PDF içeriğinden sorulara cevap verir ve rapor hazırlar.",
allow_flagging="never"
)
if __name__ == "__main__":
demo.launch(share=True)
|