mpekpe commited on
Commit
370a38d
·
verified ·
1 Parent(s): dda1921

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import random
4
+ from functools import partial
5
+ from huggingface_hub import InferenceClient
6
+ import threading
7
+
8
+ system_prompt = """
9
+ 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.
10
+ """
11
+
12
+ code = """
13
+ '''python
14
+ from huggingface_hub import InferenceClient
15
+ 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."
16
+ PROMPT = "{PROMPT}"
17
+ message = [
18
+ {"role": "system", "content": system_prompt},
19
+ {"role":"user","content": prompt}
20
+ ]
21
+ client = InferenceClient(model=model, token=hf_token)
22
+ tokens=f"**'{model_name}'**\n\n"
23
+ for completion in client.chat_completion(messages, max_tokens=1000, stream=True):
24
+ token = completion.choices[0].delta.content
25
+ tokens += token
26
+ yield tokens
27
+ '''
28
+ """
29
+
30
+
31
+ def inference(prompt, hf_token, model, model_name):
32
+ messages = [{"role": "system", "content": system_prompt},{"role":"user","content": prompt}]
33
+ if hf_token is None or not hf_token.strip():
34
+ hf_token = os.getenv("HF_TOKEN")
35
+ client = InferenceClient(model=model, token=hf_token)
36
+ tokens=f"**'{model_name}'**\n\n"
37
+ for completion in client.chat_completion(messages, max_tokens=1000, stream=True):
38
+ token = completion.choices[0].delta.content
39
+ tokens += token
40
+ yield tokens
41
+
42
+ def hide_textbox():
43
+ return gr.Textbox(visible=False)
44
+
45
+
46
+
47
+ def random_prompt():
48
+ return random.choice([
49
+ "Rédigez une lettre officielle pour demander un rendez-vous à un responsable administratif.",
50
+ "Expliquez comment créer un budget mensuel simple pour mieux gérer ses finances personnelles.",
51
+ "Rédigez une recette détaillée pour préparer un plat équilibré et rapide, comme une salade de quinoa aux légumes grillés.",
52
+ "Proposez une routine matinale de 30 minutes pour améliorer son bien-être physique et mental.",
53
+ "Expliquez comment rédiger un CV attractif pour postuler à un emploi dans le domaine technologique.",
54
+ "Créez un guide étape par étape pour apprendre à méditer et réduire le stress.",
55
+ "Rédigez une lettre de motivation pour candidater à une formation professionnelle.",
56
+ "Donnez les instructions pour fabriquer un meuble simple en bois recyclé pour débutants.",
57
+ "Expliquez comment organiser un espace de travail à domicile ergonomique et propice à la concentration.",
58
+ "Rédigez une recette simple pour faire du pain maison avec des ingrédients de base."
59
+ ])
60
+
61
+ with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
62
+ gr.Markdown("<center><h1>Open LLM Explorer</h1></center>")
63
+ gr.Markdown("Chaque LLM a sa specificite")
64
+
65
+ prompt = gr.Textbox(value=random_prompt, label="Prompt", lines=3, max_lines=10)
66
+ token = gr.Textbox(label="Hugging face Token", type ="password", placeholder="Your Hugging Face token (not required, but a HF Pro account avoids rate limits)")
67
+
68
+ with gr.Group():
69
+ with gr.Row():
70
+ generate_btn = gr.Button("Execute",variant="primary", size="sm")
71
+ code_btn = gr.Button("View code", variant="secondary", size="sm")
72
+
73
+ with gr.Row() as output_row:
74
+ llama_output = gr.Markdown("Meta-Llama-3-8B-Instruct")
75
+ nous_output = gr.Markdown("Nous Mermes 2 Mixtral 8x7B DPO")
76
+ pythia_output = gr.Markdown("Qwen2.5-72B-Instruct")
77
+
78
+ code_markdown = gr.Markdown(visible=False)
79
+
80
+ gr.on(
81
+ triggers=[prompt.submit, generate_btn.click],
82
+ fn=hide_textbox,
83
+ inputs=None,
84
+ outputs=[token],
85
+ )
86
+
87
+
88
+
89
+
90
+ gr.on(
91
+ triggers=[prompt.submit, generate_btn.click],
92
+ fn=partial(inference, model="meta-llama/Meta-Llama-3-8B-Instruct", model_name="Meta-Llama-3-8B-Instruct"),
93
+ inputs=[prompt, token],
94
+ outputs=[llama_output],
95
+ show_progress="hidden"
96
+ )
97
+
98
+ gr.on(
99
+ triggers=[prompt.submit, generate_btn.click],
100
+ fn=partial(inference, model="NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", model_name="Nous Hermes 2 Mixtral 8x7B DPO"),
101
+ inputs=[prompt, token],
102
+ outputs=[nous_output],
103
+ show_progress="hidden"
104
+ )
105
+
106
+ gr.on(
107
+ triggers=[prompt.submit, generate_btn.click],
108
+ fn=partial(inference, model="Qwen/Qwen2.5-72B-Instruct", model_name="Qwen2.5-72B-Instruct"),
109
+ inputs =[prompt, token],
110
+ outputs=[pythia_output],
111
+ show_progress="hidden"
112
+ )
113
+
114
+ code_btn.click(
115
+ lambda : gr.Row(visible=False),
116
+ None,
117
+ output_row
118
+ ).then(
119
+ fn=lambda x: code.replace("{PROMPT",x),
120
+ inputs=[prompt],
121
+ outputs=[code_markdown]
122
+ ).then(
123
+ lambda : gr.Markdown(visible=True),
124
+ None,
125
+ code_markdown
126
+ )
127
+
128
+ demo.launch()