fomext commited on
Commit
97e410d
Β·
verified Β·
1 Parent(s): ae48ea7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import json
2
  import time
3
  import uuid
4
- from threading import Thread
5
  from typing import Optional
6
 
7
  import gradio as gr
@@ -64,15 +63,8 @@ def _generate_response(prompt: str, gen_kwargs: dict) -> str:
64
  return tokenizer.decode(new_ids, skip_special_tokens=True)
65
 
66
 
67
- @spaces.GPU
68
- def _generate_streaming(prompt: str, gen_kwargs: dict, streamer: TextIteratorStreamer):
69
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
70
- with torch.no_grad():
71
- model.generate(**inputs, streamer=streamer, **gen_kwargs)
72
-
73
-
74
  # ---------------------------------------------------------------------------
75
- # API functions β€” exposed via gr.api()
76
  # ---------------------------------------------------------------------------
77
 
78
 
@@ -95,16 +87,8 @@ def chat_completions(
95
  """
96
  Non-streaming chat completions. Returns an OpenAI-compatible JSON string.
97
 
98
- Args:
99
- messages_json: JSON array of {role, content} objects, e.g.
100
- '[{"role":"user","content":"Hello"}]'
101
- max_tokens: Maximum tokens to generate (default 512).
102
- temperature: Sampling temperature (default 0.7).
103
- top_p: Nucleus sampling probability (default 0.9).
104
- enable_thinking: Enable chain-of-thought thinking (default False).
105
-
106
- Returns:
107
- OpenAI-compatible chat completion JSON string.
108
  """
109
  try:
110
  messages = json.loads(messages_json)
@@ -155,27 +139,63 @@ def health() -> str:
155
  # ---------------------------------------------------------------------------
156
  # Gradio UI + API
157
  # ---------------------------------------------------------------------------
 
 
 
 
158
 
159
  with gr.Blocks(title=f"{MODEL_ALIAS} API") as demo:
160
  gr.Markdown(f"""
161
  # {MODEL_ALIAS} β€” Gradio API
162
 
163
- Use the Gradio API endpoint at `/gradio_api/call/<fn_name>`.
164
 
165
- | Function | Description |
166
  |----------|-------------|
167
- | `list_models` | List available models (returns JSON string) |
168
- | `chat_completions` | Chat completions, non-streaming (returns JSON string) |
169
- | `health` | Health check (returns JSON string) |
 
 
 
170
 
171
  You can also chat directly below.
172
  """)
 
173
  gr.ChatInterface(fn=gradio_chat)
174
 
175
- # Expose API functions β€” endpoints appear at /gradio_api/call/<fn_name>
176
- gr.api(list_models, api_name="list_models")
177
- gr.api(chat_completions, api_name="chat_completions")
178
- gr.api(health, api_name="health")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
 
181
  # ---------------------------------------------------------------------------
 
1
  import json
2
  import time
3
  import uuid
 
4
  from typing import Optional
5
 
6
  import gradio as gr
 
63
  return tokenizer.decode(new_ids, skip_special_tokens=True)
64
 
65
 
 
 
 
 
 
 
 
66
  # ---------------------------------------------------------------------------
67
+ # API functions
68
  # ---------------------------------------------------------------------------
69
 
70
 
 
87
  """
88
  Non-streaming chat completions. Returns an OpenAI-compatible JSON string.
89
 
90
+ messages_json: JSON array of {role, content} objects,
91
+ e.g. '[{"role":"user","content":"Hello"}]'
 
 
 
 
 
 
 
 
92
  """
93
  try:
94
  messages = json.loads(messages_json)
 
139
  # ---------------------------------------------------------------------------
140
  # Gradio UI + API
141
  # ---------------------------------------------------------------------------
142
+ # In Gradio 4.x (pre-5), gr.api() does not exist. The correct way to expose
143
+ # named API endpoints is to wire invisible component events with api_name=.
144
+ # Each .click(fn=..., api_name="name") registers /gradio_api/call/<name>.
145
+ # ---------------------------------------------------------------------------
146
 
147
  with gr.Blocks(title=f"{MODEL_ALIAS} API") as demo:
148
  gr.Markdown(f"""
149
  # {MODEL_ALIAS} β€” Gradio API
150
 
151
+ Endpoints (via Gradio built-in API):
152
 
153
+ | api_name | Description |
154
  |----------|-------------|
155
+ | `list_models` | List available models β†’ JSON string |
156
+ | `chat_completions` | Chat completions β†’ JSON string |
157
+ | `health` | Health check β†’ JSON string |
158
+
159
+ Call them at `/gradio_api/call/<api_name>` (POST with `{{"data": [...]}}`)
160
+ or use the Gradio Python client.
161
 
162
  You can also chat directly below.
163
  """)
164
+
165
  gr.ChatInterface(fn=gradio_chat)
166
 
167
+ # ------------------------------------------------------------------
168
+ # Hidden API wiring β€” invisible rows that register named endpoints.
169
+ # Gradio 4.41 exposes /gradio_api/call/<api_name> for every event
170
+ # that has api_name set, regardless of whether the components are
171
+ # visible in the UI.
172
+ # ------------------------------------------------------------------
173
+ with gr.Row(visible=False):
174
+ # -- health ------------------------------------------------------
175
+ _health_btn = gr.Button("health")
176
+ _health_out = gr.Textbox()
177
+ _health_btn.click(fn=health, inputs=[], outputs=[_health_out], api_name="health")
178
+
179
+ # -- list_models -------------------------------------------------
180
+ _models_btn = gr.Button("list_models")
181
+ _models_out = gr.Textbox()
182
+ _models_btn.click(fn=list_models, inputs=[], outputs=[_models_out], api_name="list_models")
183
+
184
+ with gr.Row(visible=False):
185
+ # -- chat_completions --------------------------------------------
186
+ _cc_messages = gr.Textbox(label="messages_json")
187
+ _cc_max_tokens = gr.Number(label="max_tokens", value=512)
188
+ _cc_temp = gr.Number(label="temperature", value=0.7)
189
+ _cc_top_p = gr.Number(label="top_p", value=0.9)
190
+ _cc_thinking = gr.Checkbox(label="enable_thinking", value=False)
191
+ _cc_out = gr.Textbox(label="result")
192
+ _cc_btn = gr.Button("chat_completions")
193
+ _cc_btn.click(
194
+ fn=chat_completions,
195
+ inputs=[_cc_messages, _cc_max_tokens, _cc_temp, _cc_top_p, _cc_thinking],
196
+ outputs=[_cc_out],
197
+ api_name="chat_completions",
198
+ )
199
 
200
 
201
  # ---------------------------------------------------------------------------