abidlabs HF staff commited on
Commit
fe50e4e
·
verified ·
1 Parent(s): 8a0c673

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from functools import partial
3
+ import gradio as gr
4
+ from huggingface_hub import InferenceClient
5
+
6
+ css = """
7
+ gradio-app {
8
+ background: none !important;
9
+ }
10
+
11
+ .md .container {
12
+ border:1px solid #ccc;
13
+ border-radius:5px;
14
+ min-height:300px;
15
+ color: #666;
16
+ display: flex;
17
+ justify-content: center;
18
+ align-items: center;
19
+ text-align: center;
20
+ font-family: monospace;
21
+ padding: 10px;
22
+ }
23
+
24
+ #hf_token_box {
25
+ transition: height 1s ease-out, opacity 1s ease-out;
26
+ }
27
+
28
+ #hf_token_box.abc {
29
+ height: 0;
30
+ opacity: 0;
31
+ overflow: hidden;
32
+ }
33
+
34
+ #generate_button {
35
+ transition: background-color 1s ease-out, color 1s ease-out; border-color 1s ease-out;
36
+ }
37
+
38
+ #generate_button.changed {
39
+ background: black !important;
40
+ border-color: black !important;
41
+ color: white !important;
42
+ }
43
+
44
+
45
+
46
+ """
47
+
48
+ system_prompt = """
49
+ 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.
50
+ """
51
+
52
+ code = """
53
+ ```python
54
+ SYSTEM_PROMPT = "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."
55
+ PROMPT = "{PROMPT}"
56
+ MODEL_NAME = "meta-llama/Meta-Llama-3-70b-Instruct" # or "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO" or "HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1"
57
+
58
+ messages = [
59
+ {"role": "system", "content": SYSTEM_PROMPT},
60
+ {"role": "user", "content": PROMPT}
61
+ ]
62
+ client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
63
+ for c in client.chat_completion(messages, max_tokens=200, stream=True):
64
+ token = c.choices[0].delta.content
65
+ print(token)
66
+ ```
67
+ """
68
+
69
+ def inference(prompt, hf_token, model, model_name):
70
+ messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}]
71
+ if hf_token.strip() == "":
72
+ hf_token = None
73
+ client = InferenceClient(model=model, token=hf_token)
74
+ tokens = f"**`{model_name}`**\n\n"
75
+ for completion in client.chat_completion(messages, max_tokens=200, stream=True):
76
+ token = completion.choices[0].delta.content
77
+ tokens += token
78
+ yield tokens
79
+
80
+ def random_prompt():
81
+ return random.choice([
82
+ "Give me 5 very different ways to say the following sentence: 'The quick brown fox jumps over the lazy dog.'",
83
+ "Write a summary of the plot of the movie 'Inception' using only emojis.",
84
+ "Write a sentence with the words 'serendipity', 'baguette', and 'C++'.",
85
+ "Explain the concept of 'quantum entanglement' to a 5-year-old.",
86
+ "Write a couplet about Python"
87
+ ])
88
+
89
+ with gr.Blocks(css=css, theme="NoCrypt/miku") as demo:
90
+ gr.Markdown("<center><h1>🔮 Open LLM Explorer</h1></center>")
91
+ 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 [Hugging Face Inference API](https://huggingface.co/docs/api-inference/en/index).")
92
+ prompt = gr.Textbox(random_prompt, lines=2, show_label=False, info="Type your prompt here.")
93
+ 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")
94
+ with gr.Group():
95
+ with gr.Row():
96
+ generate_btn = gr.Button(value="Generate", elem_id="generate_button", variant="primary", size="sm")
97
+ code_btn = gr.Button(value="View Code", elem_id="code_button", variant="secondary", size="sm")
98
+
99
+ with gr.Row() as output_row:
100
+ llama_output = gr.Markdown("<div class='container'>Llama 3-70B Instruct</div>", elem_classes=["md"], height=300)
101
+ nous_output = gr.Markdown("<div class='container'>Nous Hermes 2 Mixtral 8x7B DPO</div>", elem_classes=["md"], height=300)
102
+ zephyr_output = gr.Markdown("<div class='container'>Zephyr ORPO 141B A35B</div>", elem_classes=["md"], height=300)
103
+
104
+ with gr.Row(visible=False) as code_row:
105
+ code_display = gr.Markdown(code, elem_classes=["md"], height=300)
106
+
107
+ output_visible = gr.State(True)
108
+ code_btn.click(
109
+ lambda x: (not x, gr.Row(visible=not x), gr.Row(visible=x), "View Results" if x else "View Code"),
110
+ output_visible,
111
+ [output_visible, output_row, code_row, code_btn],
112
+ )
113
+
114
+ gr.on(
115
+ [prompt.submit, generate_btn.click],
116
+ None,
117
+ None,
118
+ None,
119
+ js="""
120
+ function disappear() {
121
+ var element = document.getElementById("hf_token_box");
122
+ var height = element.offsetHeight;
123
+ var step = height / 30; // Adjust this value to change the speed of disappearance
124
+ var padding_top = parseFloat(getComputedStyle(element).paddingTop); // Get initial padding
125
+ var padding_bottom = parseFloat(getComputedStyle(element).paddingBottom); // Get initial padding
126
+ var step_padding = padding_top / 30; // Adjust this value to change the speed of disappearance
127
+
128
+ var interval = setInterval(function() {
129
+ if (height > 0) {
130
+ height -= step;
131
+ element.style.height = height + "px";
132
+ padding_bottom -= step_padding;
133
+ element.style.paddingBottom = padding_bottom + "px";
134
+ console.log("height", height);
135
+ } else {
136
+ clearInterval(interval);
137
+ }
138
+ }, 20); // Adjust this value to change the smoothness of the animation
139
+ }
140
+ """
141
+ )
142
+
143
+ gr.on(
144
+ [prompt.submit, generate_btn.click],
145
+ partial(inference, model="meta-llama/Meta-Llama-3-70b-Instruct", model_name="Llama 3-70B Instruct"),
146
+ [prompt, hf_token_box],
147
+ llama_output,
148
+ show_progress="hidden"
149
+ )
150
+
151
+ gr.on(
152
+ [prompt.submit, generate_btn.click],
153
+ partial(inference, model="NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", model_name="Nous Hermes 2 Mixtral 8x7B DPO"),
154
+ [prompt, hf_token_box],
155
+ nous_output,
156
+ show_progress="hidden"
157
+ )
158
+
159
+ gr.on(
160
+ [prompt.submit, generate_btn.click],
161
+ partial(inference, model="HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1", model_name="Zephyr ORPO 141B A35B"),
162
+ [prompt, hf_token_box],
163
+ zephyr_output,
164
+ show_progress="hidden"
165
+ )
166
+
167
+ gr.on(
168
+ triggers=[prompt.submit, generate_btn.click],
169
+ fn=lambda x: (code.replace("{PROMPT}", x), True, gr.Row(visible=True), gr.Row(visible=False), "View Code"),
170
+ inputs=[prompt],
171
+ outputs=[code_display, output_visible, output_row, code_row, code_btn]
172
+ )
173
+
174
+
175
+ demo.launch()
176
+
177
+