sdkrastev commited on
Commit
c6c15ce
·
verified ·
1 Parent(s): c5dc213

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -113
app.py CHANGED
@@ -1,11 +1,10 @@
1
  import gradio as gr
2
  import spaces
3
  from huggingface_hub import InferenceClient
4
- from huggingface_hub.errors import HfHubHTTPError, InferenceTimeoutError
5
  from transformers import pipeline
6
 
7
  LOCAL_MODEL = "Qwen/Qwen3-0.6B"
8
- REMOTE_MODEL = "openai/nopenope"
9
 
10
  pipe = pipeline(
11
  "text-generation",
@@ -16,22 +15,18 @@ pipe = pipeline(
16
 
17
  fancy_css = """
18
  .gradio-container {
19
- width: 95% !important;
20
- max-width: 1400px !important;
21
- margin: 0 auto;
22
  }
23
-
24
  #app-title {
25
  text-align: center;
26
  margin-bottom: 4px;
27
  }
28
-
29
  #app-subtitle {
30
  text-align: center;
31
  color: var(--body-text-color-subdued);
32
  margin-bottom: 24px;
33
  }
34
-
35
  #chat-container {
36
  width: 100%;
37
  border: 1px solid var(--border-color-primary);
@@ -39,18 +34,15 @@ fancy_css = """
39
  padding: 16px;
40
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
41
  }
42
-
43
  #model-note {
44
  font-size: 0.9em;
45
  color: var(--body-text-color-subdued);
46
  margin-top: 8px;
47
  }
48
-
49
  @media (max-width: 768px) {
50
  .gradio-container {
51
  width: 98% !important;
52
  }
53
-
54
  #chat-container {
55
  padding: 8px;
56
  }
@@ -76,35 +68,6 @@ def local_generate(
76
  return outputs[0]["generated_text"][-1]["content"]
77
 
78
 
79
- def remote_generate(
80
- messages,
81
- max_tokens,
82
- temperature,
83
- top_p,
84
- hf_token,
85
- ):
86
- client = InferenceClient(
87
- token=hf_token.token,
88
- model=REMOTE_MODEL,
89
- )
90
-
91
- response = ""
92
-
93
- for chunk in client.chat_completion(
94
- messages,
95
- max_tokens=max_tokens,
96
- stream=True,
97
- temperature=temperature,
98
- top_p=top_p,
99
- ):
100
- choices = chunk.choices
101
-
102
- if choices and choices[0].delta.content:
103
- response += choices[0].delta.content
104
-
105
- yield response
106
-
107
-
108
  def respond(
109
  message,
110
  history: list[dict[str, str]],
@@ -112,14 +75,14 @@ def respond(
112
  max_tokens,
113
  temperature,
114
  top_p,
115
- model_mode,
116
  hf_token: gr.OAuthToken,
117
  ):
118
  messages = [{"role": "system", "content": system_message}]
119
  messages.extend(history)
120
  messages.append({"role": "user", "content": message})
121
 
122
- if model_mode == "Local Model":
123
  print("[MODE] local")
124
 
125
  response = local_generate(
@@ -129,76 +92,37 @@ def respond(
129
  top_p,
130
  )
131
 
132
- yield f"**Backend: Local Model**\n\n{response}"
133
  return
134
 
135
- if hf_token is None or not getattr(hf_token, "token", None):
136
- if model_mode == "Automatic Failover":
137
- print("[FAILOVER] No Hugging Face token. Using local model.")
138
-
139
- response = local_generate(
140
- messages,
141
- max_tokens,
142
- temperature,
143
- top_p,
144
- )
145
-
146
- yield (
147
- "**Remote model unavailable. "
148
- "Automatically switched to Local Model.**\n\n"
149
- + response
150
- )
151
- return
152
 
 
153
  yield "⚠️ Please log in with your Hugging Face account first."
154
  return
155
 
156
- if model_mode == "Remote API":
157
- print("[MODE] api")
158
-
159
- try:
160
- for response in remote_generate(
161
- messages,
162
- max_tokens,
163
- temperature,
164
- top_p,
165
- hf_token,
166
- ):
167
- yield f"**Backend: Remote API**\n\n{response}"
168
-
169
- except (InferenceTimeoutError, HfHubHTTPError) as error:
170
- yield f"⚠️ Remote API error: {error}"
171
-
172
- return
173
-
174
- print("[MODE] automatic")
175
 
176
- try:
177
- for response in remote_generate(
178
- messages,
179
- max_tokens,
180
- temperature,
181
- top_p,
182
- hf_token,
183
- ):
184
- yield f"**Backend: Remote API**\n\n{response}"
185
 
186
- except (InferenceTimeoutError, HfHubHTTPError) as error:
187
- print(f"[FAILOVER] Remote model failed: {error}")
188
- print("[FAILOVER] Switching to local model.")
 
 
 
 
 
 
189
 
190
- response = local_generate(
191
- messages,
192
- max_tokens,
193
- temperature,
194
- top_p,
195
- )
196
 
197
- yield (
198
- "**Remote model failed. "
199
- "Automatically switched to Local Model.**\n\n"
200
- + response
201
- )
202
 
203
 
204
  chatbot = gr.ChatInterface(
@@ -229,14 +153,9 @@ chatbot = gr.ChatInterface(
229
  step=0.05,
230
  label="Top-p (nucleus sampling)",
231
  ),
232
- gr.Radio(
233
- choices=[
234
- "Remote API",
235
- "Local Model",
236
- "Automatic Failover",
237
- ],
238
- value="Remote API",
239
- label="Model mode",
240
  ),
241
  ],
242
  )
@@ -252,7 +171,7 @@ with gr.Blocks(css=fancy_css) as demo:
252
  )
253
 
254
  gr.Markdown(
255
- "Compare a remotely hosted LLM with a model running locally on the Space.",
256
  elem_id="app-subtitle",
257
  )
258
 
@@ -260,8 +179,7 @@ with gr.Blocks(css=fancy_css) as demo:
260
  chatbot.render()
261
 
262
  gr.Markdown(
263
- "Use **Additional inputs** to choose between the remote API, "
264
- "the locally executed model, or automatic failover.",
265
  elem_id="model-note",
266
  )
267
 
 
1
  import gradio as gr
2
  import spaces
3
  from huggingface_hub import InferenceClient
 
4
  from transformers import pipeline
5
 
6
  LOCAL_MODEL = "Qwen/Qwen3-0.6B"
7
+ REMOTE_MODEL = "openai/gpt-oss-20b"
8
 
9
  pipe = pipeline(
10
  "text-generation",
 
15
 
16
  fancy_css = """
17
  .gradio-container {
18
+ width: 96% !important;
19
+ max-width: none !important;
 
20
  }
 
21
  #app-title {
22
  text-align: center;
23
  margin-bottom: 4px;
24
  }
 
25
  #app-subtitle {
26
  text-align: center;
27
  color: var(--body-text-color-subdued);
28
  margin-bottom: 24px;
29
  }
 
30
  #chat-container {
31
  width: 100%;
32
  border: 1px solid var(--border-color-primary);
 
34
  padding: 16px;
35
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
36
  }
 
37
  #model-note {
38
  font-size: 0.9em;
39
  color: var(--body-text-color-subdued);
40
  margin-top: 8px;
41
  }
 
42
  @media (max-width: 768px) {
43
  .gradio-container {
44
  width: 98% !important;
45
  }
 
46
  #chat-container {
47
  padding: 8px;
48
  }
 
68
  return outputs[0]["generated_text"][-1]["content"]
69
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  def respond(
72
  message,
73
  history: list[dict[str, str]],
 
75
  max_tokens,
76
  temperature,
77
  top_p,
78
+ use_local_model,
79
  hf_token: gr.OAuthToken,
80
  ):
81
  messages = [{"role": "system", "content": system_message}]
82
  messages.extend(history)
83
  messages.append({"role": "user", "content": message})
84
 
85
+ if use_local_model:
86
  print("[MODE] local")
87
 
88
  response = local_generate(
 
92
  top_p,
93
  )
94
 
95
+ yield response
96
  return
97
 
98
+ print("[MODE] api")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+ if hf_token is None or not getattr(hf_token, "token", None):
101
  yield "⚠️ Please log in with your Hugging Face account first."
102
  return
103
 
104
+ client = InferenceClient(
105
+ token=hf_token.token,
106
+ model=REMOTE_MODEL,
107
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ response = ""
 
 
 
 
 
 
 
 
110
 
111
+ for chunk in client.chat_completion(
112
+ messages,
113
+ max_tokens=max_tokens,
114
+ stream=True,
115
+ temperature=temperature,
116
+ top_p=top_p,
117
+ ):
118
+ choices = chunk.choices
119
+ token = ""
120
 
121
+ if len(choices) and choices[0].delta.content:
122
+ token = choices[0].delta.content
 
 
 
 
123
 
124
+ response += token
125
+ yield response
 
 
 
126
 
127
 
128
  chatbot = gr.ChatInterface(
 
153
  step=0.05,
154
  label="Top-p (nucleus sampling)",
155
  ),
156
+ gr.Checkbox(
157
+ label="Use Local Model",
158
+ value=False,
 
 
 
 
 
159
  ),
160
  ],
161
  )
 
171
  )
172
 
173
  gr.Markdown(
174
+ "A fancier version of the standard Huggging Face chatbot template.",
175
  elem_id="app-subtitle",
176
  )
177
 
 
179
  chatbot.render()
180
 
181
  gr.Markdown(
182
+ "Use **Additional inputs** to switch between the API model and the locally executed model.",
 
183
  elem_id="model-note",
184
  )
185