Ilyasch2 commited on
Commit
d7286d5
·
1 Parent(s): e4cef48
Files changed (1) hide show
  1. app.py +241 -64
app.py CHANGED
@@ -3,43 +3,183 @@ from datetime import date
3
  import gradio as gr
4
  import openai
5
 
 
6
  MODEL_CONFIGS = {
7
  "Falcon-H1-34B-Instruct": {
8
- "model_id": "tiiuae/Falcon-H1-34B-Instruct",
9
- "api_key_env": "XXL_API_KEY",
10
  "base_url_env": "XXL_URL",
 
 
11
  },
12
  "Falcon-H1-7B-Instruct": {
13
- "model_id": "tiiuae/Falcon-H1-7B-Instruct",
14
- "api_key_env": "L_API_KEY",
15
  "base_url_env": "L_URL",
 
 
16
  },
17
  "Falcon-H1-3B-Instruct": {
18
- "model_id": "tiiuae/Falcon-H1-3B-Instruct",
19
- "api_key_env": "M_API_KEY",
20
  "base_url_env": "M_URL",
 
 
21
  },
22
  "Falcon-H1-1.5B-Deep-Instruct": {
23
- "model_id": "tiiuae/Falcon-H1-1.5B-Deep-Instruct",
24
- "api_key_env": "S_API_KEY",
25
  "base_url_env": "S_URL",
 
 
26
  },
27
  "Falcon-H1-1.5B-Instruct": {
28
- "model_id": "tiiuae/Falcon-H1-1.5B-Instruct",
29
- "api_key_env": "XS_API_KEY",
30
  "base_url_env": "XS_URL",
 
 
31
  },
32
  "Falcon-H1-0.5B-Instruct": {
33
- "model_id": "tiiuae/Falcon-H1-0.5B-Instruct",
34
- "api_key_env": "XXS_API_KEY",
35
  "base_url_env": "XXS_URL",
 
 
36
  },
37
  }
38
 
39
  today = date.today()
40
- TITLE = "<h1><center>Private multi-backend playground</center></h1>"
41
- SUB_TITLE = "<center>Keys & endpoints stay server-side; the browser never sees them.</center>"
42
- CSS = """.duplicate-button{margin:auto!important;color:#fff!important;background:#000!important;border-radius:100vh!important}h3{text-align:center;}"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  def stream_chat(
45
  message: str,
@@ -48,71 +188,108 @@ def stream_chat(
48
  temperature: float = 0.7,
49
  max_new_tokens: int = 1024,
50
  top_p: float = 1.0,
51
- top_k: int = 20,
52
- penalty: float = 1.2,
53
  ):
54
- cfg = MODEL_CONFIGS[model_label]
55
-
56
- api_key = os.getenv(cfg["api_key_env"])
57
- base_url = os.getenv(cfg.get("base_url_env", ""), None)
58
-
 
 
 
59
  if not api_key:
60
  yield f"❌ Env-var `{cfg['api_key_env']}` not set."
61
  return
 
62
  if cfg.get("base_url_env") and not base_url:
63
  yield f"❌ Env-var `{cfg['base_url_env']}` not set."
64
  return
65
-
66
  client = openai.OpenAI(api_key=api_key, base_url=base_url)
67
- msgs=[]
 
68
  for u, a in history:
69
  msgs += [{"role": "user", "content": u},
70
- {"role": "assistant", "content": a}]
71
- msgs.append({"role": "user", "content": message})
72
-
73
- stream = client.chat.completions.create(
74
- model=cfg["model_id"],
75
- messages=msgs,
76
- temperature=temperature,
77
- top_p=top_p,
78
- max_tokens=max_new_tokens,
79
- presence_penalty=penalty,
80
- stream=True,
81
- )
82
-
83
- partial = ""
84
- for chunk in stream:
85
- if (delta := chunk.choices[0].delta).content:
86
- partial += delta.content
87
- yield partial
 
 
 
88
 
 
 
89
 
90
- chatbot = gr.Chatbot(height=600)
91
-
92
  with gr.Blocks(css=CSS, theme="soft") as demo:
93
- gr.HTML(TITLE)
94
- gr.HTML(SUB_TITLE)
 
 
 
 
 
95
  gr.DuplicateButton(value="Duplicate Space", elem_classes="duplicate-button")
96
-
97
- gr.ChatInterface(
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  fn=stream_chat,
99
  chatbot=chatbot,
100
  fill_height=True,
101
- additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
102
- additional_inputs=[
103
- gr.Dropdown(
104
- choices=list(MODEL_CONFIGS.keys()),
105
- value=list(MODEL_CONFIGS.keys())[0],
106
- label="Model",
107
- ),
108
- gr.Slider(0, 1, 0.1, 0.7, label="Temperature"),
109
- gr.Slider(64, 4096, 1, 1024, label="Max new tokens"),
110
- gr.Slider(0, 1, 0.05, 1.0, label="top_p"),
111
- gr.Slider(1, 20, 1, 20, label="top_k (ignored)"),
112
- gr.Slider(0, 2, 0.1, 1.2, label="Presence penalty"),
113
- ],
114
- cache_examples=False,
115
- )
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  if __name__ == "__main__":
118
- demo.launch()
 
3
  import gradio as gr
4
  import openai
5
 
6
+ # Model configuration dictionary
7
  MODEL_CONFIGS = {
8
  "Falcon-H1-34B-Instruct": {
9
+ "model_id": "tiiuae/Falcon-H1-34B-Instruct",
10
+ "api_key_env": "XXL_API_KEY",
11
  "base_url_env": "XXL_URL",
12
+ "badge_color": "red",
13
+ "description": "Largest model (34B parameters)"
14
  },
15
  "Falcon-H1-7B-Instruct": {
16
+ "model_id": "tiiuae/Falcon-H1-7B-Instruct",
17
+ "api_key_env": "L_API_KEY",
18
  "base_url_env": "L_URL",
19
+ "badge_color": "orange",
20
+ "description": "Large model (7B parameters)"
21
  },
22
  "Falcon-H1-3B-Instruct": {
23
+ "model_id": "tiiuae/Falcon-H1-3B-Instruct",
24
+ "api_key_env": "M_API_KEY",
25
  "base_url_env": "M_URL",
26
+ "badge_color": "yellow",
27
+ "description": "Medium model (3B parameters)"
28
  },
29
  "Falcon-H1-1.5B-Deep-Instruct": {
30
+ "model_id": "tiiuae/Falcon-H1-1.5B-Deep-Instruct",
31
+ "api_key_env": "S_API_KEY",
32
  "base_url_env": "S_URL",
33
+ "badge_color": "green",
34
+ "description": "Small model with deeper training (1.5B parameters)"
35
  },
36
  "Falcon-H1-1.5B-Instruct": {
37
+ "model_id": "tiiuae/Falcon-H1-1.5B-Instruct",
38
+ "api_key_env": "XS_API_KEY",
39
  "base_url_env": "XS_URL",
40
+ "badge_color": "blue",
41
+ "description": "Extra small model (1.5B parameters)"
42
  },
43
  "Falcon-H1-0.5B-Instruct": {
44
+ "model_id": "tiiuae/Falcon-H1-0.5B-Instruct",
45
+ "api_key_env": "XXS_API_KEY",
46
  "base_url_env": "XXS_URL",
47
+ "badge_color": "indigo",
48
+ "description": "Extra-extra small model (0.5B parameters)"
49
  },
50
  }
51
 
52
  today = date.today()
53
+
54
+ # Enhanced styling with modern, clean look
55
+ CSS = """
56
+ /* Overall theming */
57
+ body {
58
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
59
+ }
60
+
61
+ /* Header and title styling */
62
+ .header-container {
63
+ margin-bottom: 1.5rem;
64
+ text-align: center;
65
+ }
66
+
67
+ h1.main-title {
68
+ font-size: 2.5rem !important;
69
+ font-weight: 700 !important;
70
+ background: linear-gradient(90deg, #4776E6 0%, #8E54E9 100%);
71
+ -webkit-background-clip: text;
72
+ -webkit-text-fill-color: transparent;
73
+ margin-bottom: 0.5rem !important;
74
+ }
75
+
76
+ .subtitle {
77
+ font-size: 1.1rem;
78
+ color: #666;
79
+ margin-bottom: 1rem;
80
+ }
81
+
82
+ /* Model selection styling */
83
+ .model-dropdown label span:first-of-type {
84
+ font-weight: 600 !important;
85
+ font-size: 1.1rem !important;
86
+ }
87
+
88
+ /* Parameter styling */
89
+ .parameters-container {
90
+ border-radius: 12px !important;
91
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05) !important;
92
+ margin-top: 1rem !important;
93
+ overflow: hidden !important;
94
+ }
95
+
96
+ .parameters-header {
97
+ font-weight: 600 !important;
98
+ font-size: 1.1rem !important;
99
+ }
100
+
101
+ /* Slider styling */
102
+ .gradio-slider {
103
+ margin: 1rem 0 !important;
104
+ }
105
+
106
+ .gradio-slider label span:first-of-type {
107
+ font-weight: 500 !important;
108
+ }
109
+
110
+ /* Chatbot container */
111
+ .chatbot-container {
112
+ border-radius: 12px !important;
113
+ border: 1px solid #eaeaea !important;
114
+ }
115
+
116
+ /* Badge styling */
117
+ .model-badge {
118
+ display: inline-block;
119
+ padding: 2px 8px;
120
+ border-radius: 12px;
121
+ font-size: 0.8rem;
122
+ font-weight: 500;
123
+ margin-left: 8px;
124
+ color: white;
125
+ }
126
+
127
+ .badge-red { background-color: #e53e3e; }
128
+ .badge-orange { background-color: #ed8936; }
129
+ .badge-yellow { background-color: #d69e2e; }
130
+ .badge-green { background-color: #38a169; }
131
+ .badge-blue { background-color: #3182ce; }
132
+ .badge-indigo { background-color: #5a67d8; }
133
+
134
+ /* Duplicate button styling */
135
+ .duplicate-button {
136
+ margin: 1rem auto !important;
137
+ display: block !important;
138
+ color: #fff !important;
139
+ background: linear-gradient(90deg, #4776E6 0%, #8E54E9 100%) !important;
140
+ border-radius: 100vh !important;
141
+ padding: 0.5rem 1.5rem !important;
142
+ font-weight: 600 !important;
143
+ border: none !important;
144
+ transition: all 0.3s ease !important;
145
+ box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08) !important;
146
+ }
147
+
148
+ .duplicate-button:hover {
149
+ transform: translateY(-2px) !important;
150
+ box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08) !important;
151
+ }
152
+
153
+ /* Mode color indicators */
154
+ .mode-info {
155
+ display: flex;
156
+ justify-content: center;
157
+ gap: 20px;
158
+ margin: 1rem 0;
159
+ }
160
+
161
+ .mode-item {
162
+ display: flex;
163
+ align-items: center;
164
+ font-size: 0.85rem;
165
+ }
166
+
167
+ .mode-color {
168
+ width: 12px;
169
+ height: 12px;
170
+ border-radius: 50%;
171
+ margin-right: 6px;
172
+ }
173
+ """
174
+
175
+ def format_model_option(model_name):
176
+ """Format the model dropdown options with colored badges"""
177
+ config = MODEL_CONFIGS[model_name]
178
+ color = config["badge_color"]
179
+ description = config["description"]
180
+
181
+ # This creates a string with HTML that will be rendered in the dropdown
182
+ return f"{model_name} <span class='model-badge badge-{color}'>{description}</span>"
183
 
184
  def stream_chat(
185
  message: str,
 
188
  temperature: float = 0.7,
189
  max_new_tokens: int = 1024,
190
  top_p: float = 1.0,
191
+ presence_penalty: float = 1.2,
 
192
  ):
193
+ # Extract the model name from the formatted option
194
+ # The label now includes HTML, so we need to extract just the model name
195
+ model_name = model_label.split(" <span")[0] if "<span" in model_label else model_label
196
+
197
+ cfg = MODEL_CONFIGS[model_name]
198
+ api_key = os.getenv(cfg["api_key_env"])
199
+ base_url = os.getenv(cfg.get("base_url_env", ""), None)
200
+
201
  if not api_key:
202
  yield f"❌ Env-var `{cfg['api_key_env']}` not set."
203
  return
204
+
205
  if cfg.get("base_url_env") and not base_url:
206
  yield f"❌ Env-var `{cfg['base_url_env']}` not set."
207
  return
208
+
209
  client = openai.OpenAI(api_key=api_key, base_url=base_url)
210
+
211
+ msgs = []
212
  for u, a in history:
213
  msgs += [{"role": "user", "content": u},
214
+ {"role": "assistant", "content": a}]
215
+ msgs.append({"role": "user", "content": message})
216
+
217
+ try:
218
+ stream = client.chat.completions.create(
219
+ model=cfg["model_id"],
220
+ messages=msgs,
221
+ temperature=temperature,
222
+ top_p=top_p,
223
+ max_tokens=max_new_tokens,
224
+ presence_penalty=presence_penalty,
225
+ stream=True,
226
+ )
227
+
228
+ partial = ""
229
+ for chunk in stream:
230
+ if (delta := chunk.choices[0].delta).content:
231
+ partial += delta.content
232
+ yield partial
233
+ except Exception as e:
234
+ yield f"❌ Error: {str(e)}"
235
 
236
+ # Format model options for the dropdown
237
+ model_options = {format_model_option(model): model for model in MODEL_CONFIGS.keys()}
238
 
239
+ # Create the Gradio interface
 
240
  with gr.Blocks(css=CSS, theme="soft") as demo:
241
+ # Header section
242
+ with gr.Row(elem_classes="header-container"):
243
+ with gr.Column():
244
+ gr.HTML("<h1 class='main-title'>Falcon Playground</h1>")
245
+ gr.HTML("<p class='subtitle'>Private multi-backend interface • Keys & endpoints stay server-side</p>")
246
+ gr.HTML(f"<p class='subtitle' style='font-size: 0.9rem; color: #888;'>Today: {today.strftime('%B %d, %Y')}</p>")
247
+
248
  gr.DuplicateButton(value="Duplicate Space", elem_classes="duplicate-button")
249
+
250
+ # Info section with model size indicators
251
+ with gr.Row(elem_classes="mode-info"):
252
+ for model, config in MODEL_CONFIGS.items():
253
+ gr.HTML(f"""
254
+ <div class='mode-item'>
255
+ <div class='mode-color' style='background-color: var(--{config["badge_color"]}-500);'></div>
256
+ <div>{model.split('-')[-2]}</div>
257
+ </div>
258
+ """)
259
+
260
+ # Main chat interface
261
+ chatbot = gr.Chatbot(height=600, elem_classes="chatbot-container")
262
+
263
+ with gr.ChatInterface(
264
  fn=stream_chat,
265
  chatbot=chatbot,
266
  fill_height=True,
267
+ additional_inputs_accordion="closed",
268
+ ) as chat_interface:
269
+ # Model selection dropdown with styled options
270
+ model_dropdown = gr.Dropdown(
271
+ choices=list(model_options.keys()),
272
+ value=list(model_options.keys())[0],
273
+ label="Model Selection",
274
+ elem_classes="model-dropdown"
275
+ )
276
+
277
+ # Parameters in accordion
278
+ with gr.Accordion("⚙️ Advanced Parameters", open=False, elem_classes="parameters-container"):
279
+ with gr.Group():
280
+ temperature = gr.Slider(0, 1, value=0.7, step=0.05, label="Temperature", info="Higher values produce more diverse outputs")
281
+ max_new_tokens = gr.Slider(64, 4096*8, value=1024, step=64, label="Max new tokens", info="Maximum length of generated response")
282
+ top_p = gr.Slider(0, 1, value=1.0, step=0.05, label="Top-p (nucleus sampling)", info="1.0 means no filtering")
283
+ presence_penalty = gr.Slider(0, 2, value=1.2, step=0.1, label="Presence penalty", info="Penalizes repetition")
284
+
285
+ # Connect the inputs to the chat interface
286
+ chat_interface.additional_inputs = [
287
+ model_dropdown,
288
+ temperature,
289
+ max_new_tokens,
290
+ top_p,
291
+ presence_penalty
292
+ ]
293
 
294
  if __name__ == "__main__":
295
+ demo.launch()