Spaces:
Runtime error
Runtime error
fixed bugs
Browse filesFiles changed (3)
- README.md+6-7
- app.py+53-224
- requirements.txt+5-5
12345678910111213---title: Deepseekmathemoji: 💬colorFrom: yellowcolorTo: purplesdk: gradiosdk_version: 4.36.1app_file: app.pypinned: falselicense: apache-2.0---An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).123456789101112---title: Deepseek Math Majorityemoji: 🏆colorFrom: pinkcolorTo: bluesdk: gradiosdk_version: 4.44.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227import gradio as gr# from huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig, set_seed# from accelerate import infer_auto_device_map as iadmimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfigmodel_name = "deepseek-ai/deepseek-math-7b-instruct"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_iddef evaluate_response(problem):# problem=b'what is angle x if angle y is 60 degree and angle z in 60 degree of a traingle'problem=problem+'\nPlease reason step by step, and put your final answer within \\boxed{}.'messages = [{"role": "user", "content": problem}]input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)# result_output, code_output = process_output(raw_output)return result# def respond(# evaluate_response,# history: list[tuple[str, str]],# system_message,# max_tokens,# temperature,# top_p,# ):# messages = [{"role": "system", "content": system_message}]# for val in history:# if val[0]:# messages.append({"role": "user", "content": val[0]})# if val[1]:# messages.append({"role": "assistant", "content": val[1]})# messages.append({"role": "user", "content": message})# response = ""# for message in client.chat_completion(# messages,# max_tokens=max_tokens,# stream=True,# temperature=temperature,# top_p=top_p,# ):# token = message.choices[0].delta.content# response += token# yield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""# demo = gr.ChatInterface(# evaluate_response,# additional_inputs=[# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),# gr.Slider(# minimum=0.1,# maximum=1.0,# value=0.95,# step=0.05,# label="Top-p (nucleus sampling)",# ),# ],# )demo = gr.Interface(fn=evaluate_response,inputs=[gr.Textbox(label="Question")],outputs=gr.Textbox(label="Answer"),title="Question and Answer Interface",description="Enter a question.")if __name__ == "__main__":demo.launch()# import gradio as gr# # from huggingface_hub import InferenceClient# """# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference# """# # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")# from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig, set_seed# # from accelerate import infer_auto_device_map as iadm# import torch# from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig# from transformers import BitsAndBytesConfig# from tqdm import tqdm# import os# USE_PAST_KEY = True# import gc# torch.backends.cuda.enable_mem_efficient_sdp(False)# from transformers import (# AutoModelForCausalLM,# AutoTokenizer,# AutoConfig,# StoppingCriteria,# set_seed# )# n_repetitions = 1# TOTAL_TOKENS = 2048# MODEL_PATH = "Pra-tham/quant_deepseekmath"# #"/kaggle/input/gemma/transformers/7b-it/1"# # DEEP = True# import torch# from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig# import transformers# tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)# model = AutoModelForCausalLM.from_pretrained(# MODEL_PATH,# device_map="cpu",# torch_dtype="auto",# trust_remote_code=True,# )# pipeline = transformers.pipeline(# "text-generation",# model=model,# tokenizer=tokenizer,# torch_dtype='auto',# device_map='cpu',# )# from transformers import StoppingCriteriaList# class StoppingCriteriaSub(StoppingCriteria):# def __init__(self, stops = [], encounters=1):# super().__init__()# # self.stops = [stop.to("cuda") for stop in stops]# self.stops = stops# def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):# for stop in self.stops:# last_token = input_ids[0][-len(stop):]# if torch.all(torch.eq(stop,last_token)):# return True# return False# stop_words = ["```output", "```python", "```\nOutput" , ")\n```" , "``````output"] #,# stop_words_ids = [tokenizer(stop_word, return_tensors='pt', add_special_tokens=False)['input_ids'].squeeze() for stop_word in stop_words]# stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])# code = """Below is a math problem you are to solve (positive numerical answer):# \"{}\"# To accomplish this, first determine a sympy-based approach for solving the problem by listing each step to take and what functions need to be called in each step. Be clear so even an idiot can follow your instructions, and remember, your final answer should be positive integer, not an algebraic expression!# Write the entire script covering all the steps (use comments and document it well) and print the result. After solving the problem, output the final numerical answer within \\boxed{}.# Approach:"""# cot = """Below is a math problem you are to solve (positive numerical answer!):# \"{}\"# Analyze this problem and think step by step to come to a solution with programs. After solving the problem, output the final numerical answer within \\boxed{}.\n\n"""# promplt_options = [code,cot]# import re# from collections import defaultdict# from collections import Counter# from numpy.random import choice# import numpy as np# tool_instruction = '\n\nPlease integrate natural language reasoning with programs to solve the above problem, and put your final numerical answer within \\boxed{}.\nNote that the intermediary calculations may be real numbers, but the final numercal answer would always be an integer.'# #tool_instruction = " The answer should be given as a non-negative modulo 1000."# #tool_instruction += '\nPlease integrate natural language reasoning with programs to solve the problem above, and put your final answer within \\boxed{}.'# demo = gr.Interface(# fn=predict,# inputs=[gr.Textbox(label="Question")],# outputs=gr.Textbox(label="Answer"),# title="Question and Answer Interface",# description="Enter a question."# )# import subprocess# def reboot_system():# try:# # Execute the reboot command# subprocess.run(['sudo', 'reboot'], check=True)# except subprocess.CalledProcessError as e:# print(f"Error occurred while trying to reboot the system: {e}")# if __name__ == "__main__":# if os.path.exists("temp.txt"):# os.remove("temp.txt")# reboot_system()# demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grimport ctranslate2from transformers import AutoTokenizerfrom huggingface_hub import snapshot_download# Define the model and tokenizer loadingmodel_prompt = "Solve the following mathematical problem: "tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")# Function to generate predictions using the modeldef get_prediction(question):input_text = model_prompt + questioninput_tokens = tokenizer.tokenize(input_text)results = generator.generate_batch([input_tokens])output_tokens = results[0].sequences[0]predicted_answer = tokenizer.convert_tokens_to_string(output_tokens)return predicted_answer# Function to perform majority voting across multiple predictionsdef majority_vote(question, num_iterations=10):all_predictions = []for _ in range(num_iterations):prediction = get_prediction(question)all_predictions.append(prediction)majority_voted_pred = max(set(all_predictions), key=all_predictions.count)return majority_voted_pred, all_predictions# Gradio interface for user input and outputdef gradio_interface(question, correct_answer):final_prediction, all_predictions = majority_vote(question, num_iterations=10)return {"Question": question,"Generated Answers (10 iterations)": all_predictions,"Majority-Voted Prediction": final_prediction,"Correct Answer": correct_answer}# Gradio app setupinterface = gr.Interface(fn=gradio_interface,inputs=[gr.Textbox(label="Math Question"),gr.Textbox(label="Correct Answer"),],outputs=[gr.JSON(label="Results"), # Display the results in a JSON format],title="Math Question Solver",description="Enter a math question to get the model prediction and see all generated answers.",)if __name__ == "__main__":interface.launch()
12345huggingface_hub==0.22.2transformers==4.40.0torch==2.3.1accelerate==0.31.0bitsandbytes==0.43.112345gradioctranslate2transformerstorch
README.md+6-7
app.py+53-224
requirements.txt+5-5