File size: 1,062 Bytes
5d0d94b
500536b
8105e67
500536b
8105e67
 
 
500536b
e02bd89
8105e67
 
500536b
 
 
e02bd89
c0e1eee
2915102
8105e67
 
 
 
 
 
 
 
 
 
500536b
8105e67
500536b
 
e02bd89
 
 
 
8105e67
 
 
e02bd89
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 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 generate(prompt: str) -> str:
    prompt = prompt.strip()
    if not prompt:
        return "Please enter a prompt."

    try:
        completion = client.chat.completions.create(
            model=MODEL_ID,
            messages=[
                {
                    "role": "user",
                    "content": f"Continue this text in a natural way:\n\n{prompt}"
                }
            ],
            max_tokens=120,
            temperature=0.8,
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"Error from model: {e}"

def app():
    return gr.Interface(
        fn=generate,
        inputs=gr.Textbox(lines=3, label="Enter a starting text"),
        outputs=gr.Textbox(label="Generated continuation"),
        title="Chapter 1 – Text Generator (HF Router + OpenAI client)",
    )