Artples commited on
Commit
1556304
β€’
1 Parent(s): 3c7dd30

Upload 5 files

Browse files
Files changed (5) hide show
  1. NOTICE.txt +1 -0
  2. README (3).md +11 -0
  3. app (2).py +134 -0
  4. requirements (1).txt +9 -0
  5. styles.css +17 -0
NOTICE.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ This application is designed not to rival any OpenAI product but to showcase this Model's capabilities for research purposes.
README (3).md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: L-MChat-7b
3
+ emoji: πŸš€
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: gradio
7
+ app_file: app.py
8
+ pinned: true
9
+ license: apache-2.0
10
+ short_description: A chat demonstration of M-LChat-7b!
11
+ ---
app (2).py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
+
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
+
10
+ MAX_MAX_NEW_TOKENS = 2048
11
+ DEFAULT_MAX_NEW_TOKENS = 1024
12
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
13
+
14
+ DESCRIPTION = """\
15
+ # L-MChat-7b
16
+
17
+ This Space demonstrates [L-MChat-7b](https://huggingface.co/Artples/L-MChat-7b) by L-AI.
18
+
19
+ """
20
+
21
+
22
+ if not torch.cuda.is_available():
23
+ DESCRIPTION += "\n<p>Running on CPU! This demo does not work on CPU.</p>"
24
+
25
+
26
+ if torch.cuda.is_available():
27
+ model_id = "Artples/L-MChat-7b"
28
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
29
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
30
+ tokenizer.use_default_system_prompt = False
31
+
32
+
33
+ @spaces.GPU(enable_queue=True, duration=90)
34
+ def generate(
35
+ message: str,
36
+ chat_history: list[tuple[str, str]],
37
+ system_prompt: str,
38
+ max_new_tokens: int = 1024,
39
+ temperature: float = 0.1,
40
+ top_p: float = 0.9,
41
+ top_k: int = 50,
42
+ repetition_penalty: float = 1.2,
43
+ ) -> Iterator[str]:
44
+ conversation = []
45
+ if system_prompt:
46
+ conversation.append({"role": "system", "content": system_prompt})
47
+ for user, assistant in chat_history:
48
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
49
+ conversation.append({"role": "user", "content": message})
50
+
51
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True)
52
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
53
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
54
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
55
+ input_ids = input_ids.to(model.device)
56
+
57
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
58
+ generate_kwargs = dict(
59
+ {"input_ids": input_ids},
60
+ streamer=streamer,
61
+ max_new_tokens=max_new_tokens,
62
+ do_sample=True,
63
+ top_p=top_p,
64
+ top_k=top_k,
65
+ temperature=temperature,
66
+ num_beams=1,
67
+ repetition_penalty=repetition_penalty,
68
+ )
69
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
70
+ t.start()
71
+
72
+ outputs = []
73
+ for text in streamer:
74
+ outputs.append(text)
75
+ yield "".join(outputs)
76
+
77
+
78
+ chat_interface = gr.ChatInterface(
79
+ theme='ehristoforu/RE_Theme',
80
+ fn=generate,
81
+ additional_inputs=[
82
+ gr.Textbox(label="System prompt", lines=6),
83
+ gr.Slider(
84
+ label="Max new tokens",
85
+ minimum=1,
86
+ maximum=MAX_MAX_NEW_TOKENS,
87
+ step=1,
88
+ value=DEFAULT_MAX_NEW_TOKENS,
89
+ ),
90
+ gr.Slider(
91
+ label="Temperature",
92
+ minimum=0.1,
93
+ maximum=4.0,
94
+ step=0.1,
95
+ value=0.6,
96
+ ),
97
+ gr.Slider(
98
+ label="Top-p (nucleus sampling)",
99
+ minimum=0.05,
100
+ maximum=1.0,
101
+ step=0.05,
102
+ value=0.9,
103
+ ),
104
+ gr.Slider(
105
+ label="Top-k",
106
+ minimum=1,
107
+ maximum=1000,
108
+ step=1,
109
+ value=50,
110
+ ),
111
+ gr.Slider(
112
+ label="Repetition penalty",
113
+ minimum=1.0,
114
+ maximum=2.0,
115
+ step=0.05,
116
+ value=1.2,
117
+ ),
118
+ ],
119
+ stop_btn=None,
120
+ examples=[
121
+ ["Hello there! How are you doing?"],
122
+ ["Can you explain briefly to me what is the Python programming language?"],
123
+ ["Explain the plot of Cinderella in a sentence."],
124
+ ["How many hours does it take a man to eat a Helicopter?"],
125
+ ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
126
+ ],
127
+ )
128
+
129
+ with gr.Blocks(css="style.css") as demo:
130
+ gr.Markdown(DESCRIPTION)
131
+ chat_interface.render()
132
+
133
+ if __name__ == "__main__":
134
+ demo.queue(max_size=20).launch()
requirements (1).txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.27.2
2
+ bitsandbytes==0.42.0
3
+ gradio
4
+ protobuf==3.20.3
5
+ scipy==1.12.0
6
+ sentencepiece==0.1.99
7
+ spaces==0.24.1
8
+ torch==2.0.0
9
+ transformers==4.38.2
styles.css ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ #duplicate-button {
7
+ margin: auto;
8
+ color: white;
9
+ background: #1565c0;
10
+ border-radius: 100vh;
11
+ }
12
+
13
+ .contain {
14
+ max-width: 900px;
15
+ margin: auto;
16
+ padding-top: 1.5rem;
17
+ }