Spaces:
Runtime error
Runtime error
File size: 1,333 Bytes
8a7e52f c049f98 e58b37b c049f98 e58b37b 95a3e6c e58b37b 11e8efb e58b37b c049f98 fb65789 2445bed e58b37b c049f98 1308e75 c049f98 e58b37b 1308e75 | 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 improve(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": "system",
"content": (
"You are a prompt engineer. Rewrite user prompts to be clearer, "
"more detailed, and more effective for AI models. Keep the same intent."
),
},
{
"role": "user",
"content": prompt,
},
],
max_tokens=160,
temperature=0.5,
)
return completion.choices[0].message.content
except Exception as e:
return f"Error from model: {e}"
def app():
return gr.Interface(
fn=improve,
inputs=gr.Textbox(lines=3, label="Enter a prompt to improve"),
outputs=gr.Textbox(label="Improved prompt"),
title="Chapter 2 – Prompt Improver (HF Router + OpenAI client)",
)
|