hugging2021 commited on
Commit
e7aaa80
1 Parent(s): 0bf3de3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained("DanielAWrightGabrielAI/mpt-7b-storywriter-4bit-128g-65kTokens-CPU", trust_remote_code=True)
7
+ model = AutoModelForCausalLM.from_pretrained("DanielAWrightGabrielAI/mpt-7b-storywriter-4bit-128g-65kTokens-CPU", trust_remote_code=True)
8
+
9
+ def respond(
10
+ message,
11
+ history: list[tuple[str, str]],
12
+ system_message,
13
+ max_tokens,
14
+ temperature,
15
+ top_p,
16
+ ):
17
+ messages = [{"role": "system", "content": system_message}]
18
+
19
+ for val in history:
20
+ if val[0]:
21
+ messages.append({"role": "user", "content": val[0]})
22
+ if val[1]:
23
+ messages.append({"role": "assistant", "content": val[1]})
24
+
25
+ messages.append({"role": "user", "content": message})
26
+
27
+ response = ""
28
+
29
+ for message in client.chat_completion(
30
+ messages,
31
+ max_tokens=max_tokens,
32
+ stream=True,
33
+ temperature=temperature,
34
+ top_p=top_p,
35
+ ):
36
+ token = message.choices[0].delta.content
37
+
38
+ response += token
39
+ yield response
40
+
41
+ demo = gr.ChatInterface(
42
+ respond,
43
+ additional_inputs=[
44
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
45
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
46
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
47
+ gr.Slider(
48
+ minimum=0.1,
49
+ maximum=1.0,
50
+ value=0.95,
51
+ step=0.05,
52
+ label="Top-p (nucleus sampling)",
53
+ ),
54
+ ],
55
+ )
56
+
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()