File size: 5,143 Bytes
550f132
ad41ac1
 
 
 
 
0c6cf3e
 
ad41ac1
 
 
0c6cf3e
 
 
 
 
ad41ac1
0c6cf3e
 
 
 
 
ad41ac1
55ca4f6
0c6cf3e
 
 
 
 
ad41ac1
0c6cf3e
 
 
 
 
 
 
 
 
ad41ac1
0c6cf3e
 
ad41ac1
0c6cf3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad41ac1
0c6cf3e
 
 
 
34fbe71
 
0c6cf3e
 
 
 
 
ad41ac1
0c6cf3e
34fbe71
ad41ac1
0c6cf3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import spaces
import torch
import sys
import html
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
from threading import Thread
import gradio as gr
from gradio_rich_textbox import RichTextbox



title = """# 🙋🏻‍♂️Welcome to🌟Tonic's🫡📉MetaMath
this is Tencent's mistral DPO finetune for mathematics. You can build with this endpoint using🫡📉MetaMath available here : [TencentARC/Mistral_Pro_8B_v0.1](https://huggingface.co/TencentARC/Mistral_Pro_8B_v0.1). We're using 🤖[introspector/unimath](https://huggingface.co/datasets/introspector/unimath) for cool examples, check it out below ! The demo is still a work in progress and we're looking forward to build downstream tasks that showcase outstanding mathematical reasoning. Have any ideas ? join us below !
You can also use 🫡📉MetaMath by cloning this space. Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/MetaMath?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3> 
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) Math with [introspector](https://huggingface.co/introspector) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [SciTonic](https://github.com/Tonic-AI/scitonic)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
"""

model_name = 'TencentARC/Mistral_Pro_8B_v0.1'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id

@torch.inference_mode()
@spaces.GPU
def predict_math_bot(user_message, system_message, max_new_tokens, temperature, top_p, repetition_penalty):
    prompt = f"<|system|>\n{self.system_message}\n<|user|>\n{user_message}<|assistant|>" if system_message else user_message
    inputs = tokenizer(prompt, return_tensors='pt', add_special_tokens=False)
    input_ids = inputs["input_ids"].to(model.device)

    output_ids = model.generate(
        input_ids,
        max_length=input_ids.shape[1] + max_new_tokens,
        temperature=temperature,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        pad_token_id=tokenizer.eos_token_id,
        do_sample=True
    )

    response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
    return response

def main():
    with gr.Blocks() as demo:
        gr.Markdown(title)
        output_text = gr.RichTextbox(label="🫡📉MetaMath", showlabel=True, interactive=True)
        
        with gr.Accordion("Advanced Settings"):
            with gr.Row():
                max_new_tokens = gr.Slider(label="Max new tokens", value=125, minimum=25, maximum=1250)
                temperature = gr.Slider(label="Temperature", value=0.1, minimum=0.05, maximum=1.0)
                top_p = gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.01, maximum=0.99)
                repetition_penalty = gr.Slider(label="Repetition penalty", value=1.9, minimum=1.0, maximum=2.0)
                use_custom_settings = gr.Checkbox(label="Use custom settings", value=False)
        
        with gr.Row():
            user_message = gr.Textbox(label="🫡Your Message", lines=3, placeholder="Enter your math query here...")
            system_message = gr.Textbox(label="📉System Prompt", lines=2, placeholder="Optional: Set a scene or introduce a character...")

        gr.Button("Generate").click(
            predict_math_bot,
            inputs=[user_message, system_message, max_new_tokens, temperature, top_p, repetition_penalty, use_custom_settings],
            outputs=output_text
        )

        # Example placeholders
        demo.add_examples([
            ["Calculate the integral of x^2 from 0 to 1", "produce a complete mathematical explanation of the following in rich text format:", 125, 0.1, 0.9, 1.9, False],
            ["What is the derivative of sin(x)?", "produce a complete mathematical explanation of the following in rich text format:", 125, 0.1, 0.9, 1.9, False]
        ])

    demo.launch()

if __name__ == "__main__":
    main()