modelflaredev commited on
Commit
c845c55
·
verified ·
1 Parent(s): f2f03dc

Add Gradio chat app

Browse files
Files changed (3) hide show
  1. README.md +22 -6
  2. app.py +84 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,10 +1,26 @@
1
  ---
2
- title: Chat
3
- emoji: 🚀
4
- colorFrom: blue
5
- colorTo: gray
6
- sdk: static
 
 
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Modelflare Chat
3
+ emoji: 💬
4
+ colorFrom: indigo
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 5.49.1
8
+ app_file: app.py
9
  pinned: false
10
+ license: mit
11
+ short_description: Chat demo via Modelflare OpenAI-compatible API gateway
12
  ---
13
 
14
+ # Modelflare Chat Demo
15
+
16
+ Public Gradio demo for [Modelflare](https://modelflare.dev/) — a stable, lower-cost LLM / image / video API gateway.
17
+
18
+ ## Setup
19
+
20
+ 1. Create an API key in the [Modelflare console](https://modelflare.dev/).
21
+ 2. In this Space → **Settings** → **Variables and secrets**, add secret `MODELFLARE_API_KEY`.
22
+ 3. Restart the Space.
23
+
24
+ Point OpenAI SDKs at `https://modelflare.dev/v1` with the same key. Docs: [docs.modelflare.dev](https://docs.modelflare.dev/).
25
+
26
+ Model dropdown ids are examples — use a model name enabled on your Modelflare group (override with the custom model field).
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from openai import OpenAI
3
+ import gradio as gr
4
+
5
+ BASE_URL = "https://modelflare.dev/v1"
6
+ DEFAULT_MODELS = [
7
+ "gpt-4o-mini",
8
+ "gpt-4o",
9
+ "claude-sonnet-4-5",
10
+ "grok-4",
11
+ ]
12
+
13
+
14
+ def get_client() -> OpenAI:
15
+ api_key = os.environ.get("MODELFLARE_API_KEY", "").strip()
16
+ if not api_key:
17
+ raise gr.Error(
18
+ "缺少 Space Secret:MODELFLARE_API_KEY。"
19
+ "请在 Space Settings → Variables and secrets 里添加后重启。"
20
+ )
21
+ return OpenAI(api_key=api_key, base_url=BASE_URL)
22
+
23
+
24
+ def chat(message: str, history: list, model: str, custom_model: str):
25
+ model_id = (custom_model or "").strip() or (model or "").strip() or DEFAULT_MODELS[0]
26
+ client = get_client()
27
+ messages = []
28
+ for turn in history:
29
+ if isinstance(turn, dict):
30
+ role, content = turn.get("role"), turn.get("content")
31
+ if role in ("user", "assistant") and content:
32
+ messages.append({"role": role, "content": content})
33
+ elif isinstance(turn, (list, tuple)) and len(turn) == 2:
34
+ u, a = turn
35
+ if u:
36
+ messages.append({"role": "user", "content": u})
37
+ if a:
38
+ messages.append({"role": "assistant", "content": a})
39
+ messages.append({"role": "user", "content": message})
40
+
41
+ stream = client.chat.completions.create(
42
+ model=model_id,
43
+ messages=messages,
44
+ stream=True,
45
+ )
46
+ partial = ""
47
+ for chunk in stream:
48
+ delta = chunk.choices[0].delta.content or ""
49
+ if delta:
50
+ partial += delta
51
+ yield partial
52
+
53
+
54
+ with gr.Blocks(title="Modelflare Chat Demo") as demo:
55
+ gr.Markdown(
56
+ """
57
+ # Modelflare Chat Demo
58
+ 通过 [Modelflare](https://modelflare.dev/) 统一网关试用 OpenAI 兼容对话。
59
+ 文档:[docs.modelflare.dev](https://docs.modelflare.dev/) · Base URL:`https://modelflare.dev/v1`
60
+ """
61
+ )
62
+ with gr.Row():
63
+ model = gr.Dropdown(
64
+ choices=DEFAULT_MODELS,
65
+ value=DEFAULT_MODELS[0],
66
+ label="Model",
67
+ scale=2,
68
+ )
69
+ custom_model = gr.Textbox(
70
+ label="Custom model id (可选,填写则覆盖上方)",
71
+ placeholder="例如你的分组里实际可用的模型名",
72
+ scale=3,
73
+ )
74
+ chatbot = gr.ChatInterface(
75
+ fn=chat,
76
+ additional_inputs=[model, custom_model],
77
+ examples=[
78
+ ["用一句话介绍 Modelflare"],
79
+ ["Write a haiku about API gateways"],
80
+ ],
81
+ )
82
+
83
+ if __name__ == "__main__":
84
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=5.0.0
2
+ openai>=1.40.0