Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import random
|
2 |
from functools import partial
|
3 |
import gradio as gr
|
@@ -53,8 +55,6 @@ function refresh() {
|
|
53 |
}
|
54 |
"""
|
55 |
|
56 |
-
|
57 |
-
|
58 |
system_prompt = """
|
59 |
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
60 |
"""
|
@@ -76,10 +76,23 @@ for c in client.chat_completion(messages, max_tokens=200, stream=True):
|
|
76 |
```
|
77 |
"""
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def inference(prompt, hf_token, model, model_name):
|
80 |
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}]
|
81 |
-
if hf_token.strip()
|
82 |
-
hf_token =
|
83 |
client = InferenceClient(model=model, token=hf_token)
|
84 |
tokens = f"**`{model_name}`**\n\n"
|
85 |
for completion in client.chat_completion(messages, max_tokens=200, stream=True):
|
@@ -98,9 +111,9 @@ def random_prompt():
|
|
98 |
|
99 |
with gr.Blocks(css=css, theme="NoCrypt/miku", js=js) as demo:
|
100 |
gr.Markdown("<center><h1>🔮 Open LLM Explorer</h1></center>")
|
101 |
-
gr.Markdown("Type your prompt below and compare results from the 3 leading open models from the [Open LLM Leaderboard](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) that are on the
|
102 |
prompt = gr.Textbox(random_prompt, lines=2, show_label=False, info="Type your prompt here.")
|
103 |
-
hf_token_box = gr.Textbox(lines=1, placeholder="Your Hugging Face token (not required, but a HF Pro account avoids rate limits):", show_label=False, elem_id="hf_token_box")
|
104 |
with gr.Group():
|
105 |
with gr.Row():
|
106 |
generate_btn = gr.Button(value="Generate", elem_id="generate_button", variant="primary", size="sm")
|
@@ -119,13 +132,17 @@ with gr.Blocks(css=css, theme="NoCrypt/miku", js=js) as demo:
|
|
119 |
lambda x: (not x, gr.Row(visible=not x), gr.Row(visible=x), "View Results" if x else "View Code"),
|
120 |
output_visible,
|
121 |
[output_visible, output_row, code_row, code_btn],
|
|
|
122 |
)
|
123 |
|
|
|
|
|
124 |
gr.on(
|
125 |
[prompt.submit, generate_btn.click],
|
126 |
None,
|
127 |
None,
|
128 |
None,
|
|
|
129 |
js="""
|
130 |
function disappear() {
|
131 |
var element = document.getElementById("hf_token_box");
|
@@ -152,36 +169,48 @@ with gr.Blocks(css=css, theme="NoCrypt/miku", js=js) as demo:
|
|
152 |
|
153 |
gr.on(
|
154 |
[prompt.submit, generate_btn.click],
|
|
|
|
|
|
|
155 |
partial(inference, model="meta-llama/Meta-Llama-3-70b-Instruct", model_name="Llama 3-70B Instruct"),
|
156 |
[prompt, hf_token_box],
|
157 |
llama_output,
|
158 |
-
show_progress="hidden"
|
|
|
159 |
)
|
160 |
|
161 |
gr.on(
|
162 |
[prompt.submit, generate_btn.click],
|
|
|
|
|
|
|
163 |
partial(inference, model="NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", model_name="Nous Hermes 2 Mixtral 8x7B DPO"),
|
164 |
[prompt, hf_token_box],
|
165 |
nous_output,
|
166 |
-
show_progress="hidden"
|
|
|
167 |
)
|
168 |
|
169 |
gr.on(
|
170 |
[prompt.submit, generate_btn.click],
|
|
|
|
|
171 |
partial(inference, model="HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1", model_name="Zephyr ORPO 141B A35B"),
|
172 |
[prompt, hf_token_box],
|
173 |
zephyr_output,
|
174 |
-
show_progress="hidden"
|
|
|
175 |
)
|
176 |
|
177 |
gr.on(
|
178 |
triggers=[prompt.submit, generate_btn.click],
|
179 |
fn=lambda x: (code.replace("{PROMPT}", x), True, gr.Row(visible=True), gr.Row(visible=False), "View Code"),
|
180 |
inputs=[prompt],
|
181 |
-
outputs=[code_display, output_visible, output_row, code_row, code_btn]
|
|
|
182 |
)
|
183 |
|
184 |
|
185 |
-
demo.launch()
|
186 |
|
187 |
|
|
|
1 |
+
import os
|
2 |
+
from datetime import datetime, timedelta
|
3 |
import random
|
4 |
from functools import partial
|
5 |
import gradio as gr
|
|
|
55 |
}
|
56 |
"""
|
57 |
|
|
|
|
|
58 |
system_prompt = """
|
59 |
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
60 |
"""
|
|
|
76 |
```
|
77 |
"""
|
78 |
|
79 |
+
ip_requests = {}
|
80 |
+
|
81 |
+
def allow_ip(request: gr.Request, show_error=True):
|
82 |
+
ip = request.client.host
|
83 |
+
now = datetime.now()
|
84 |
+
window = timedelta(hours=24)
|
85 |
+
if ip in ip_requests:
|
86 |
+
ip_requests[ip] = [timestamp for timestamp in ip_requests[ip] if now - timestamp < window]
|
87 |
+
if len(ip_requests.get(ip, [])) >= 15:
|
88 |
+
raise gr.Error("Rate limit exceeded. Please try again tomorrow or use your Hugging Face Pro token.", visible=show_error)
|
89 |
+
ip_requests.setdefault(ip, []).append(now)
|
90 |
+
return True
|
91 |
+
|
92 |
def inference(prompt, hf_token, model, model_name):
|
93 |
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}]
|
94 |
+
if hf_token is None or not hf_token.strip():
|
95 |
+
hf_token = os.getenv("HF_TOKEN")
|
96 |
client = InferenceClient(model=model, token=hf_token)
|
97 |
tokens = f"**`{model_name}`**\n\n"
|
98 |
for completion in client.chat_completion(messages, max_tokens=200, stream=True):
|
|
|
111 |
|
112 |
with gr.Blocks(css=css, theme="NoCrypt/miku", js=js) as demo:
|
113 |
gr.Markdown("<center><h1>🔮 Open LLM Explorer</h1></center>")
|
114 |
+
gr.Markdown("Every LLM has its own personality! Type your prompt below and compare results from the 3 leading open models from the [Open LLM Leaderboard](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) that are on the Hugging Face Inference API. You can sign up for [Hugging Face Pro](https://huggingface.co/pricing#pro) and get a token to avoid rate limits.")
|
115 |
prompt = gr.Textbox(random_prompt, lines=2, show_label=False, info="Type your prompt here.")
|
116 |
+
hf_token_box = gr.Textbox(lines=1, placeholder="Your Hugging Face token (not required, but a HF Pro account avoids rate limits):", show_label=False, elem_id="hf_token_box", type="password")
|
117 |
with gr.Group():
|
118 |
with gr.Row():
|
119 |
generate_btn = gr.Button(value="Generate", elem_id="generate_button", variant="primary", size="sm")
|
|
|
132 |
lambda x: (not x, gr.Row(visible=not x), gr.Row(visible=x), "View Results" if x else "View Code"),
|
133 |
output_visible,
|
134 |
[output_visible, output_row, code_row, code_btn],
|
135 |
+
api_name=False,
|
136 |
)
|
137 |
|
138 |
+
false = gr.State(False)
|
139 |
+
|
140 |
gr.on(
|
141 |
[prompt.submit, generate_btn.click],
|
142 |
None,
|
143 |
None,
|
144 |
None,
|
145 |
+
api_name=False,
|
146 |
js="""
|
147 |
function disappear() {
|
148 |
var element = document.getElementById("hf_token_box");
|
|
|
169 |
|
170 |
gr.on(
|
171 |
[prompt.submit, generate_btn.click],
|
172 |
+
allow_ip,
|
173 |
+
false,
|
174 |
+
).success(
|
175 |
partial(inference, model="meta-llama/Meta-Llama-3-70b-Instruct", model_name="Llama 3-70B Instruct"),
|
176 |
[prompt, hf_token_box],
|
177 |
llama_output,
|
178 |
+
show_progress="hidden",
|
179 |
+
api_name=False
|
180 |
)
|
181 |
|
182 |
gr.on(
|
183 |
[prompt.submit, generate_btn.click],
|
184 |
+
allow_ip,
|
185 |
+
false,
|
186 |
+
).success(
|
187 |
partial(inference, model="NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", model_name="Nous Hermes 2 Mixtral 8x7B DPO"),
|
188 |
[prompt, hf_token_box],
|
189 |
nous_output,
|
190 |
+
show_progress="hidden",
|
191 |
+
api_name=False
|
192 |
)
|
193 |
|
194 |
gr.on(
|
195 |
[prompt.submit, generate_btn.click],
|
196 |
+
allow_ip,
|
197 |
+
).success(
|
198 |
partial(inference, model="HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1", model_name="Zephyr ORPO 141B A35B"),
|
199 |
[prompt, hf_token_box],
|
200 |
zephyr_output,
|
201 |
+
show_progress="hidden",
|
202 |
+
api_name=False
|
203 |
)
|
204 |
|
205 |
gr.on(
|
206 |
triggers=[prompt.submit, generate_btn.click],
|
207 |
fn=lambda x: (code.replace("{PROMPT}", x), True, gr.Row(visible=True), gr.Row(visible=False), "View Code"),
|
208 |
inputs=[prompt],
|
209 |
+
outputs=[code_display, output_visible, output_row, code_row, code_btn],
|
210 |
+
api_name=False
|
211 |
)
|
212 |
|
213 |
|
214 |
+
demo.launch(show_api=False)
|
215 |
|
216 |
|