basakerdogan commited on
Commit
4787649
1 Parent(s): 516ea3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -13
app.py CHANGED
@@ -1,19 +1,245 @@
 
 
1
  import gradio as gr
2
- import os
3
 
4
- # Tema dosyası değiştirildi
5
- with gr.Blocks(theme='light') as demo:
6
 
7
- model_id = 'basakerdogan/Cyber-Jarvis-4Bit'
8
- API_URL = "https://api-inference.huggingface.co/models/" + model_id
9
- HF_TOKEN = os.environ.get('HF_READ_TOKEN', False)
10
 
11
- client = Client(
12
- API_URL,
13
- headers={'Authorization': f"Bearer {HF_TOKEN}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  )
15
- EOS_STRING = "</s>"
16
- EOT_STRING = "<EOT>"
17
 
18
- def get_prompt(message, chat_history, system_prompt):
19
- texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n
 
1
+ import os
2
+ from typing import Iterator
3
  import gradio as gr
4
+ from text_generation import Client
5
 
6
+ model_id = 'mistralai/Mistral-7B-Instruct-v0.1'
 
7
 
8
+ API_URL = "https://api-inference.huggingface.co/models/" + model_id
9
+ HF_TOKEN = os.environ.get('HF_READ_TOKEN', False)
 
10
 
11
+ client = Client(
12
+ API_URL,
13
+ headers={'Authorization': f"Bearer {HF_TOKEN}"}
14
+ )
15
+ EOS_STRING = "</s>"
16
+ EOT_STRING = "<EOT>"
17
+
18
+ def get_prompt(message, chat_history, system_prompt):
19
+ texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
20
+
21
+ do_strip = False
22
+ for user_input, response in chat_history:
23
+ user_input = user_input.strip() if do_strip else user_input
24
+ do_strip = True
25
+ texts.append(f"{user_input} [/INST] {response.strip()} </s><s>[INST] ")
26
+ message = message.strip() if do_strip else message
27
+ texts.append(f"{message} [/INST]")
28
+ return ''.join(texts)
29
+
30
+ def run(message, chat_history, system_prompt, max_new_tokens=1024, temperature=0.1, top_p=0.9, top_k=50):
31
+ prompt = get_prompt(message, chat_history, system_prompt)
32
+
33
+ generate_kwargs = dict(
34
+ max_new_tokens=max_new_tokens,
35
+ do_sample=True,
36
+ top_p=top_p,
37
+ top_k=top_k,
38
+ temperature=temperature
39
+ )
40
+ stream = client.generate_stream(prompt, **generate_kwargs)
41
+ output = ''
42
+ for response in stream:
43
+ if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
44
+ return output
45
+ else:
46
+ output += response.token.text
47
+ yield output
48
+ return output
49
+
50
+ DEFAULT_SYSTEM_PROMPT = """
51
+ You are Ricky. You are an AI assistant, you are moderately-polite and give only true information.
52
+ You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning.
53
+ If you think there might not be a correct answer, you say so. Since you are autoregressive,
54
+ each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context,
55
+ assumptions, and step-by-step thinking BEFORE you try to answer a question. You are an AI developed by MCES10 Software the website is www.mces10-software.com.
56
+ The CEO is MCES10. You are based on the Mistral-7B-Instruct-v0.1. You ask what the person's name is if they say hello. MCES10 Software has made apps named To-List a brilliant to-do list app. Web Development Tutorials also known as W.D.T which teaches you how to code websites.
57
+ There are AI Hub which is in development which is the gateway to everything AI.
58
+ """
59
+ MAX_MAX_NEW_TOKENS = 4096
60
+ DEFAULT_MAX_NEW_TOKENS = 256
61
+ MAX_INPUT_TOKEN_LENGTH = 4000
62
+
63
+ DESCRIPTION = "Ricky AI"
64
+
65
+ def clear_and_save_textbox(message): return '', message
66
+
67
+ def display_input(message, history=[]):
68
+ history.append((message, ''))
69
+ return history
70
+
71
+ def delete_prev_fn(history=[]):
72
+ try:
73
+ message, _ = history.pop()
74
+ except IndexError:
75
+ message = ''
76
+ return history, message or ''
77
+
78
+ def generate(message, history_with_input, system_prompt, max_new_tokens, temperature, top_p, top_k):
79
+ if max_new_tokens > MAX_MAX_NEW_TOKENS:
80
+ raise ValueError
81
+
82
+ history = history_with_input[:-1]
83
+ generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
84
+ try:
85
+ first_response = next(generator)
86
+ yield history + [(message, first_response)]
87
+ except StopIteration:
88
+ yield history + [(message, '')]
89
+ for response in generator:
90
+ yield history + [(message, response)]
91
+
92
+ def process_example(message):
93
+ generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 1, 0.95, 50)
94
+ for x in generator:
95
+ pass
96
+ return '', x
97
+
98
+ def check_input_token_length(message, chat_history, system_prompt):
99
+ input_token_length = len(message) + len(chat_history)
100
+ if input_token_length > MAX_INPUT_TOKEN_LENGTH:
101
+ raise gr.Error(f"The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.")
102
+
103
+ with gr.Blocks(theme='Taithrah/Minimal') as demo:
104
+ gr.Markdown(DESCRIPTION)
105
+
106
+
107
+
108
+ with gr.Group():
109
+ chatbot = gr.Chatbot(label='RickyAI based on Mistral-7B-Instruct-v0.1')
110
+ with gr.Row():
111
+ textbox = gr.Textbox(
112
+ container=False,
113
+ show_label=False,
114
+ placeholder='Hi, Ricky',
115
+ scale=10
116
+ )
117
+ submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0)
118
+
119
+ with gr.Row():
120
+ retry_button = gr.Button('Retry', variant='secondary')
121
+ undo_button = gr.Button('Undo', variant='secondary')
122
+ clear_button = gr.Button('Clear', variant='secondary')
123
+
124
+ saved_input = gr.State()
125
+
126
+ with gr.Accordion(label='Advanced options', open=False):
127
+ system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=5, interactive=False)
128
+ max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
129
+ temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=0.1)
130
+ top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
131
+ top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=10)
132
+
133
+ textbox.submit(
134
+ fn=clear_and_save_textbox,
135
+ inputs=textbox,
136
+ outputs=[textbox, saved_input],
137
+ api_name=False,
138
+ queue=False,
139
+ ).then(
140
+ fn=display_input,
141
+ inputs=[saved_input, chatbot],
142
+ outputs=chatbot,
143
+ api_name=False,
144
+ queue=False,
145
+ ).then(
146
+ fn=check_input_token_length,
147
+ inputs=[saved_input, chatbot, system_prompt],
148
+ api_name=False,
149
+ queue=False,
150
+ ).success(
151
+ fn=generate,
152
+ inputs=[
153
+ saved_input,
154
+ chatbot,
155
+ system_prompt,
156
+ max_new_tokens,
157
+ temperature,
158
+ top_p,
159
+ top_k,
160
+ ],
161
+ outputs=chatbot,
162
+ api_name=False,
163
+ )
164
+
165
+ button_event_preprocess = submit_button.click(
166
+ fn=clear_and_save_textbox,
167
+ inputs=textbox,
168
+ outputs=[textbox, saved_input],
169
+ api_name=False,
170
+ queue=False,
171
+ ).then(
172
+ fn=display_input,
173
+ inputs=[saved_input, chatbot],
174
+ outputs=chatbot,
175
+ api_name=False,
176
+ queue=False,
177
+ ).then(
178
+ fn=check_input_token_length,
179
+ inputs=[saved_input, chatbot, system_prompt],
180
+ api_name=False,
181
+ queue=False,
182
+ ).success(
183
+ fn=generate,
184
+ inputs=[
185
+ saved_input,
186
+ chatbot,
187
+ system_prompt,
188
+ max_new_tokens,
189
+ temperature,
190
+ top_p,
191
+ top_k,
192
+ ],
193
+ outputs=chatbot,
194
+ api_name=False,
195
+ )
196
+
197
+ retry_button.click(
198
+ fn=delete_prev_fn,
199
+ inputs=chatbot,
200
+ outputs=[chatbot, saved_input],
201
+ api_name=False,
202
+ queue=False,
203
+ ).then(
204
+ fn=display_input,
205
+ inputs=[saved_input, chatbot],
206
+ outputs=chatbot,
207
+ api_name=False,
208
+ queue=False,
209
+ ).then(
210
+ fn=generate,
211
+ inputs=[
212
+ saved_input,
213
+ chatbot,
214
+ system_prompt,
215
+ max_new_tokens,
216
+ temperature,
217
+ top_p,
218
+ top_k,
219
+ ],
220
+ outputs=chatbot,
221
+ api_name=False,
222
+ )
223
+
224
+ undo_button.click(
225
+ fn=delete_prev_fn,
226
+ inputs=chatbot,
227
+ outputs=[chatbot, saved_input],
228
+ api_name=False,
229
+ queue=False,
230
+ ).then(
231
+ fn=lambda x: x,
232
+ inputs=[saved_input],
233
+ outputs=textbox,
234
+ api_name=False,
235
+ queue=False,
236
+ )
237
+
238
+ clear_button.click(
239
+ fn=lambda: ([], ''),
240
+ outputs=[chatbot, saved_input],
241
+ queue=False,
242
+ api_name=False,
243
  )
 
 
244
 
245
+ demo.queue(max_size=32).launch(show_api=False)