Spaces:
Running
Running
File size: 3,322 Bytes
cc21f11 c4e9c8e cc21f11 c4e9c8e 0303b9b cc21f11 138dc70 31be26a 138dc70 31be26a 60263a2 468903e 96bc60c 99e492b 96bc60c 99e492b cc21f11 99e492b cc21f11 99e492b cc21f11 99e492b c4e9c8e cc21f11 99e492b 31be26a cc21f11 138dc70 cc21f11 0303b9b cc21f11 0303b9b fe09770 cc21f11 60263a2 138dc70 44e2364 cc21f11 0303b9b c4e9c8e |
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 |
import os
import requests
from dotenv import load_dotenv
from utils import chunk_text_by_tokens # type: ignore
load_dotenv()
print("KEY IN SUMMARIZER:", os.getenv("OPENROUTER_API_KEY")) # <<< bunu ekle
api_key = os.getenv("OPENROUTER_API_KEY")
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key or not api_key.strip():
raise RuntimeError("❌ OPENROUTER_API_KEY bulunamadı. Hugging Face Secrets kısmına eklenmeli.")
def build_prompt(text, mode, lang_mode="Otomatik", is_table=False):
lang_instruction = ""
if "Çevir" in lang_mode:
if "Türkçeye" in lang_mode:
lang_instruction = "\n\nSonuç Türkçeye çevrilsin."
elif "İngilizceye" in lang_mode:
lang_instruction = "\n\nSonuç İngilizceye çevrilsin."
elif lang_mode == "Otomatik":
lang_instruction = "\n\nMetnin dilini algıla ve uygun dilde özetle."
if is_table:
instruction = "Aşağıdaki tabloyu analiz et ve teknik bir şekilde özetle."
return f"{instruction}{lang_instruction}\n\n{text}"
if "Karma" in mode:
instruction = """
Aşağıdaki metni 3 ayrı biçimde özetle:
1. Teknik bir özet ver.
2. Herkesin anlayacağı şekilde sade bir açıklama yaz.
3. Madde madde önemli notları çıkar.
"""
elif "Sade" in mode:
instruction = "Bu metni herkesin anlayacağı şekilde sadeleştir."
elif "Eleştir" in mode:
instruction = "Metni eleştir, eksik ve güçlü yönlerini değerlendir."
elif "Başlık" in mode:
instruction = "Metne uygun başlık önerileri üret."
elif "Not" in mode:
instruction = "Bu metinden önemli notlar çıkar."
elif "Chat" in mode:
instruction = """
Aşağıdaki yazışmalıarı veya serbest notları oku ve şunları çıkar:
- Ana konuşma başlıkları
- Varsa karar verilen noktalar
- Belirgin fikir veya öneriler
Yazım sade ve maddeli olsun.
"""
else:
instruction = "Metni kısa ve teknik bir şekilde özetle."
return f"{instruction}{lang_instruction}\n\nMetin:\n{text}"
def summarize_text(text, mode, model_name="anthropic/claude-3-haiku", lang_mode="Otomatik", is_table=False):
url = "https://openrouter.ai/api/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key.strip()}",
"Content-Type": "application/json"
}
payload = {
"model": model_name,
"messages": [
{"role": "user", "content": build_prompt(text, mode, lang_mode, is_table)}
],
"max_tokens": 1300
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
result = response.json()
return result['choices'][0]['message']['content'].strip()
except requests.exceptions.HTTPError as e:
return f"❌ HTTP Hatası: {e} | Yanıt: {response.text}"
except Exception as e:
return f"❌ Sistemsel Hata: {str(e)}"
def summarize_long_text(text, mode, model_name="anthropic/claude-3-haiku", lang_mode="Otomatik", is_table=False):
chunks = chunk_text_by_tokens(text, max_tokens=1300)
summaries = []
for chunk in chunks:
summary = summarize_text(chunk, mode, model_name, lang_mode, is_table)
summaries.append(summary)
return "\n\n".join(summaries)
|