File size: 2,710 Bytes
80b4aca
384ef5d
80b4aca
384ef5d
 
80b4aca
384ef5d
 
 
80b4aca
384ef5d
 
 
 
 
80b4aca
384ef5d
 
80b4aca
384ef5d
 
 
80b4aca
384ef5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b4aca
 
384ef5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b4aca
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import json
import gradio as gr
from openai import OpenAI
from datetime import datetime

ENV_API_KEY = os.getenv("LLMAPI_KEY")  # backup if user doesn't provide
DEFAULT_ENDPOINT = "https://api.llmapi.ai/v1"
DEFAULT_MODEL = "deepseek-v4-flash-0731"

def run_llm(endpoint: str, api_key: str, model: str, prompt: str):
    endpoint = (endpoint or "").strip().rstrip("/")
    api_key = (api_key or "").strip()
    model = (model or "").strip()
    prompt = (prompt or "").strip()

    if not endpoint:
        return ("Missing endpoint (e.g. https://api.llmapi.ai/v1).", None)

    key = api_key or (ENV_API_KEY or "").strip()
    if not key:
        return ("Missing API key. Provide one in the form or set Spaces secret env LLMAPI_KEY.", None)

    if not model:
        return ("Missing model (e.g. deepseek-v4-flash-0731).", None)

    if not prompt:
        return ("Missing prompt.", None)

    client = OpenAI(base_url=endpoint, api_key=key)

    try:
        resp = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}],
        )
        content = resp.choices[0].message.content

        # Build a JSON download payload (complete-ish)
        payload = {
            "created_at_utc": datetime.utcnow().isoformat() + "Z",
            "request": {
                "base_url": endpoint,
                "model": model,
                "messages": [{"role": "user", "content": prompt}],
            },
            "response": resp.model_dump() if hasattr(resp, "model_dump") else str(resp),
        }

        # Write to a file for Gradio download
        filename = "result.json"
        with open(filename, "w", encoding="utf-8") as f:
            json.dump(payload, f, ensure_ascii=False, indent=2)

        return (content, filename)
    except Exception as e:
        return (f"Error: {e}", None)

with gr.Blocks() as demo:
    gr.Markdown("# Custom LLM Endpoint (user-provided)")

    with gr.Row():
        endpoint = gr.Textbox(label="Endpoint (base_url)", value=DEFAULT_ENDPOINT)
        api_key = gr.Textbox(
            label="API Key (optional - uses env LLMAPI_KEY as backup)",
            type="password",
            placeholder="Leave blank to use env LLMAPI_KEY",
        )

    model = gr.Textbox(label="Model", value=DEFAULT_MODEL)
    prompt = gr.Textbox(label="Prompt", lines=6, placeholder="Type your prompt here...")

    run_btn = gr.Button("Send")

    out_text = gr.Textbox(label="Response", lines=10)
    out_file = gr.File(label="Download results as JSON")

    run_btn.click(
        fn=run_llm,
        inputs=[endpoint, api_key, model, prompt],
        outputs=[out_text, out_file],
    )

demo.launch()