Zenkad commited on
Commit
fec4279
·
verified ·
1 Parent(s): 41e7713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -18
app.py CHANGED
@@ -7,8 +7,7 @@ from transformers import (
7
  )
8
  import pdfplumber
9
 
10
- # ---- Modeller ----
11
-
12
  CORE_MODEL_NAME = "TURKCELL/Turkcell-LLM-7b-v1" # Ana sohbet / QA beyni
13
  SUMM_MODEL_NAME = "mukayese/mt5-base-turkish-summarization" # Özet beyni
14
 
@@ -23,7 +22,6 @@ core_tokenizer = AutoTokenizer.from_pretrained(CORE_MODEL_NAME)
23
  core_model = AutoModelForCausalLM.from_pretrained(
24
  CORE_MODEL_NAME,
25
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
26
- device_map="auto" if torch.cuda.is_available() else None,
27
  )
28
  if device == "cpu":
29
  core_model.to(device)
@@ -32,9 +30,7 @@ if device == "cpu":
32
  # ---- Yardımcı fonksiyonlar ----
33
 
34
  def extract_pdf_text(pdf_file) -> str:
35
- """
36
- Yüklenen PDF dosyasından düz metin çıkarır.
37
- """
38
  if pdf_file is None:
39
  return ""
40
 
@@ -49,13 +45,10 @@ def extract_pdf_text(pdf_file) -> str:
49
 
50
 
51
  def summarize_text(text: str, max_input_chars: int = 6000) -> str:
52
- """
53
- PDF metnini kısaltarak mT5 ile özetler.
54
- """
55
  if not text:
56
  return "PDF'ten metin çıkarılamadı veya dosya boş görünüyor."
57
 
58
- # Çok uzun metni kırp (MVP için basit truncation)
59
  text = text[:max_input_chars]
60
 
61
  inputs = summ_tokenizer(
@@ -78,16 +71,13 @@ def summarize_text(text: str, max_input_chars: int = 6000) -> str:
78
 
79
 
80
  def answer_question_from_text(text: str, question: str, max_context_chars: int = 4000) -> str:
81
- """
82
- PDF metni + kullanıcının sorusuna göre, Turkcell-LLM ile cevap üretir.
83
- """
84
  if not text:
85
  return "Önce geçerli bir PDF yüklemelisin."
86
 
87
  if not question:
88
  return "Lütfen PDF hakkında bir soru yaz."
89
 
90
- # Konteksti çok büyütmemek için basit truncation
91
  context = text[:max_context_chars]
92
 
93
  prompt = (
@@ -118,7 +108,6 @@ def answer_question_from_text(text: str, question: str, max_context_chars: int =
118
 
119
  full_answer = core_tokenizer.decode(output_ids[0], skip_special_tokens=True)
120
 
121
- # Prompt'u cevaptan ayırmak için basit kesme
122
  if "Cevap:" in full_answer:
123
  answer = full_answer.split("Cevap:", 1)[-1].strip()
124
  else:
@@ -127,8 +116,6 @@ def answer_question_from_text(text: str, question: str, max_context_chars: int =
127
  return answer
128
 
129
 
130
- # ---- Gradio Arayüzü ----
131
-
132
  def summarize_pdf(pdf_file):
133
  text = extract_pdf_text(pdf_file)
134
  if not text:
@@ -141,6 +128,8 @@ def qa_on_pdf(pdf_file, question):
141
  return answer_question_from_text(text, question)
142
 
143
 
 
 
144
  with gr.Blocks() as demo:
145
  gr.Markdown(
146
  """
@@ -186,5 +175,6 @@ with gr.Blocks() as demo:
186
  outputs=[answer_output],
187
  )
188
 
 
189
  if __name__ == "__main__":
190
- demo.launch(
 
7
  )
8
  import pdfplumber
9
 
10
+ # ---- Model isimleri ----
 
11
  CORE_MODEL_NAME = "TURKCELL/Turkcell-LLM-7b-v1" # Ana sohbet / QA beyni
12
  SUMM_MODEL_NAME = "mukayese/mt5-base-turkish-summarization" # Özet beyni
13
 
 
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)
 
30
  # ---- Yardımcı fonksiyonlar ----
31
 
32
  def extract_pdf_text(pdf_file) -> str:
33
+ """PDF dosyasından düz metin çıkar."""
 
 
34
  if pdf_file is None:
35
  return ""
36
 
 
45
 
46
 
47
  def summarize_text(text: str, max_input_chars: int = 6000) -> str:
48
+ """Türkçe özet üret."""
 
 
49
  if not text:
50
  return "PDF'ten metin çıkarılamadı veya dosya boş görünüyor."
51
 
 
52
  text = text[:max_input_chars]
53
 
54
  inputs = summ_tokenizer(
 
71
 
72
 
73
  def answer_question_from_text(text: str, question: str, max_context_chars: int = 4000) -> str:
74
+ """PDF metnine göre soru cevapla."""
 
 
75
  if not text:
76
  return "Önce geçerli bir PDF yüklemelisin."
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 = (
 
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:
 
116
  return answer
117
 
118
 
 
 
119
  def summarize_pdf(pdf_file):
120
  text = extract_pdf_text(pdf_file)
121
  if not text:
 
128
  return answer_question_from_text(text, question)
129
 
130
 
131
+ # ---- Gradio arayüzü ----
132
+
133
  with gr.Blocks() as demo:
134
  gr.Markdown(
135
  """
 
175
  outputs=[answer_output],
176
  )
177
 
178
+
179
  if __name__ == "__main__":
180
+ demo.launch()