Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
-
AutoTokenizer,
|
| 5 |
-
AutoModelForSeq2SeqLM,
|
| 6 |
-
AutoModelForCausalLM,
|
| 7 |
-
)
|
| 8 |
import pdfplumber
|
| 9 |
|
| 10 |
-
# ----
|
| 11 |
-
|
| 12 |
-
SUMM_MODEL_NAME = "mukayese/mt5-base-turkish-summarization" # Özet beyni
|
| 13 |
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
summ_model = AutoModelForSeq2SeqLM.from_pretrained(SUMM_MODEL_NAME).to(device)
|
| 19 |
-
|
| 20 |
-
# Ana LLM (Turkcell-LLM)
|
| 21 |
-
core_tokenizer = AutoTokenizer.from_pretrained(CORE_MODEL_NAME)
|
| 22 |
-
core_model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
-
CORE_MODEL_NAME,
|
| 24 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 25 |
-
)
|
| 26 |
-
if device == "cpu":
|
| 27 |
-
core_model.to(device)
|
| 28 |
|
| 29 |
|
| 30 |
# ---- Yardımcı fonksiyonlar ----
|
|
@@ -44,88 +29,59 @@ def extract_pdf_text(pdf_file) -> str:
|
|
| 44 |
return full_text
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
"""
|
| 49 |
-
if not
|
| 50 |
-
return "
|
| 51 |
-
|
| 52 |
-
text = text[:max_input_chars]
|
| 53 |
|
| 54 |
-
inputs =
|
| 55 |
-
|
| 56 |
return_tensors="pt",
|
| 57 |
truncation=True,
|
| 58 |
max_length=1024,
|
| 59 |
).to(device)
|
| 60 |
|
| 61 |
with torch.no_grad():
|
| 62 |
-
output_ids =
|
| 63 |
**inputs,
|
| 64 |
-
max_length=
|
| 65 |
num_beams=4,
|
| 66 |
early_stopping=True,
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
return
|
| 71 |
|
| 72 |
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
if not text:
|
| 76 |
-
return "
|
| 77 |
-
|
| 78 |
-
if not question:
|
| 79 |
-
return "Lütfen PDF hakkında bir soru yaz."
|
| 80 |
-
|
| 81 |
-
context = text[:max_context_chars]
|
| 82 |
|
| 83 |
prompt = (
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"
|
| 87 |
-
"Belge metni:\n"
|
| 88 |
-
f"{context}\n\n"
|
| 89 |
-
f"Soru: {question}\n"
|
| 90 |
-
"Cevap:"
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
prompt,
|
| 95 |
-
return_tensors="pt",
|
| 96 |
-
truncation=True,
|
| 97 |
-
max_length=4096,
|
| 98 |
-
).to(core_model.device)
|
| 99 |
-
|
| 100 |
-
with torch.no_grad():
|
| 101 |
-
output_ids = core_model.generate(
|
| 102 |
-
**inputs,
|
| 103 |
-
max_length=512,
|
| 104 |
-
do_sample=True,
|
| 105 |
-
top_p=0.9,
|
| 106 |
-
temperature=0.7,
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
full_answer = core_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 110 |
-
|
| 111 |
-
if "Cevap:" in full_answer:
|
| 112 |
-
answer = full_answer.split("Cevap:", 1)[-1].strip()
|
| 113 |
-
else:
|
| 114 |
-
answer = full_answer.strip()
|
| 115 |
|
| 116 |
-
return answer
|
| 117 |
|
| 118 |
-
|
| 119 |
-
def summarize_pdf(pdf_file):
|
| 120 |
text = extract_pdf_text(pdf_file)
|
| 121 |
if not text:
|
| 122 |
-
return "
|
| 123 |
-
|
|
|
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
text = extract_pdf_text(pdf_file)
|
| 128 |
-
return answer_question_from_text(text, question)
|
| 129 |
|
| 130 |
|
| 131 |
# ---- Gradio arayüzü ----
|
|
@@ -133,11 +89,12 @@ def qa_on_pdf(pdf_file, question):
|
|
| 133 |
with gr.Blocks() as demo:
|
| 134 |
gr.Markdown(
|
| 135 |
"""
|
| 136 |
-
# 🧠 ZenkaMind PDF Analiz (
|
| 137 |
|
| 138 |
- PDF yükle
|
| 139 |
-
-
|
| 140 |
-
-
|
|
|
|
| 141 |
"""
|
| 142 |
)
|
| 143 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import pdfplumber
|
| 5 |
|
| 6 |
+
# ---- TEK MODEL: Türkçe özet + QA için mT5 ----
|
| 7 |
+
MODEL_NAME = "mukayese/mt5-base-turkish-summarization"
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
# ---- Yardımcı fonksiyonlar ----
|
|
|
|
| 29 |
return full_text
|
| 30 |
|
| 31 |
|
| 32 |
+
def generate_text(prompt: str, max_new_tokens: int = 256) -> str:
|
| 33 |
+
"""mT5 ile verilen prompt'a göre metin üret."""
|
| 34 |
+
if not prompt:
|
| 35 |
+
return "Metin boş görünüyor."
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
inputs = tokenizer(
|
| 38 |
+
prompt,
|
| 39 |
return_tensors="pt",
|
| 40 |
truncation=True,
|
| 41 |
max_length=1024,
|
| 42 |
).to(device)
|
| 43 |
|
| 44 |
with torch.no_grad():
|
| 45 |
+
output_ids = model.generate(
|
| 46 |
**inputs,
|
| 47 |
+
max_length=max_new_tokens,
|
| 48 |
num_beams=4,
|
| 49 |
early_stopping=True,
|
| 50 |
)
|
| 51 |
|
| 52 |
+
text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 53 |
+
return text.strip()
|
| 54 |
|
| 55 |
|
| 56 |
+
def summarize_pdf(pdf_file):
|
| 57 |
+
text = extract_pdf_text(pdf_file)
|
| 58 |
if not text:
|
| 59 |
+
return "PDF'ten metin çıkarılamadı. Dosya boş veya okunamıyor olabilir."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
prompt = (
|
| 62 |
+
"Aşağıdaki Türkçe metni kısa ve anlaşılır bir şekilde özetle.\n\n"
|
| 63 |
+
f"Metin:\n{text}\n\n"
|
| 64 |
+
"Özet:"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
+
return generate_text(prompt, max_new_tokens=256)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
|
| 70 |
+
def qa_on_pdf(pdf_file, question):
|
|
|
|
| 71 |
text = extract_pdf_text(pdf_file)
|
| 72 |
if not text:
|
| 73 |
+
return "Önce geçerli bir PDF yüklemelisin."
|
| 74 |
+
if not question:
|
| 75 |
+
return "Lütfen PDF hakkında bir soru yaz."
|
| 76 |
|
| 77 |
+
prompt = (
|
| 78 |
+
"Aşağıdaki Türkçe metne göre soruya cevap ver. Bilmediğin şeyi uydurma.\n\n"
|
| 79 |
+
f"Metin:\n{text}\n\n"
|
| 80 |
+
f"Soru: {question}\n"
|
| 81 |
+
"Cevap:"
|
| 82 |
+
)
|
| 83 |
|
| 84 |
+
return generate_text(prompt, max_new_tokens=256)
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# ---- Gradio arayüzü ----
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown(
|
| 91 |
"""
|
| 92 |
+
# 🧠 ZenkaMind PDF Analiz (hafif sürüm)
|
| 93 |
|
| 94 |
- PDF yükle
|
| 95 |
+
- Özet al
|
| 96 |
+
- Aynı PDF hakkında soru sor
|
| 97 |
+
(Şu an tek model: mT5 Türkçe özet/QA)
|
| 98 |
"""
|
| 99 |
)
|
| 100 |
|