rubenroy commited on
Commit
785d10d
Β·
verified Β·
1 Parent(s): 795fa9e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +225 -0
app.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+
6
+ model_name = "rubenroy/Geneva-12B-GCv2-5m"
7
+
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.bfloat16,
11
+ device_map="auto"
12
+ )
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+
15
+ @spaces.GPU
16
+ def generate(message, chat_history, temperature=0.7, top_p=0.9, top_k=50, max_new_tokens=512, repetition_penalty=1.1):
17
+ messages = [
18
+ {"role": "system", "content": "You are a helpful assistant named Geneva, a 12 billion parameter Large Language Model, fine-tuned and trained by Ruben Roy. You have been trained with the GammaCorpus v2 dataset, a dataset filled with structured and filtered multi-turn conversations. This dataset was also made by Ruben Roy."}, # Attribution for Mistral removed to prevent unneccesary hallucinations.
19
+ ]
20
+
21
+ for user, assistant in chat_history:
22
+ messages.append({"role": "user", "content": user})
23
+ messages.append({"role": "assistant", "content": assistant})
24
+
25
+ messages.append({"role": "user", "content": message})
26
+
27
+ text = tokenizer.apply_chat_template(
28
+ messages,
29
+ tokenize=False,
30
+ add_generation_prompt=True
31
+ )
32
+
33
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
34
+
35
+ generated_ids = model.generate(
36
+ **model_inputs,
37
+ temperature=float(temperature),
38
+ top_p=float(top_p),
39
+ top_k=int(top_k),
40
+ max_new_tokens=int(max_new_tokens),
41
+ repetition_penalty=float(repetition_penalty),
42
+ do_sample=True if float(temperature) > 0 else False
43
+ )
44
+
45
+ generated_ids = [
46
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
47
+ ]
48
+
49
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
50
+ return response
51
+
52
+
53
+ TITLE_HTML = """
54
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
55
+ <style>
56
+ .model-btn {
57
+ background: linear-gradient(135deg, #059669 0%, #047857 100%);
58
+ color: white !important;
59
+ padding: 0.75rem 1rem;
60
+ border-radius: 0.5rem;
61
+ text-decoration: none !important;
62
+ font-weight: 500;
63
+ transition: all 0.2s ease;
64
+ font-size: 0.9rem;
65
+ display: flex;
66
+ align-items: center;
67
+ justify-content: center;
68
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
69
+ }
70
+ .model-btn:hover {
71
+ background: linear-gradient(135deg, #047857 0%, #065f46 100%);
72
+ box-shadow: 0 4px 6px rgba(0,0,0,0.2);
73
+ }
74
+ .model-section {
75
+ flex: 1;
76
+ max-width: 800px;
77
+ background: rgba(255, 255, 255, 0.05);
78
+ padding: 1.5rem;
79
+ border-radius: 1rem;
80
+ border: 1px solid rgba(255, 255, 255, 0.1);
81
+ backdrop-filter: blur(10px);
82
+ transition: all 0.3s ease;
83
+ }
84
+ .info-link {
85
+ color: #34d399;
86
+ text-decoration: none;
87
+ transition: color 0.2s ease;
88
+ }
89
+ .info-link:hover {
90
+ color: #6ee7b7;
91
+ text-decoration: underline;
92
+ }
93
+ .info-section {
94
+ margin-top: 0.5rem;
95
+ font-size: 0.9rem;
96
+ color: #94a3b8;
97
+ }
98
+ .settings-section {
99
+ background: rgba(255, 255, 255, 0.05);
100
+ padding: 1.5rem;
101
+ border-radius: 1rem;
102
+ margin: 1.5rem auto;
103
+ border: 1px solid rgba(255, 255, 255, 0.1);
104
+ max-width: 800px;
105
+ }
106
+ .settings-title {
107
+ color: #e2e8f0;
108
+ font-size: 1.25rem;
109
+ font-weight: 600;
110
+ margin-bottom: 1rem;
111
+ display: flex;
112
+ align-items: center;
113
+ gap: 0.7rem;
114
+ }
115
+ .parameter-info {
116
+ color: #94a3b8;
117
+ font-size: 0.8rem;
118
+ margin-top: 0.25rem;
119
+ }
120
+ </style>
121
+
122
+ <div style="background: linear-gradient(135deg, #064e3b 0%, #022c22 100%); padding: 1.5rem; border-radius: 1.5rem; text-align: center; margin: 1rem auto; max-width: 1200px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
123
+ <div style="margin-bottom: 1.5rem;">
124
+ <div style="display: flex; align-items: center; justify-content: center; gap: 1rem;">
125
+ <h1 style="font-size: 2.5rem; font-weight: 800; margin: 0; background: linear-gradient(135deg, #34d399 0%, #6ee7b7 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Geneva</h1>
126
+ <div style="width: 2px; height: 2.5rem; background: linear-gradient(180deg, #059669 0%, #34d399 100%);"></div>
127
+ <p style="font-size: 1.25rem; color: #94a3b8; margin: 0;">GammaCorpus v2-5m</p>
128
+ </div>
129
+ <div class="info-section">
130
+ <span>Fine-tuned from <a href="https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407" class="info-link">Mistral NeMo Instruct 2407</a> | Model: <a href="https://huggingface.co/rubenroy/Geneva-14B-GCv2-5m" class="info-link">Geneva-14B-GCv2-5m</a> | Training Dataset: <a href="https://huggingface.co/datasets/rubenroy/GammaCorpus-v2-5m" class="info-link">GammaCorpus v2 5m</a></span>
131
+ </div>
132
+ </div>
133
+
134
+ <div style="display: flex; gap: 1.5rem; justify-content: center;">
135
+ <div class="model-section">
136
+ <h2 style="font-size: 1.25rem; color: #e2e8f0; margin-bottom: 1.4rem; margin-top: 1px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 0.7rem;">
137
+ <i class="fas fa-sparkles"></i>
138
+ Geneva Models
139
+ </h2>
140
+ <div style="display: grid; grid-auto-flow: column; gap: 0.75rem; overflow-x: auto; white-space: nowrap;">
141
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-5m" class="model-btn">Geneva 12B GCv2 5m</a>
142
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-1m" class="model-btn">Geneva 12B GCv2 1m</a>
143
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-500k" class="model-btn">Geneva 12B GCv2 500k</a>
144
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-100k" class="model-btn">Geneva 12B GCv2 100k</a>
145
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-50k" class="model-btn">Geneva 12B GCv2 50k</a>
146
+ <a href="https://huggingface.co/rubenroy/Geneva-12B-GCv2-10k" class="model-btn">Geneva 12B GCv2 10k</a>
147
+ </div>
148
+ </div>
149
+ </div>
150
+ </div>
151
+ """
152
+
153
+ examples = [
154
+ ["Explain deep learning in simple terms."],
155
+ ["Write a short science fiction story."],
156
+ ["Describe the laws of thermodynamics."],
157
+ ["Write me a simple game in Python."]
158
+ ]
159
+
160
+ with gr.Blocks() as demo:
161
+ gr.HTML(TITLE_HTML)
162
+
163
+ with gr.Accordion("Generation Settings", open=False):
164
+ with gr.Row():
165
+ with gr.Column():
166
+ temperature = gr.Slider(
167
+ minimum=0.0,
168
+ maximum=2.0,
169
+ value=0.7,
170
+ step=0.1,
171
+ label="Temperature",
172
+ info="Higher values make the output more random, lower values make it more deterministic",
173
+ interactive=True
174
+ )
175
+ top_p = gr.Slider(
176
+ minimum=0.0,
177
+ maximum=1.0,
178
+ value=0.9,
179
+ step=0.05,
180
+ label="Top P",
181
+ info="Controls the cumulative probability threshold for nucleus sampling",
182
+ interactive=True
183
+ )
184
+ top_k = gr.Slider(
185
+ minimum=1,
186
+ maximum=100,
187
+ value=50,
188
+ step=1,
189
+ label="Top K",
190
+ info="Limits the number of tokens to consider for each generation step",
191
+ interactive=True
192
+ )
193
+ with gr.Column():
194
+ max_new_tokens = gr.Slider(
195
+ minimum=1,
196
+ maximum=2048,
197
+ value=512,
198
+ step=1,
199
+ label="Max New Tokens",
200
+ info="Maximum number of tokens to generate in the response",
201
+ interactive=True
202
+ )
203
+ repetition_penalty = gr.Slider(
204
+ minimum=1.0,
205
+ maximum=2.0,
206
+ value=1.1,
207
+ step=0.1,
208
+ label="Repetition Penalty",
209
+ info="Higher values stop the model from repeating the same info",
210
+ interactive=True
211
+ )
212
+
213
+ chatbot = gr.ChatInterface(
214
+ fn=generate,
215
+ additional_inputs=[
216
+ temperature,
217
+ top_p,
218
+ top_k,
219
+ max_new_tokens,
220
+ repetition_penalty
221
+ ],
222
+ examples=examples
223
+ )
224
+
225
+ demo.launch(share=True)