Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
gr.load("models/AIDC-AI/Marco-o1").launch()
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import subprocess
|
4 |
+
from threading import Thread
|
5 |
+
|
6 |
+
import torch
|
7 |
+
import spaces
|
8 |
import gradio as gr
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
|
10 |
+
|
11 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
12 |
+
|
13 |
+
MODEL_ID = "models/AIDC-AI/Marco-o1"
|
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 coding assistance and general AI tasks." # Descripción predeterminada
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
@spaces.GPU()
|
26 |
+
def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
|
27 |
+
# Format history with a given chat template
|
28 |
+
if CHAT_TEMPLATE == "Auto":
|
29 |
+
stop_tokens = [tokenizer.eos_token_id]
|
30 |
+
instruction = system_prompt + "\n\n"
|
31 |
+
for user, assistant in history:
|
32 |
+
instruction += f"User: {user}\nAssistant: {assistant}\n"
|
33 |
+
instruction += f"User: {message}\nAssistant:"
|
34 |
+
elif CHAT_TEMPLATE == "ChatML":
|
35 |
+
stop_tokens = ["<|endoftext|>", "<|im_end|>"]
|
36 |
+
instruction = '<|im_start|>system\n' + system_prompt + '\n<|im_end|>\n'
|
37 |
+
for user, assistant in history:
|
38 |
+
instruction += f'<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n'
|
39 |
+
instruction += f'<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n'
|
40 |
+
elif CHAT_TEMPLATE == "Mistral Instruct":
|
41 |
+
stop_tokens = ["</s>", "[INST]", "[INST] ", "<s>", "[/INST]", "[/INST] "]
|
42 |
+
instruction = f'<s>[INST] {system_prompt}\n'
|
43 |
+
for user, assistant in history:
|
44 |
+
instruction += f'{user} [/INST] {assistant}</s>[INST]'
|
45 |
+
instruction += f' {message} [/INST]'
|
46 |
+
else:
|
47 |
+
raise Exception("Incorrect chat template, select 'Auto', 'ChatML' or 'Mistral Instruct'")
|
48 |
+
print(instruction)
|
49 |
+
|
50 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
51 |
+
enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
|
52 |
+
input_ids, attention_mask = enc.input_ids, enc.attention_mask
|
53 |
+
|
54 |
+
if input_ids.shape[1] > CONTEXT_LENGTH:
|
55 |
+
input_ids = input_ids[:, -CONTEXT_LENGTH:]
|
56 |
+
attention_mask = attention_mask[:, -CONTEXT_LENGTH:]
|
57 |
+
|
58 |
+
generate_kwargs = dict(
|
59 |
+
input_ids=input_ids.to(device),
|
60 |
+
attention_mask=attention_mask.to(device),
|
61 |
+
streamer=streamer,
|
62 |
+
do_sample=True,
|
63 |
+
temperature=temperature,
|
64 |
+
max_new_tokens=max_new_tokens,
|
65 |
+
top_k=top_k,
|
66 |
+
repetition_penalty=repetition_penalty,
|
67 |
+
top_p=top_p
|
68 |
+
)
|
69 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
70 |
+
t.start()
|
71 |
+
outputs = []
|
72 |
+
for new_token in streamer:
|
73 |
+
outputs.append(new_token)
|
74 |
+
if new_token in stop_tokens:
|
75 |
+
break
|
76 |
+
yield "".join(outputs)
|
77 |
+
|
78 |
+
|
79 |
+
# Load model
|
80 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
81 |
+
quantization_config = BitsAndBytesConfig(
|
82 |
+
load_in_4bit=True,
|
83 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
84 |
+
)
|
85 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
86 |
+
model = AutoModelForCausalLM.from_pretrained(
|
87 |
+
MODEL_ID,
|
88 |
+
device_map="auto",
|
89 |
+
quantization_config=quantization_config,
|
90 |
+
attn_implementation="flash_attention_2",
|
91 |
+
)
|
92 |
+
|
93 |
+
# Create Gradio interface
|
94 |
+
gr.ChatInterface(
|
95 |
+
predict,
|
96 |
+
title=EMOJI + " " + MODEL_NAME,
|
97 |
+
description=DESCRIPTION,
|
98 |
+
examples=[
|
99 |
+
["Can you solve the equation 2x + 3 = 11 for x in Python?"],
|
100 |
+
["Write a Java program that checks if a number is even or odd."],
|
101 |
+
["How can I reverse a string in JavaScript?"],
|
102 |
+
["Create a C++ function to find the factorial of a number."],
|
103 |
+
["Write a Python list comprehension to generate a list of squares of numbers from 1 to 10."],
|
104 |
+
["How do I implement a binary search algorithm in C?"],
|
105 |
+
["Write a Ruby script to read a file and count the number of lines in it."],
|
106 |
+
["Create a Swift class to represent a bank account with deposit and withdrawal methods."],
|
107 |
+
["How do I find the maximum element in an array using Kotlin?"],
|
108 |
+
["Write a Rust program to generate the Fibonacci sequence up to the 10th number."]
|
109 |
+
],
|
110 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
|
111 |
+
additional_inputs=[
|
112 |
+
gr.Textbox("You are a code assistant.", label="System prompt"),
|
113 |
+
gr.Slider(0, 1, 0.3, label="Temperature"),
|
114 |
+
gr.Slider(128, 4096, 1024, label="Max new tokens"),
|
115 |
+
gr.Slider(1, 80, 40, label="Top K sampling"),
|
116 |
+
gr.Slider(0, 2, 1.1, label="Repetition penalty"),
|
117 |
+
gr.Slider(0, 1, 0.95, label="Top P sampling"),
|
118 |
+
],
|
119 |
+
theme=gr.themes.Soft(primary_hue=COLOR),
|
120 |
+
).queue().launch()
|
121 |
|
|