beginner-apps / chapter3_summarizer.py
ppvbooks's picture
Update chapter3_summarizer.py
9b5c61d verified
Raw
History Blame Contribute Delete
1.31 kB
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)",
)