Makima57 commited on
Commit
8da95b6
·
verified ·
1 Parent(s): 8a529be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -11
app.py CHANGED
@@ -2,14 +2,14 @@ import gradio as gr
2
  import ctranslate2
3
  from transformers import AutoTokenizer
4
  from huggingface_hub import snapshot_download
5
- from codeexecutor import postprocess_completion,get_majority_vote
6
 
7
  # Define the model and tokenizer loading
8
  model_prompt = "Solve the following mathematical problem: "
9
  tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
10
  model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
11
  generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
12
- iterations=10
13
 
14
  # Function to generate predictions using the model
15
  def get_prediction(question):
@@ -23,19 +23,19 @@ def get_prediction(question):
23
  # Function to perform majority voting across multiple predictions
24
  def majority_vote(question, num_iterations=10):
25
  all_predictions = []
26
- all_answer=[]
27
  for _ in range(num_iterations):
28
  prediction = get_prediction(question)
29
- answer=postprocess_completion(prediction,True,True)
30
  all_predictions.append(prediction)
31
  all_answer.append(answer)
32
  majority_voted_pred = max(set(all_predictions), key=all_predictions.count)
33
- majority_voted_ans=get_majority_vote(all_answer)
34
- return majority_voted_pred, all_predictions,majority_voted_ans
35
 
36
  # Gradio interface for user input and output
37
  def gradio_interface(question, correct_answer):
38
- final_prediction, all_predictions,final_answer = majority_vote(question, iterations)
39
  return {
40
  "Question": question,
41
  "Generated Answers (10 iterations)": all_predictions,
@@ -44,19 +44,64 @@ def gradio_interface(question, correct_answer):
44
  "Majority answer": final_answer
45
  }
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # Gradio app setup
48
  interface = gr.Interface(
49
  fn=gradio_interface,
50
  inputs=[
51
- gr.Textbox(label="Math Question"),
52
- gr.Textbox(label="Correct Answer"),
53
  ],
54
  outputs=[
55
- gr.JSON(label="Results"), # Display the results in a JSON format
56
  ],
57
  title="Math Question Solver",
58
  description="Enter a math question to get the model prediction and see all generated answers.",
 
59
  )
60
 
61
  if __name__ == "__main__":
62
- interface.launch()
 
2
  import ctranslate2
3
  from transformers import AutoTokenizer
4
  from huggingface_hub import snapshot_download
5
+ from codeexecutor import postprocess_completion, get_majority_vote
6
 
7
  # Define the model and tokenizer loading
8
  model_prompt = "Solve the following mathematical problem: "
9
  tokenizer = AutoTokenizer.from_pretrained("AI-MO/NuminaMath-7B-TIR")
10
  model_path = snapshot_download(repo_id="Makima57/deepseek-math-Numina")
11
  generator = ctranslate2.Generator(model_path, device="cpu", compute_type="int8")
12
+ iterations = 10
13
 
14
  # Function to generate predictions using the model
15
  def get_prediction(question):
 
23
  # Function to perform majority voting across multiple predictions
24
  def majority_vote(question, num_iterations=10):
25
  all_predictions = []
26
+ all_answer = []
27
  for _ in range(num_iterations):
28
  prediction = get_prediction(question)
29
+ answer = postprocess_completion(prediction, True, True)
30
  all_predictions.append(prediction)
31
  all_answer.append(answer)
32
  majority_voted_pred = max(set(all_predictions), key=all_predictions.count)
33
+ majority_voted_ans = get_majority_vote(all_answer)
34
+ return majority_voted_pred, all_predictions, majority_voted_ans
35
 
36
  # Gradio interface for user input and output
37
  def gradio_interface(question, correct_answer):
38
+ final_prediction, all_predictions, final_answer = majority_vote(question, iterations)
39
  return {
40
  "Question": question,
41
  "Generated Answers (10 iterations)": all_predictions,
 
44
  "Majority answer": final_answer
45
  }
46
 
47
+ # Custom CSS for styling
48
+ custom_css = """
49
+ body {
50
+ background-color: #f4f7fb;
51
+ font-family: 'Roboto', sans-serif;
52
+ }
53
+ .gradio-container {
54
+ border-radius: 15px;
55
+ border: 2px solid #e0e4e7;
56
+ padding: 20px;
57
+ box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
58
+ background-color: white;
59
+ max-width: 700px;
60
+ margin: 0 auto;
61
+ }
62
+ h1, h2, p {
63
+ text-align: center;
64
+ color: #333;
65
+ }
66
+ input, textarea {
67
+ border-radius: 8px;
68
+ border: 1px solid #ccc;
69
+ padding: 10px;
70
+ font-size: 16px;
71
+ }
72
+ .gr-button {
73
+ background-color: #4caf50;
74
+ color: white;
75
+ border-radius: 8px;
76
+ padding: 10px 20px;
77
+ font-size: 16px;
78
+ transition: background-color 0.3s;
79
+ }
80
+ .gr-button:hover {
81
+ background-color: #45a049;
82
+ }
83
+ .gr-output {
84
+ background-color: #f1f1f1;
85
+ border-radius: 8px;
86
+ padding: 15px;
87
+ font-size: 14px;
88
+ }
89
+ """
90
+
91
  # Gradio app setup
92
  interface = gr.Interface(
93
  fn=gradio_interface,
94
  inputs=[
95
+ gr.Textbox(label="Math Question", placeholder="Enter your math question here...", elem_id="math_question"),
96
+ gr.Textbox(label="Correct Answer", placeholder="Enter the correct answer here...", elem_id="correct_answer"),
97
  ],
98
  outputs=[
99
+ gr.JSON(label="Results", elem_id="results"), # Display the results in a JSON format
100
  ],
101
  title="Math Question Solver",
102
  description="Enter a math question to get the model prediction and see all generated answers.",
103
+ css=custom_css # Apply custom CSS
104
  )
105
 
106
  if __name__ == "__main__":
107
+ interface.launch()