samir-fama commited on
Commit
5c1f5a0
·
1 Parent(s): 0e9bfd4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+
4
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
5
+
6
+ def format_prompt(new_message, history):
7
+ prompt = "<s>"
8
+ for user_msg, bot_msg in history:
9
+ prompt += f"[INST] {user_msg} [/INST]"
10
+ prompt += f" {bot_msg}</s> "
11
+ prompt += f"[INST] {new_message} [/INST]"
12
+ return prompt
13
+
14
+ def generate(prompt, history,
15
+ temperature=0.25,
16
+ max_new_tokens=512,
17
+ top_p=0.95,
18
+ repetition_penalty=1.0,):
19
+
20
+ temperature = float(temperature)
21
+
22
+ if temperature < 1e-2:
23
+ temperature = 1e-2
24
+
25
+ top_p = float(top_p)
26
+
27
+ generate_kwargs = dict(
28
+ temperature=temperature,
29
+ max_new_tokens=max_new_tokens,
30
+ top_p=top_p,
31
+ repetition_penalty=repetition_penalty,
32
+ do_sample=True,
33
+ seed=727,
34
+ )
35
+
36
+ formatted_prompt = format_prompt(prompt, history)
37
+
38
+ stream = client.text_generation(formatted_prompt,
39
+ **generate_kwargs,
40
+ stream=True,
41
+ details=True,
42
+ return_full_text=False)
43
+ output = ""
44
+
45
+ for response in stream:
46
+ output += response.token.text
47
+ yield output
48
+ return output
49
+
50
+ samir_chatbot = gr.Chatbot(avatar_images=["./user.png", "./bot.png"],
51
+ bubble_full_width=False,
52
+ show_label=False,
53
+ show_copy_button=True,
54
+ likeable=True,)
55
+
56
+ demo = gr.ChatInterface(fn=generate,
57
+ chatbot=samir_chatbot,
58
+ title="Samir's Mixtral 8x7b Chat",)
59
+
60
+ demo.queue().launch(show_api=False)