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:
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()