File size: 1,313 Bytes
df97ba1
b8b95d4
9b5c61d
b8b95d4
9b5c61d
 
 
b8b95d4
8a388fa
9b5c61d
 
b8b95d4
 
 
86677d9
 
01eecba
9b5c61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b95d4
9b5c61d
b8b95d4
 
8a388fa
 
 
 
9b5c61d
b8b95d4
9b5c61d
c600347
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
import os
import gradio as gr
from openai import OpenAI

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.environ["HF_TOKEN"],
)

MODEL_ID = "zai-org/GLM-4.7-Flash:novita"

def summarize(text: str) -> str:
    text = text.strip()
    if not text:
        return "Please enter text to summarize."

    try:
        completion = client.chat.completions.create(
            model=MODEL_ID,
            messages=[
                {
                    "role": "system",
                    "content": (
                        "You are a concise summarizer. Read the text and produce a short, clear summary "
                        "in 2–3 sentences, preserving the main ideas."
                    ),
                },
                {
                    "role": "user",
                    "content": text,
                },
            ],
            max_tokens=160,
            temperature=0.4,
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"Error from model: {e}"

def app():
    return gr.Interface(
        fn=summarize,
        inputs=gr.Textbox(lines=6, label="Enter text to summarize"),
        outputs=gr.Textbox(label="Summary"),
        title="Chapter 3 – Summarizer (HF Router + OpenAI client)",
    )