Bellamy66 commited on
Commit
f936ab1
Β·
verified Β·
1 Parent(s): 678c1f3

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +80 -0
app (1).py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
5
+ from threading import Thread
6
+
7
+ torch.set_default_device("cuda")
8
+
9
+ # Loading the tokenizer and model from Hugging Face's model hub.
10
+ tokenizer = AutoTokenizer.from_pretrained(
11
+ "mlabonne/phixtral-2x2_8",
12
+ trust_remote_code=True
13
+ )
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ "mlabonne/phixtral-2x2_8",
16
+ torch_dtype="auto",
17
+ load_in_8bit=True,
18
+ trust_remote_code=True
19
+ )
20
+
21
+ # Defining a custom stopping criteria class for the model's text generation.
22
+ class StopOnTokens(StoppingCriteria):
23
+ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
24
+ stop_ids = [50256, 50295] # IDs of tokens where the generation should stop.
25
+ for stop_id in stop_ids:
26
+ if input_ids[0][-1] == stop_id: # Checking if the last generated token is a stop token.
27
+ return True
28
+ return False
29
+
30
+
31
+ # Function to generate model predictions.
32
+ def predict(message, history):
33
+ history_transformer_format = history + [[message, ""]]
34
+ stop = StopOnTokens()
35
+
36
+ # Formatting the input for the model.
37
+ system_prompt = "<|im_start|>system\nYou are Phixtral, a helpful AI assistant.<|im_end|>"
38
+ messages = system_prompt + "".join(["".join(["\n<|im_start|>user\n" + item[0], "<|im_end|>\n<|im_start|>assistant\n" + item[1]]) for item in history_transformer_format])
39
+ print(messages)
40
+ input_ids = tokenizer([messages], return_tensors="pt").to('cuda')
41
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
42
+ generate_kwargs = dict(
43
+ input_ids,
44
+ streamer=streamer,
45
+ max_new_tokens=1024,
46
+ do_sample=True,
47
+ top_p=0.95,
48
+ top_k=50,
49
+ temperature=0.7,
50
+ num_beams=1,
51
+ stopping_criteria=StoppingCriteriaList([stop])
52
+ )
53
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
54
+ t.start() # Starting the generation in a separate thread.
55
+ partial_message = ""
56
+ for new_token in streamer:
57
+ partial_message += new_token
58
+ if '<|im_end|>' in partial_message: # Breaking the loop if the stop token is generated.
59
+ break
60
+ yield partial_message
61
+
62
+
63
+ # Setting up the Gradio chat interface.
64
+ gr.ChatInterface(predict,
65
+ description="""
66
+ <center><img src="https://i.imgur.com/CJSeIGg.png" width="33%"></center>\n\n
67
+ Chat with [mlabonne/phixtral-2x2_8](https://huggingface.co/mlabonne/phixtral-2x2_8), the first Mixture of Experts made by merging two fine-tuned [microsoft/phi-2](https://huggingface.co/microsoft/phi-2) models.
68
+ This small model (4.46B param) is good for various tasks, such as programming, dialogues, story writing, and more.\n\n
69
+ ❀️ If you like this work, please follow me on [Hugging Face](https://huggingface.co/mlabonne) and [Twitter](https://twitter.com/maximelabonne).
70
+ """,
71
+ examples=[
72
+ 'Can you solve the equation 2x + 3 = 11 for x?',
73
+ 'Write an epic poem about Ancient Rome.',
74
+ 'Who was the first person to walk on the Moon?',
75
+ 'Use a list comprehension to create a list of squares for numbers from 1 to 10.',
76
+ 'Recommend some popular science fiction books.',
77
+ 'Can you write a short story about a time-traveling detective?'
78
+ ],
79
+ theme=gr.themes.Soft(primary_hue="orange"),
80
+ ).launch()