rubenroy commited on
Commit
f196196
Β·
verified Β·
1 Parent(s): 61c26ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -154
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import gradio as gr
2
  import spaces
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import torch
 
 
5
 
6
  model_name = "rubenroy/Zurich-14B-GCv2-5m"
 
 
7
  model = AutoModelForCausalLM.from_pretrained(
8
  model_name,
9
  torch_dtype=torch.bfloat16,
@@ -12,31 +16,41 @@ model = AutoModelForCausalLM.from_pretrained(
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
 
14
  @spaces.GPU
15
- def generate(message, chat_history, temperature=0.7, top_p=0.9, top_k=50, max_new_tokens=512, repetition_penalty=1.1):
16
- messages = [
17
- {"role": "system", "content": "You are a helpul assistant named Zurich, a 7 billion parameter Large Language model, you were 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 was also made by Ruben Roy."},
18
- {"role": "user", "content": message}
19
- ]
20
- text = tokenizer.apply_chat_template(
21
- messages,
22
- tokenize=False,
23
- add_generation_prompt=True
24
- )
25
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
26
- generated_ids = model.generate(
27
- **model_inputs,
28
- temperature=float(temperature),
29
- top_p=float(top_p),
30
- top_k=int(top_k),
31
- max_new_tokens=int(max_new_tokens),
32
- repetition_penalty=float(repetition_penalty),
33
- do_sample=True if float(temperature) > 0 else False
 
 
 
 
 
 
 
34
  )
35
- generated_ids = [
36
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
37
- ]
38
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
39
- return response
 
 
 
40
 
41
  TITLE_HTML = """
42
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
@@ -69,85 +83,12 @@ TITLE_HTML = """
69
  backdrop-filter: blur(10px);
70
  transition: all 0.3s ease;
71
  }
72
- .info-link {
73
- color: #60a5fa;
74
- text-decoration: none;
75
- transition: color 0.2s ease;
76
- }
77
- .info-link:hover {
78
- color: #93c5fd;
79
- text-decoration: underline;
80
- }
81
- .info-section {
82
- margin-top: 0.5rem;
83
- font-size: 0.9rem;
84
- color: #94a3b8;
85
- }
86
- .settings-section {
87
- background: rgba(255, 255, 255, 0.05);
88
- padding: 1.5rem;
89
- border-radius: 1rem;
90
- margin: 1.5rem auto;
91
- border: 1px solid rgba(255, 255, 255, 0.1);
92
- max-width: 800px;
93
- }
94
- .settings-title {
95
- color: #e2e8f0;
96
- font-size: 1.25rem;
97
- font-weight: 600;
98
- margin-bottom: 1rem;
99
- display: flex;
100
- align-items: center;
101
- gap: 0.7rem;
102
- }
103
- .parameter-info {
104
- color: #94a3b8;
105
- font-size: 0.8rem;
106
- margin-top: 0.25rem;
107
- }
108
  </style>
109
 
110
  <div style="background: linear-gradient(135deg, #1e293b 0%, #0f172a 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);">
111
  <div style="margin-bottom: 1.5rem;">
112
- <div style="display: flex; align-items: center; justify-content: center; gap: 1rem;">
113
- <h1 style="font-size: 2.5rem; font-weight: 800; margin: 0; background: linear-gradient(135deg, #60a5fa 0%, #93c5fd 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Zurich</h1>
114
- <div style="width: 2px; height: 2.5rem; background: linear-gradient(180deg, #3b82f6 0%, #60a5fa 100%);"></div>
115
- <p style="font-size: 1.25rem; color: #94a3b8; margin: 0;">GammaCorpus v2-5m</p>
116
- </div>
117
- <div class="info-section">
118
- <span>Fine-tuned from <a href="https://huggingface.co/Qwen/Qwen2.5-14B-Instruct" class="info-link">Qwen 2.5 14B Instruct</a> | Model: <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-5m" class="info-link">Zurich-14B-GCv2-5m</a> | Training Dataset: <a href="https://huggingface.co/datasets/rubenroy/GammaCorpus-v2-5m" class="info-link">GammaCorpus v2 5m</a></span>
119
- </div>
120
- </div>
121
-
122
- <div style="display: flex; gap: 1.5rem; justify-content: center;">
123
- <div class="model-section">
124
- <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;">
125
- <i class="fas fa-brain"></i>
126
- 7B Models
127
- </h2>
128
- <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;">
129
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-5m" class="model-btn">Zurich 7B GCv2 5m</a>
130
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-1m" class="model-btn">Zurich 7B GCv2 1m</a>
131
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-500k" class="model-btn">Zurich 7B GCv2 500k</a>
132
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-100k" class="model-btn">Zurich 7B GCv2 100k</a>
133
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-50k" class="model-btn">Zurich 7B GCv2 50k</a>
134
- <a href="https://huggingface.co/rubenroy/Zurich-7B-GCv2-10k" class="model-btn">Zurich 7B GCv2 10k</a>
135
- </div>
136
- </div>
137
- <div class="model-section">
138
- <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;">
139
- <i class="fas fa-rocket"></i>
140
- 14B Models
141
- </h2>
142
- <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem;">
143
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-5m" class="model-btn">Zurich 14B GCv2 5m</a>
144
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-1m" class="model-btn">Zurich 14B GCv2 1m</a>
145
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-500k" class="model-btn">Zurich 14B GCv2 500k</a>
146
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-100k" class="model-btn">Zurich 14B GCv2 100k</a>
147
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-50k" class="model-btn">Zurich 14B GCv2 50k</a>
148
- <a href="https://huggingface.co/rubenroy/Zurich-14B-GCv2-10k" class="model-btn">Zurich 14B GCv2 10k</a>
149
- </div>
150
- </div>
151
  </div>
152
  </div>
153
  """
@@ -156,7 +97,7 @@ examples = [
156
  ["Explain quantum computing in simple terms"],
157
  ["Write a short story about a time traveler"],
158
  ["Explain the process of photosynthesis"],
159
- ["Tell me an intersting fact about Palm trees"]
160
  ]
161
 
162
  with gr.Blocks() as demo:
@@ -165,62 +106,16 @@ with gr.Blocks() as demo:
165
  with gr.Accordion("Generation Settings", open=False):
166
  with gr.Row():
167
  with gr.Column():
168
- temperature = gr.Slider(
169
- minimum=0.0,
170
- maximum=2.0,
171
- value=0.7,
172
- step=0.1,
173
- label="Temperature",
174
- info="Higher values make the output more random, lower values make it more deterministic",
175
- interactive=True
176
- )
177
- top_p = gr.Slider(
178
- minimum=0.0,
179
- maximum=1.0,
180
- value=0.9,
181
- step=0.05,
182
- label="Top P",
183
- info="Controls the cumulative probability threshold for nucleus sampling",
184
- interactive=True
185
- )
186
- top_k = gr.Slider(
187
- minimum=1,
188
- maximum=100,
189
- value=50,
190
- step=1,
191
- label="Top K",
192
- info="Limits the number of tokens to consider for each generation step",
193
- interactive=True
194
- )
195
  with gr.Column():
196
- max_new_tokens = gr.Slider(
197
- minimum=1,
198
- maximum=2048,
199
- value=512,
200
- step=1,
201
- label="Max New Tokens",
202
- info="Maximum number of tokens to generate in the response",
203
- interactive=True
204
- )
205
- repetition_penalty = gr.Slider(
206
- minimum=1.0,
207
- maximum=2.0,
208
- value=1.1,
209
- step=0.1,
210
- label="Repetition Penalty",
211
- info="Higher values stop the model from repeating the same info",
212
- interactive=True
213
- )
214
-
215
  chatbot = gr.ChatInterface(
216
  fn=generate,
217
- additional_inputs=[
218
- temperature,
219
- top_p,
220
- top_k,
221
- max_new_tokens,
222
- repetition_penalty
223
- ],
224
  examples=examples
225
  )
226
 
 
1
  import gradio as gr
2
  import spaces
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
  import torch
5
+ from threading import Thread
6
+ from typing import Iterator
7
 
8
  model_name = "rubenroy/Zurich-14B-GCv2-5m"
9
+ MAX_INPUT_TOKEN_LENGTH = 4096
10
+
11
  model = AutoModelForCausalLM.from_pretrained(
12
  model_name,
13
  torch_dtype=torch.bfloat16,
 
16
  tokenizer = AutoTokenizer.from_pretrained(model_name)
17
 
18
  @spaces.GPU
19
+ def generate(message: str, chat_history: list[tuple[str, str]], temperature=0.7, top_p=0.9, top_k=50, max_new_tokens=512, repetition_penalty=1.1) -> Iterator[str]:
20
+ """Generates text responses using Zurich model with streaming."""
21
+
22
+ conversation = []
23
+ for user, assistant in chat_history:
24
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
25
+ conversation.append({"role": "user", "content": message})
26
+
27
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
28
+
29
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
30
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
31
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
32
+
33
+ input_ids = input_ids.to(model.device)
34
+
35
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
36
+ generate_kwargs = dict(
37
+ {"input_ids": input_ids},
38
+ streamer=streamer,
39
+ max_new_tokens=max_new_tokens,
40
+ do_sample=True if float(temperature) > 0 else False,
41
+ top_p=top_p,
42
+ top_k=top_k,
43
+ temperature=temperature,
44
+ repetition_penalty=repetition_penalty
45
  )
46
+
47
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
48
+ t.start()
49
+
50
+ outputs = []
51
+ for text in streamer:
52
+ outputs.append(text)
53
+ yield "".join(outputs)
54
 
55
  TITLE_HTML = """
56
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
 
83
  backdrop-filter: blur(10px);
84
  transition: all 0.3s ease;
85
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  </style>
87
 
88
  <div style="background: linear-gradient(135deg, #1e293b 0%, #0f172a 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);">
89
  <div style="margin-bottom: 1.5rem;">
90
+ <h1 style="font-size: 2.5rem; font-weight: 800; margin: 0; background: linear-gradient(135deg, #60a5fa 0%, #93c5fd 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Zurich</h1>
91
+ <p style="font-size: 1.25rem; color: #94a3b8; margin: 0;">GammaCorpus v2-5m</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  </div>
93
  </div>
94
  """
 
97
  ["Explain quantum computing in simple terms"],
98
  ["Write a short story about a time traveler"],
99
  ["Explain the process of photosynthesis"],
100
+ ["Tell me an interesting fact about Palm trees"]
101
  ]
102
 
103
  with gr.Blocks() as demo:
 
106
  with gr.Accordion("Generation Settings", open=False):
107
  with gr.Row():
108
  with gr.Column():
109
+ temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.1, label="Temperature", info="Higher values make the output more random")
110
+ top_p = gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top P", info="Controls nucleus sampling")
111
+ top_k = gr.Slider(1, 100, value=50, step=1, label="Top K", info="Limits vocabulary choices per step")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  with gr.Column():
113
+ max_new_tokens = gr.Slider(1, 2048, value=512, step=1, label="Max New Tokens", info="Limits response length")
114
+ repetition_penalty = gr.Slider(1.0, 2.0, value=1.1, step=0.1, label="Repetition Penalty", info="Discourages repeated phrases")
115
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  chatbot = gr.ChatInterface(
117
  fn=generate,
118
+ additional_inputs=[temperature, top_p, top_k, max_new_tokens, repetition_penalty],
 
 
 
 
 
 
119
  examples=examples
120
  )
121