File size: 1,176 Bytes
cc21f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from dotenv import load_dotenv

load_dotenv()
api_key = os.getenv("OPENROUTER_API_KEY").strip()


def build_prompt(text, mode):
    if "Sade" in mode:
        instruction = "Bu metni herkesin anlayabileceği şekilde sadeleştir."
    elif "Eleştir" in mode:
        instruction = "Metni eleştir, eksiklerini ve güçlü yönlerini değerlendir."
    elif "Başlık" in mode:
        instruction = "Metne uygun birkaç başlık öner."
    elif "Not" in mode:
        instruction = "Metinden önemli notlar çıkar."
    else:
        instruction = "Kısa ve teknik bir özet ver."

    return f"{instruction}\n\nMetin:\n{text}"

def summarize_text(text, mode):
    url = "https://openrouter.ai/api/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "openai/gpt-3.5-turbo",
        "messages": [
            {"role": "user", "content": build_prompt(text, mode)}
        ]
    }

    response = requests.post(url, headers=headers, json=data)
    response.raise_for_status()
    return response.json()['choices'][0]['message']['content']