nikravan commited on
Commit
3b4a08d
·
verified ·
1 Parent(s): dad2653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -23
app.py CHANGED
@@ -10,19 +10,50 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
10
 
11
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
12
 
13
- MODEL_ID = "nikravan/Marco_o1_q4"
14
-
15
  CHAT_TEMPLATE = "ChatML"
16
  MODEL_NAME = MODEL_ID.split("/")[-1]
17
  CONTEXT_LENGTH = 16000
18
 
19
- COLOR = "blue" # تغییر رنگ به مشکی
20
- EMOJI = "🤖"
21
- DESCRIPTION = f"This is the {MODEL_NAME} model designed for testing thinking for general AI tasks."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
  @spaces.GPU()
25
  def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
 
26
  if CHAT_TEMPLATE == "Auto":
27
  stop_tokens = [tokenizer.eos_token_id]
28
  instruction = system_prompt + "\n\n"
@@ -43,6 +74,7 @@ def predict(message, history, system_prompt, temperature, max_new_tokens, top_k,
43
  instruction += f' {message} [/INST]'
44
  else:
45
  raise Exception("Incorrect chat template, select 'Auto', 'ChatML' or 'Mistral Instruct'")
 
46
 
47
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
48
  enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
@@ -70,11 +102,10 @@ def predict(message, history, system_prompt, temperature, max_new_tokens, top_k,
70
  outputs.append(new_token)
71
  if new_token in stop_tokens:
72
  break
73
- result = "".join(outputs)
74
- # تغییر قالب به Markdown و LaTeX
75
- yield f"### $$ {result} $$"
76
 
77
 
 
78
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
79
  quantization_config = BitsAndBytesConfig(
80
  load_in_4bit=True,
@@ -88,19 +119,27 @@ model = AutoModelForCausalLM.from_pretrained(
88
  attn_implementation="flash_attention_2",
89
  )
90
 
91
- gr.ChatInterface(
92
- predict,
93
- title=EMOJI + " " + MODEL_NAME,
94
- description=DESCRIPTION,
95
- additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
96
- additional_inputs=[
97
- gr.Textbox("You are a code assistant.", label="System prompt"),
98
- gr.Slider(0, 1, 0.3, label="Temperature"),
99
- gr.Slider(128, 4096, 1024, label="Max new tokens"),
100
- gr.Slider(1, 80, 40, label="Top K sampling"),
101
- gr.Slider(0, 2, 1.1, label="Repetition penalty"),
102
- gr.Slider(0, 1, 0.95, label="Top P sampling"),
103
- ],
104
- theme=gr.themes.Soft(primary_hue=COLOR),
105
- ).queue().launch()
 
 
 
 
 
 
 
106
 
 
 
10
 
11
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
12
 
13
+ MODEL_ID = "nikravan/Marco-O1-q4"
 
14
  CHAT_TEMPLATE = "ChatML"
15
  MODEL_NAME = MODEL_ID.split("/")[-1]
16
  CONTEXT_LENGTH = 16000
17
 
18
+ # Estableciendo valores directamente para las variables
19
+ COLOR = "blue" # Color predeterminado de la interfaz
20
+ EMOJI = "🤖" # Emoji predeterminado para el modelo
21
+ DESCRIPTION = f"This is the {MODEL_NAME} model designed for testing thinking for general AI tasks." # Descripción predeterminada
22
+
23
+ latex_delimiters_set = [{
24
+ "left": "\\(",
25
+ "right": "\\)",
26
+ "display": False
27
+ }, {
28
+ "left": "\\begin{equation}",
29
+ "right": "\\end{equation}",
30
+ "display": True
31
+ }, {
32
+ "left": "\\begin{align}",
33
+ "right": "\\end{align}",
34
+ "display": True
35
+ }, {
36
+ "left": "\\begin{alignat}",
37
+ "right": "\\end{alignat}",
38
+ "display": True
39
+ }, {
40
+ "left": "\\begin{gather}",
41
+ "right": "\\end{gather}",
42
+ "display": True
43
+ }, {
44
+ "left": "\\begin{CD}",
45
+ "right": "\\end{CD}",
46
+ "display": True
47
+ }, {
48
+ "left": "\\[",
49
+ "right": "\\]",
50
+ "display": True
51
+ }]
52
 
53
 
54
  @spaces.GPU()
55
  def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
56
+ # Format history with a given chat template
57
  if CHAT_TEMPLATE == "Auto":
58
  stop_tokens = [tokenizer.eos_token_id]
59
  instruction = system_prompt + "\n\n"
 
74
  instruction += f' {message} [/INST]'
75
  else:
76
  raise Exception("Incorrect chat template, select 'Auto', 'ChatML' or 'Mistral Instruct'")
77
+ print(instruction)
78
 
79
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
80
  enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
 
102
  outputs.append(new_token)
103
  if new_token in stop_tokens:
104
  break
105
+ yield "".join(outputs)
 
 
106
 
107
 
108
+ # Load model
109
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
110
  quantization_config = BitsAndBytesConfig(
111
  load_in_4bit=True,
 
119
  attn_implementation="flash_attention_2",
120
  )
121
 
122
+ # Create Gradio interface
123
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue=COLOR)) as demo:
124
+ chatbot = gr.Chatbot(label=EMOJI + " " + MODEL_NAME, latex=True)
125
+ system_prompt = gr.Textbox("You are a code assistant.", label="System prompt")
126
+ temperature = gr.Slider(0, 1, 0.3, label="Temperature")
127
+ max_new_tokens = gr.Slider(128, 4096, 1024, label="Max new tokens")
128
+ top_k = gr.Slider(1, 80, 40, label="Top K sampling")
129
+ repetition_penalty = gr.Slider(0, 2, 1.1, label="Repetition penalty")
130
+ top_p = gr.Slider(0, 1, 0.95, label="Top P sampling")
131
+ message = gr.Textbox(label="User Input")
132
+ submit = gr.Button("Submit")
133
+
134
+ def respond(message, chatbot_history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
135
+ response = predict(message, chatbot_history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p)
136
+ chatbot_history.append((message, "".join(response)))
137
+ return chatbot_history
138
+
139
+ submit.click(
140
+ respond,
141
+ inputs=[message, chatbot, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p],
142
+ outputs=chatbot
143
+ )
144
 
145
+ demo.launch()