Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130import jsonimport osimport tempfilefrom datetime import datetimefrom pathlib import Pathfrom typing import Dict, Generator, List, Optional, Tuple
import gradio as grfrom openai import OpenAI
# ------------------------------------------------------------------# CONFIG# ------------------------------------------------------------------BASE_URL = os.getenv("BASE_URL", "https://kemmlowww-g4s.hf.space/v1")MODEL = os.getenv("MODEL", "zai-org/GLM-5.1")API_KEY = os.getenv("API_KEY", "dummy")PROVIDER = os.getenv("PROVIDER", "deepinfra")
SYSTEM_PROMPT_DEFAULT = os.getenv( "SYSTEM_PROMPT", "You are a helpful, precise AI assistant.")
APP_NAME = "Nova Chat"APP_TAGLINE = "Claude-style UI over OpenAI-compatible endpoints"
# ------------------------------------------------------------------# CLIENT# ------------------------------------------------------------------def get_client(): return OpenAI( base_url=BASE_URL.rstrip("/"), api_key=API_KEY, default_headers={"Provider": PROVIDER}, )
# ------------------------------------------------------------------# CHAT HELPERS# ------------------------------------------------------------------def build_messages(history, system_prompt): messages = [{"role": "system", "content": system_prompt}] for u, a in history: messages.append({"role": "user", "content": u}) if a: messages.append({"role": "assistant", "content": a}) return messages
def stream_chat(message, history, system_prompt, model): client = get_client()
messages = build_messages(history, system_prompt) messages.append({"role": "user", "content": message})
stream = client.chat.completions.create( model=model, messages=messages, stream=True, )
partial = "" for chunk in stream: delta = chunk.choices[0].delta.content or "" if delta: partial += delta yield partial
# ------------------------------------------------------------------# UI CALLBACKS# ------------------------------------------------------------------def respond(message, history, system_prompt, model): history = history + [[message, ""]]
for output in stream_chat(message, history[:-1], system_prompt, model): history[-1][1] = output yield history
def clear(): return []
# ------------------------------------------------------------------# UI# ------------------------------------------------------------------CSS = """body { background-color: #0b0f14; }.gradio-container { max-width: 1000px !important; margin: auto; }"""
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo: gr.Markdown(f"# {APP_NAME}\n{APP_TAGLINE}")
chatbot = gr.Chatbot(height=650)
with gr.Row(): msg = gr.Textbox( placeholder="Type message...", scale=8 ) send = gr.Button("Send", scale=2)
with gr.Row(): system_prompt = gr.Textbox( value=SYSTEM_PROMPT_DEFAULT, label="System Prompt" ) model = gr.Textbox( value=MODEL, label="Model" )
send.click( respond, inputs=[msg, chatbot, system_prompt, model], outputs=chatbot, ).then(lambda: "", None, msg)
msg.submit( respond, inputs=[msg, chatbot, system_prompt, model], outputs=chatbot, ).then(lambda: "", None, msg)
gr.Button("Clear").click(clear, None, chatbot)
demo.launch()app.pyAdded+130