File size: 11,752 Bytes
511e4d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6edab15
511e4d2
6edab15
 
 
 
 
 
 
 
 
 
 
 
511e4d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08dda1a
511e4d2
 
 
 
 
 
 
 
 
 
08dda1a
511e4d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""
Tool Calling Demo — Saat Dilimi Farkı
=====================================

Bir LLM'in kullanıcı sorusuna göre doğru fonksiyonları (Tool / Function
Calling) çağırmasını sağlayan, timeapi.io (ücretsiz, anahtarsız) API'sinden
gerçek veri çeken ve çağrılan araçları arayüzde açıkça gösteren bir uygulama.

Araçlar:
  - get_current_time(timezone)                 -> o an ilgili zaman diliminde saat + UTC farkı
  - time_difference(timezone1, timezone2)      -> iki zaman dilimi arasındaki saat farkı

Not: timeapi.io'ya ulaşılamazsa Python'un yerleşik `zoneinfo` (IANA veritabanı)
kütüphanesine otomatik düşülür; böylece demo her koşulda çalışır.

Model: Hugging Face Inference Providers üzerinden tool-calling destekli bir
sohbet modeli (varsayılan: openai/gpt-oss-120b).
"""

import os
import json
from datetime import datetime

import requests
import gradio as gr
from huggingface_hub import InferenceClient

try:
    from zoneinfo import ZoneInfo  # Python 3.9+
except ImportError:  # çok eski sürümler için
    ZoneInfo = None

# ---------------------------------------------------------------------------
# ZeroGPU uyumluluğu
# ---------------------------------------------------------------------------
# HF Spaces ZeroGPU donanımı, başlatılabilmek için en az bir @spaces.GPU
# fonksiyonu ister. Bu uygulama GPU kullanmaz (yalnızca API çağırır); aşağıdaki
# fonksiyon sadece ZeroGPU'nun başlatma kontrolünü geçmek için vardır.
try:
    import spaces

    @spaces.GPU
    def _zerogpu_warmup():
        return True
except Exception:
    pass

MODEL_ID = os.environ.get("MODEL_ID", "openai/gpt-oss-120b")
HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
HF_PROVIDER = os.environ.get("HF_PROVIDER", "auto")
MAX_TURNS = 6  # Sonsuz döngüye karşı üst sınır

_client_kwargs = {"model": MODEL_ID, "token": HF_TOKEN}
if HF_PROVIDER:
    _client_kwargs["provider"] = HF_PROVIDER
client = InferenceClient(**_client_kwargs)

TIMEAPI_BASE = "https://timeapi.io/api"


def _fetch_zone(timezone: str) -> dict:
    """
    Önce timeapi.io public API'sinden dener; olmazsa yerel zoneinfo'ya düşer.
    Döner: {"timezone", "local_time", "utc_offset_hours"}
    """
    # 1) Birincil yol: public API
    try:
        r = requests.get(
            f"{TIMEAPI_BASE}/timezone/zone",
            params={"timeZone": timezone},
            timeout=10,
        )
        r.raise_for_status()
        data = r.json()
        offset_sec = data["currentUtcOffset"]["seconds"]
        local_raw = data.get("currentLocalTime", "")
        # "2026-07-30T15:30:00.123" -> "2026-07-30 15:30:00"
        local_time = local_raw.replace("T", " ").split(".")[0]
        return {
            "timezone": data.get("timeZone", timezone),
            "local_time": local_time,
            "utc_offset_hours": round(offset_sec / 3600, 2),
            "source": "timeapi.io",
        }
    except Exception:
        pass  # API başarısız -> yedek yola geç

    # 2) Yedek yol: yerel IANA veritabanı
    if ZoneInfo is not None:
        try:
            now = datetime.now(ZoneInfo(timezone))
            return {
                "timezone": timezone,
                "local_time": now.strftime("%Y-%m-%d %H:%M:%S"),
                "utc_offset_hours": round(now.utcoffset().total_seconds() / 3600, 2),
                "source": "zoneinfo (yerel)",
            }
        except Exception:
            pass

    return {"error": f"'{timezone}' geçerli bir IANA zaman dilimi değil (örn. 'Europe/Istanbul')."}


def get_current_time(timezone: str) -> dict:
    """Belirtilen IANA zaman diliminde güncel saati ve UTC farkını döndürür."""
    return _fetch_zone(timezone)


def time_difference(timezone1: str, timezone2: str) -> dict:
    """İki IANA zaman dilimi arasındaki saat farkını hesaplar."""
    z1 = _fetch_zone(timezone1)
    z2 = _fetch_zone(timezone2)
    if "error" in z1:
        return z1
    if "error" in z2:
        return z2

    diff = round(z2["utc_offset_hours"] - z1["utc_offset_hours"], 2)
    if diff > 0:
        note = f"{z2['timezone']}, {z1['timezone']}'dan {abs(diff)} saat ileridedir."
    elif diff < 0:
        note = f"{z2['timezone']}, {z1['timezone']}'dan {abs(diff)} saat geridedir."
    else:
        note = f"{z1['timezone']} ve {z2['timezone']} aynı saat dilimindedir."

    return {
        "timezone1": z1["timezone"],
        "timezone2": z2["timezone"],
        "difference_hours": diff,
        "note": note,
    }


TOOL_FUNCS = {
    "get_current_time": get_current_time,
    "time_difference": time_difference,
}


TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": (
                "Belirtilen zaman diliminde o anki yerel saati ve UTC farkını "
                "döndürür."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "timezone": {
                        "type": "string",
                        "description": (
                            "IANA zaman dilimi kimliği, örn. 'Europe/Istanbul', "
                            "'America/New_York', 'Asia/Tokyo'. Kullanıcı şehir adı "
                            "verirse uygun IANA kimliğine çevir."
                        ),
                    }
                },
                "required": ["timezone"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "time_difference",
            "description": "İki zaman dilimi arasındaki saat farkını hesaplar.",
            "parameters": {
                "type": "object",
                "properties": {
                    "timezone1": {
                        "type": "string",
                        "description": "Birinci IANA zaman dilimi, örn. 'America/New_York'.",
                    },
                    "timezone2": {
                        "type": "string",
                        "description": "İkinci IANA zaman dilimi, örn. 'Asia/Tokyo'.",
                    },
                },
                "required": ["timezone1", "timezone2"],
            },
        },
    },
]

SYSTEM_PROMPT = (
    "Sen araç kullanabilen yardımcı bir asistansın. Saat ve zaman dilimi "
    "sorularında ASLA bilgi uydurma; her zaman verilen araçları "
    "(get_current_time, time_difference) çağırarak gerçek verilere ulaş. "
    "Kullanıcı şehir adı verirse ('New York', 'Tokyo', 'İstanbul'), bunu doğru "
    "IANA zaman dilimi kimliğine çevir (örn. 'America/New_York', 'Asia/Tokyo', "
    "'Europe/Istanbul'). Birden fazla yer sorulduğunda her biri için ayrı araç "
    "çağır. Son yanıtını kullanıcının diliyle, kısa ve net ver."
)


def _fmt_args(args: dict) -> str:
    return ", ".join(f"{k}={v!r}" for k, v in args.items())


def run_agent(user_message: str, history_messages: list):
    """Modeli araçlarla çalıştırır. (nihai_yanit, adim_izi) döner."""
    if not HF_TOKEN:
        return (
            "⚠️ HF_TOKEN ayarlı değil. HF Spaces > Settings > Secrets bölümünden "
            "`HF_TOKEN` ekleyin (huggingface.co/settings/tokens).",
            "",
        )

    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    messages.extend(history_messages)
    messages.append({"role": "user", "content": user_message})

    trace_lines = []

    for turn in range(1, MAX_TURNS + 1):
        try:
            resp = client.chat_completion(
                messages=messages,
                tools=TOOLS,
                tool_choice="auto",
                max_tokens=1024,
                temperature=0.2,
            )
        except Exception as exc:
            return f"Model çağrısı başarısız oldu: {exc}", "\n".join(trace_lines)

        msg = resp.choices[0].message
        tool_calls = msg.tool_calls or []

        if not tool_calls:
            final = msg.content or "(boş yanıt)"
            if trace_lines:
                trace_lines.append(f"\n[Turn {turn}] Nihai Yanıt:")
                trace_lines.append(final)
            return final, "\n".join(trace_lines)

        messages.append(
            {
                "role": "assistant",
                "content": msg.content or "",
                "tool_calls": [
                    {
                        "id": tc.id,
                        "type": "function",
                        "function": {
                            "name": tc.function.name,
                            "arguments": tc.function.arguments,
                        },
                    }
                    for tc in tool_calls
                ],
            }
        )

        trace_lines.append(f"[Turn {turn}] Araç Çağrıları:")
        for tc in tool_calls:
            name = tc.function.name
            try:
                args = json.loads(tc.function.arguments or "{}")
            except json.JSONDecodeError:
                args = {}

            func = TOOL_FUNCS.get(name)
            result = func(**args) if func else {"error": f"Bilinmeyen araç: {name}"}

            trace_lines.append(f"   -> {name}({_fmt_args(args)})")
            trace_lines.append(f"   <- {result}")

            messages.append(
                {
                    "role": "tool",
                    "tool_call_id": tc.id,
                    "name": name,
                    "content": json.dumps(result, ensure_ascii=False),
                }
            )

    return "Adım sınırına ulaşıldı, yanıt tamamlanamadı.", "\n".join(trace_lines)


def respond(message, chat_history):
    history_messages = [
        {"role": t["role"], "content": t["content"]} for t in chat_history
    ]
    final, trace = run_agent(message, history_messages)
    chat_history = chat_history + [
        {"role": "user", "content": message},
        {"role": "assistant", "content": final},
    ]
    return chat_history, trace, ""


with gr.Blocks(title="Tool Calling — Saat Dilimi Farkı") as demo:
    gr.Markdown(
        "# 🕐 Tool Calling Demo — Saat Dilimi Farkı\n"
        "Model, sorunuza göre **`get_current_time`** ve **`time_difference`** "
        "araçlarını otomatik çağırır. Sağ tarafta arka planda çağrılan araçları "
        "ve adımları görebilirsiniz.\n\n"
        f"**Model:** `{MODEL_ID}` · **Veri kaynağı:** timeapi.io"
    )

    with gr.Row():
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(height=420, label="Sohbet")
            msg = gr.Textbox(
                placeholder="Örn: New York ile Tokyo arasında kaç saat fark var, şu an saat kaç?",
                label="Sorunuz",
            )
            with gr.Row():
                send = gr.Button("Gönder", variant="primary")
                clear = gr.Button("Temizle")
        with gr.Column(scale=2):
            trace_box = gr.Textbox(
                label="🔧 Araç Çağrı Adımları (Tool Trace)",
                lines=22,
                interactive=False,
            )

    gr.Examples(
        examples=[
            "New York ile Tokyo arasında kaç saat fark var, şu an oralarda saat kaç?",
            "İstanbul'da şu an saat kaç?",
            "Londra mı ileride Los Angeles mı, aradaki fark nedir?",
            "Sidney ile Berlin arasındaki saat farkı kaç saat?",
        ],
        inputs=msg,
    )

    send.click(respond, [msg, chatbot], [chatbot, trace_box, msg])
    msg.submit(respond, [msg, chatbot], [chatbot, trace_box, msg])
    clear.click(lambda: ([], "", ""), None, [chatbot, trace_box, msg])


if __name__ == "__main__":
    demo.launch()