Ilyasch2 commited on
Commit
a6c84ea
·
1 Parent(s): b0ae0d3
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ 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,
46
+ history: list,
47
+ model_label: str,
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
+
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()