Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,54 @@
|
|
1 |
-
# Imports
|
2 |
-
import gradio as gr
|
3 |
-
import os
|
4 |
-
import random
|
5 |
-
import time
|
6 |
import spaces
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
return random.randint(0, MAX_SEED)
|
42 |
-
|
43 |
-
def generate(input=DEFAULT_INPUT, history=[], system=DEFAULT_SYSTEM, stream=False, temperature=1, top_p=0.95, top_k=50, rep_p=1.2, max_tokens=64, seed=None, separator=DEFAULT_SEPARATOR, stop_sequences=DEFAULT_STOP_SEQUENCES):
|
44 |
-
print("[GENERATE] Model is generating...")
|
45 |
-
|
46 |
-
memory = ""
|
47 |
-
for item in history:
|
48 |
-
if item[0]:
|
49 |
-
memory += f"{TAG_USER}: {item[0].strip()}\n"
|
50 |
-
if item[1]:
|
51 |
-
memory += f"{TAG_ASSISTANT}: {item[1].strip()}</s>\n"
|
52 |
-
prompt = f"{system.strip()}\n{memory}{TAG_USER}: {input.strip()}\n{TAG_ASSISTANT}: "
|
53 |
-
|
54 |
-
print(prompt)
|
55 |
-
|
56 |
-
parameters = {
|
57 |
-
"prompt": prompt,
|
58 |
-
"temperature": temperature,
|
59 |
-
"top_p": top_p,
|
60 |
-
"top_k": top_k,
|
61 |
-
"repeat_penalty": rep_p,
|
62 |
-
"max_tokens": max_tokens,
|
63 |
-
"stop": [seq.strip() for seq in stop_sequences.split(separator)] if stop_sequences else [],
|
64 |
-
"seed": get_seed(seed),
|
65 |
-
"stream": stream
|
66 |
-
}
|
67 |
-
|
68 |
-
event = threading.Event()
|
69 |
-
|
70 |
-
try:
|
71 |
-
output = model.create_completion(**parameters)
|
72 |
-
print("[GENERATE] Model has generated.")
|
73 |
-
if stream:
|
74 |
-
buffer = ""
|
75 |
-
timer = threading.Timer(TIMEOUT, event.set)
|
76 |
-
timer.start()
|
77 |
-
try:
|
78 |
-
for _, item in enumerate(output):
|
79 |
-
if event.is_set():
|
80 |
-
raise TimeoutError("[ERROR] Generation timed out.")
|
81 |
-
buffer += item["choices"][0]["text"]
|
82 |
-
yield buffer
|
83 |
-
timer.cancel()
|
84 |
-
timer = threading.Timer(TIMEOUT, event.set)
|
85 |
-
timer.start()
|
86 |
-
finally:
|
87 |
-
timer.cancel()
|
88 |
-
else:
|
89 |
-
yield output["choices"][0]["text"]
|
90 |
-
except TimeoutError as e:
|
91 |
-
yield str(e)
|
92 |
-
finally:
|
93 |
-
timer.cancel()
|
94 |
-
|
95 |
-
@spaces.GPU(duration=15)
|
96 |
-
def gpu():
|
97 |
-
return
|
98 |
-
|
99 |
-
# Initialize
|
100 |
-
theme = gr.themes.Default(
|
101 |
-
primary_hue="violet",
|
102 |
-
secondary_hue="indigo",
|
103 |
-
neutral_hue="zinc",
|
104 |
-
spacing_size="sm",
|
105 |
-
radius_size="lg",
|
106 |
-
font=[gr.themes.GoogleFont('Kanit'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
107 |
-
font_mono=[gr.themes.GoogleFont('Kanit'), 'ui-monospace', 'Consolas', 'monospace'],
|
108 |
-
).set(background_fill_primary='*neutral_50', background_fill_secondary='*neutral_100')
|
109 |
|
110 |
-
|
111 |
-
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
gr.Markdown("# ποΈβπ¨οΈ WizardLM")
|
116 |
-
gr.Markdown("β β β’ β‘ A text generation inference for one of the best open-source text models: WizardLM-2-8x22B.")
|
117 |
-
gr.Markdown("β β β’ β οΈ WARNING! The inference is very slow due to the model being HUGE; it takes 10 seconds before it starts generating; please avoid high max token parameters and sending large amounts of text; note it uses CPU because I cannot figure out how to run it in GPU without overloading the model.")
|
118 |
-
gr.Markdown(f"β β β’ π Link to models: {model_base} (BASE), {model_quant} (QUANT)")
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
gr.Slider(minimum=0.01, maximum=0.99, step=0.01, value=0.95, label="𧲠Top P", render=False),
|
129 |
-
gr.Slider(minimum=1, maximum=2048, step=1, value=50, label="π Top K", render=False),
|
130 |
-
gr.Slider(minimum=0.01, maximum=2, step=0.01, value=1.2, label="π Repetition Penalty", render=False),
|
131 |
-
gr.Slider(minimum=1, maximum=2048, step=1, value=256, label="β³ Max New Tokens", render=False),
|
132 |
-
gr.Textbox(lines=1, value="", label="π± Seed (Blank for random)", render=False),
|
133 |
-
gr.Textbox(lines=1, value=DEFAULT_SEPARATOR, label="π·οΈ Stop Sequences Separator", render=False),
|
134 |
-
gr.Textbox(lines=1, value=DEFAULT_STOP_SEQUENCES, label="π Stop Sequences (Blank for none)", render=False),
|
135 |
-
]
|
136 |
-
)
|
137 |
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from auto_gptq import AutoGPTQForCausalLM
|
5 |
+
|
6 |
+
# Model identifier
|
7 |
+
model_id = "xmadai/Mistral-Large-Instruct-2407-xMADai-INT4"
|
8 |
+
|
9 |
+
# Load the tokenizer
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
11 |
+
|
12 |
+
@spaces.DynamicGpuSupport()
|
13 |
+
class ModelWrapper:
|
14 |
+
def __init__(self):
|
15 |
+
self.model = None # Model will be loaded when GPU is allocated
|
16 |
+
|
17 |
+
@spaces.GPU
|
18 |
+
def generate(self, prompt):
|
19 |
+
if self.model is None:
|
20 |
+
# Load the model when GPU is allocated
|
21 |
+
self.model = AutoGPTQForCausalLM.from_quantized(
|
22 |
+
model_id,
|
23 |
+
device_map='auto',
|
24 |
+
trust_remote_code=True,
|
25 |
+
)
|
26 |
+
|
27 |
+
# Tokenize the input prompt
|
28 |
+
inputs = tokenizer(prompt, return_tensors='pt').to('cuda')
|
29 |
+
|
30 |
+
# Generate text
|
31 |
+
outputs = self.model.generate(
|
32 |
+
**inputs,
|
33 |
+
do_sample=True,
|
34 |
+
max_new_tokens=512
|
35 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Decode the generated text
|
38 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
return generated_text
|
40 |
|
41 |
+
# Instantiate the model wrapper
|
42 |
+
model_wrapper = ModelWrapper()
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Create the Gradio interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=model_wrapper.generate,
|
47 |
+
inputs=gr.Textbox(lines=5, label="Input Prompt"),
|
48 |
+
outputs=gr.Textbox(label="Generated Text"),
|
49 |
+
title="Mistral Generator",
|
50 |
+
description="Enter a prompt and receive a text completion using the Mistral model."
|
51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
if __name__ == "__main__":
|
54 |
+
interface.launch()
|