Spaces:
Runtime error
Runtime error
| 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)", | |
| ) | |