Vartex39 commited on
Commit
99e492b
·
1 Parent(s): 2ca41ce

lang_mode parametresi summarize_text fonksiyonuna eklendi

Browse files
Files changed (2) hide show
  1. summarizer.py +18 -12
  2. ui.py +12 -3
summarizer.py CHANGED
@@ -7,31 +7,37 @@ api_key = os.getenv("OPENROUTER_API_KEY")
7
  if not api_key or not api_key.strip():
8
  raise RuntimeError("❌ OPENROUTER_API_KEY bulunamadı. Hugging Face Secrets kısmına eklenmeli.")
9
 
10
- def build_prompt(text, mode):
11
  if "Karma" in mode:
12
- return f"""
13
  Aşağıdaki metni 3 ayrı biçimde özetle:
14
 
15
  1. Teknik bir özet ver.
16
  2. Herkesin anlayacağı şekilde sade bir açıklama yaz.
17
  3. Madde madde önemli notları çıkar.
18
-
19
- Metin:
20
- {text}
21
  """
22
  elif "Sade" in mode:
23
- return "Bu metni herkesin anlayacağı şekilde sadeleştir.\n\nMetin:\n" + text
24
  elif "Eleştir" in mode:
25
- return "Metni eleştir, eksik ve güçlü yönlerini değerlendir.\n\nMetin:\n" + text
26
  elif "Başlık" in mode:
27
- return "Metne uygun başlık önerileri üret.\n\nMetin:\n" + text
28
  elif "Not" in mode:
29
- return "Bu metinden önemli notlar çıkar.\n\nMetin:\n" + text
30
  else:
31
- return "Metni kısa ve teknik bir şekilde özetle.\n\nMetin:\n" + text
 
 
 
 
 
 
 
 
32
 
 
33
 
34
- def summarize_text(text, mode, model_name="anthropic/claude-3-haiku"):
35
  url = "https://openrouter.ai/api/v1/chat/completions"
36
  headers = {
37
  "Authorization": f"Bearer {api_key.strip()}",
@@ -41,7 +47,7 @@ def summarize_text(text, mode, model_name="anthropic/claude-3-haiku"):
41
  payload = {
42
  "model": model_name,
43
  "messages": [
44
- {"role": "user", "content": build_prompt(text, mode)}
45
  ]
46
  }
47
 
 
7
  if not api_key or not api_key.strip():
8
  raise RuntimeError("❌ OPENROUTER_API_KEY bulunamadı. Hugging Face Secrets kısmına eklenmeli.")
9
 
10
+ def build_prompt(text, mode, lang_mode="Otomatik"):
11
  if "Karma" in mode:
12
+ instruction = """
13
  Aşağıdaki metni 3 ayrı biçimde özetle:
14
 
15
  1. Teknik bir özet ver.
16
  2. Herkesin anlayacağı şekilde sade bir açıklama yaz.
17
  3. Madde madde önemli notları çıkar.
 
 
 
18
  """
19
  elif "Sade" in mode:
20
+ instruction = "Bu metni herkesin anlayacağı şekilde sadeleştir."
21
  elif "Eleştir" in mode:
22
+ instruction = "Metni eleştir, eksik ve güçlü yönlerini değerlendir."
23
  elif "Başlık" in mode:
24
+ instruction = "Metne uygun başlık önerileri üret."
25
  elif "Not" in mode:
26
+ instruction = "Bu metinden önemli notlar çıkar."
27
  else:
28
+ instruction = "Metni kısa ve teknik bir şekilde özetle."
29
+
30
+ if "Çevir" in lang_mode:
31
+ if "Türkçeye" in lang_mode:
32
+ instruction += "\n\nSonuç Türkçeye çevrilsin."
33
+ elif "İngilizceye" in lang_mode:
34
+ instruction += "\n\nSonuç İngilizceye çevrilsin."
35
+ elif lang_mode == "Otomatik":
36
+ instruction += "\n\nMetnin dilini algıla ve uygun dilde özetle."
37
 
38
+ return f"{instruction}\n\nMetin:\n{text}"
39
 
40
+ def summarize_text(text, mode, model_name="anthropic/claude-3-haiku", lang_mode="Otomatik"):
41
  url = "https://openrouter.ai/api/v1/chat/completions"
42
  headers = {
43
  "Authorization": f"Bearer {api_key.strip()}",
 
47
  payload = {
48
  "model": model_name,
49
  "messages": [
50
+ {"role": "user", "content": build_prompt(text, mode,lang_mode)}
51
  ]
52
  }
53
 
ui.py CHANGED
@@ -4,7 +4,8 @@ from ocr_engine import extract_text_from_image
4
  from pdf_reader import extract_text_chunks_from_pdf
5
  from summarizer import summarize_text
6
 
7
- def process_input(pdf, image, manual_text, mode, model_name, start_page, end_page):
 
8
  if pdf is not None:
9
  text_chunks = extract_text_chunks_from_pdf(pdf, start=int(start_page), end=int(end_page))
10
  if any("[ERROR]" in chunk for chunk in text_chunks):
@@ -23,7 +24,7 @@ def process_input(pdf, image, manual_text, mode, model_name, start_page, end_pag
23
  summaries = []
24
 
25
  for chunk in text_chunks:
26
- summary = summarize_text(chunk, mode, model_name)
27
  summaries.append(summary)
28
 
29
  full_summary = "\n\n".join(summaries)
@@ -97,12 +98,20 @@ with gr.Blocks() as demo:
97
  text_output = gr.Textbox(label="Giriş Metni")
98
  summary_output = gr.Textbox(label="AI Özeti", lines=10, show_copy_button=True)
99
  summary_file = gr.File(label="Özeti İndir", interactive=True)
 
 
 
 
 
 
100
 
101
  submit_btn.click(
102
  fn=process_input,
103
- inputs=[pdf_input, image_input, manual_input, mode_selector, model_selector, start_page, end_page],
104
  outputs=[text_output, summary_output, summary_file]
105
  )
106
 
 
 
107
  if __name__ == "__main__":
108
  demo.launch(share=True)
 
4
  from pdf_reader import extract_text_chunks_from_pdf
5
  from summarizer import summarize_text
6
 
7
+ def process_input(pdf, image, manual_text, mode, model_name, start_page, end_page, lang_mode):
8
+
9
  if pdf is not None:
10
  text_chunks = extract_text_chunks_from_pdf(pdf, start=int(start_page), end=int(end_page))
11
  if any("[ERROR]" in chunk for chunk in text_chunks):
 
24
  summaries = []
25
 
26
  for chunk in text_chunks:
27
+ summary = summarize_text(chunk, mode, model_name, lang_mode)
28
  summaries.append(summary)
29
 
30
  full_summary = "\n\n".join(summaries)
 
98
  text_output = gr.Textbox(label="Giriş Metni")
99
  summary_output = gr.Textbox(label="AI Özeti", lines=10, show_copy_button=True)
100
  summary_file = gr.File(label="Özeti İndir", interactive=True)
101
+
102
+ lang_mode = gr.Radio(
103
+ choices=["Otomatik", "Sadece Türkçe", "Sadece İngilizce", "Türkçeye Çevir", "İngilizceye Çevir"],
104
+ label="Dil Algılama / Çeviri Modu",
105
+ value="Otomatik"
106
+ )
107
 
108
  submit_btn.click(
109
  fn=process_input,
110
+ inputs=[pdf_input, image_input, manual_input, mode_selector, model_selector, start_page, end_page, lang_mode],
111
  outputs=[text_output, summary_output, summary_file]
112
  )
113
 
114
+
115
+
116
  if __name__ == "__main__":
117
  demo.launch(share=True)