huzaifa113 commited on
Commit
60836c7
·
verified ·
1 Parent(s): aa927ee

initial commit

Browse files
Files changed (3) hide show
  1. app.py +100 -0
  2. math_meme_training.txt +80 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.py.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/16d-Y-lJ3S-vY04H28dnBtHqjBTz3_lQh
8
+ """
9
+
10
+ import gradio as gr
11
+ from transformers import AutoTokenizer, AutoModelForCausalLM
12
+ import torch
13
+ import os
14
+ import pandas as pd
15
+
16
+
17
+ # Load DeepSeek-Math 7B Instruct model and tokenizer
18
+ model_name = "deepseek-ai/deepseek-math-7b-instruct"
19
+ try:
20
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
21
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
22
+ model.generation_config.pad_token_id = tokenizer.eos_token_id
23
+ print("DeepSeek-Math-7B-Instruct loaded successfully!")
24
+ except Exception as e:
25
+ print(f"Error loading model: {e}")
26
+ model = None
27
+ tokenizer = None
28
+
29
+ # Load training data from math_meme_training.txt
30
+ training_file = "math_meme_training.txt"
31
+ few_shot_prompt = "Below are examples of correcting incorrect math memes with step-by-step reasoning:\n\n"
32
+ if os.path.exists(training_file):
33
+ with open(training_file, 'r') as f:
34
+ lines = f.readlines()
35
+ # Take up to 3 examples to keep prompt manageable
36
+ examples = []
37
+ for i in range(0, min(6, len(lines)), 2): # Step by 2 for Prompt/Response pairs
38
+ if lines[i].startswith("Prompt: ") and i+1 < len(lines) and lines[i+1].startswith("Response: "):
39
+ prompt = lines[i].replace("Prompt: ", "").strip()
40
+ response = lines[i+1].replace("Response: ", "").strip()
41
+ examples.append(f"{prompt}\nResponse: {response}")
42
+ few_shot_prompt += "\n".join(examples[:3]) + "\n\nNow, correct the following incorrect math meme:\n"
43
+ else:
44
+ few_shot_prompt += "No training data found. Using default behavior.\n\nNow, correct the following incorrect math meme:\n"
45
+ print("Warning: math_meme_training.txt not found. Using minimal prompt.")
46
+
47
+ # Function to generate correction
48
+ def generate_deepseek_correction(user_input):
49
+ if model is None or tokenizer is None:
50
+ return "Error: Model not loaded. Please check the Space logs and ensure a GPU is enabled."
51
+
52
+ # Validate input format
53
+ if '=' not in user_input or len(user_input.split('=')) != 2:
54
+ return "Invalid input. Please enter in the format 'expression = wrong_answer' (e.g., '8 ÷ 2(2+2) = 1')."
55
+
56
+ # Construct prompt with training data
57
+ meme_prompt = f"Correct this incorrect math meme: '{user_input}'\nPlease reason step by step and provide the correct answer with an explanation."
58
+ full_prompt = few_shot_prompt + meme_prompt
59
+
60
+ try:
61
+ # Generate response
62
+ inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
63
+ outputs = model.generate(
64
+ **inputs,
65
+ max_new_tokens=150,
66
+ temperature=0.7,
67
+ do_sample=True,
68
+ eos_token_id=tokenizer.eos_token_id
69
+ )
70
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+ correction = response[len(full_prompt):].strip()
72
+
73
+ if not correction:
74
+ correction = "DeepSeek-Math failed to generate a meaningful correction."
75
+
76
+ # Add funny error rating
77
+ error_rating = "Funny Error Rating: '90% fixing memes, 10% wondering why fractions are so hard'"
78
+ return f"{correction}\n\n{error_rating}"
79
+ except Exception as e:
80
+ return f"Error processing input: {str(e)}"
81
+
82
+ # Gradio interface
83
+ interface = gr.Interface(
84
+ fn=generate_deepseek_correction,
85
+ inputs=gr.Textbox(
86
+ label="Enter an Incorrect Math Meme",
87
+ placeholder="e.g., '7 + 2 × 3 = 27' or '1/2 + 1/2 = 0.25'"
88
+ ),
89
+ outputs=gr.Textbox(label="Corrected Answer and Explanation"),
90
+ title="Math Meme Repair with DeepSeek-Math",
91
+ description="Input an incorrect math meme (e.g., '8 ÷ 2(2+2) = 1') and get the correct answer with an explanation!",
92
+ examples=[
93
+ ["7 + 2 × 3 = 27"],
94
+ ["4 ÷ 2(1+1) = 1"],
95
+ ["1/2 + 1/2 = 0.25"]
96
+ ]
97
+ )
98
+
99
+ # Launch the interface
100
+ interface.launch(server_name="0.0.0.0", server_port=7860)
math_meme_training.txt ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Prompt: Correct this incorrect math meme: '6 ÷ 2(1+2) = 1.0'
2
+ Please reason step by step and provide the correct answer with an explanation.
3
+ Response: Correct answer: 9.0. Explanation: PEMDAS: (1+2) = 3, then 6 ÷ 2 × 3 = 3 × 3 = 9, left-to-right.
4
+
5
+ Prompt: Correct this incorrect math meme: '8 ÷ 2(2+2) = 1.0'
6
+ Please reason step by step and provide the correct answer with an explanation.
7
+ Response: Correct answer: 16.0. Explanation: PEMDAS: (2+2) = 4, then 8 ÷ 2 × 4 = 4 × 4 = 16.
8
+
9
+ Prompt: Correct this incorrect math meme: '5 + 3 × 2 = 16.0'
10
+ Please reason step by step and provide the correct answer with an explanation.
11
+ Response: Correct answer: 11.0. Explanation: PEMDAS: 3 × 2 = 6, then 5 + 6 = 11. Multiply first.
12
+
13
+ Prompt: Correct this incorrect math meme: '2³ = 6.0'
14
+ Please reason step by step and provide the correct answer with an explanation.
15
+ Response: Correct answer: 8.0. Explanation: Exponents mean 2 × 2 × 2 = 8, not 2 × 3.
16
+
17
+ Prompt: Correct this incorrect math meme: '1/2 + 1/2 = 0.25'
18
+ Please reason step by step and provide the correct answer with an explanation.
19
+ Response: Correct answer: 1.0. Explanation: Add numerators: 1+1 = 2, keep denominator 2, so 2/2 = 1.
20
+
21
+ Prompt: Correct this incorrect math meme: '9 - 3 ÷ 1/3 = 0.0'
22
+ Please reason step by step and provide the correct answer with an explanation.
23
+ Response: Correct answer: 0.0. Explanation: 3 ÷ 1/3 = 9, then 9 - 9 = 0. Division by fraction flips it.
24
+
25
+ Prompt: Correct this incorrect math meme: '4 + 2² = 36.0'
26
+ Please reason step by step and provide the correct answer with an explanation.
27
+ Response: Correct answer: 8.0. Explanation: PEMDAS: 2² = 4, then 4 + 4 = 8. Exponents first.
28
+
29
+ Prompt: Correct this incorrect math meme: '10 ÷ 5 × 2 = 1.0'
30
+ Please reason step by step and provide the correct answer with an explanation.
31
+ Response: Correct answer: 4.0. Explanation: PEMDAS: 10 ÷ 5 = 2, then 2 × 2 = 4, left-to-right.
32
+
33
+ Prompt: Correct this incorrect math meme: '3 + 4 × 5 = 35.0'
34
+ Please reason step by step and provide the correct answer with an explanation.
35
+ Response: Correct answer: 23.0. Explanation: PEMDAS: 4 × 5 = 20, then 3 + 20 = 23.
36
+
37
+ Prompt: Correct this incorrect math meme: '5² = 10.0'
38
+ Please reason step by step and provide the correct answer with an explanation.
39
+ Response: Correct answer: 25.0. Explanation: 5² = 5 × 5 = 25, not 5 × 2.
40
+
41
+ Prompt: Correct this incorrect math meme: '6 ÷ 3 + 1 = 3.0'
42
+ Please reason step by step and provide the correct answer with an explanation.
43
+ Response: Correct answer: 3.0. Explanation: 6 ÷ 3 = 2, then 2 + 1 = 3. Division first.
44
+
45
+ Prompt: Correct this incorrect math meme: '2 + 2 × 2 = 8.0'
46
+ Please reason step by step and provide the correct answer with an explanation.
47
+ Response: Correct answer: 6.0. Explanation: PEMDAS: 2 × 2 = 4, then 2 + 4 = 6.
48
+
49
+ Prompt: Correct this incorrect math meme: '1/3 + 1/3 = 0.333'
50
+ Please reason step by step and provide the correct answer with an explanation.
51
+ Response: Correct answer: 0.667. Explanation: 1/3 + 1/3 = 2/3 ≈ 0.667, not 2/6 without simplifying.
52
+
53
+ Prompt: Correct this incorrect math meme: '7 - 2 × 3 = 15.0'
54
+ Please reason step by step and provide the correct answer with an explanation.
55
+ Response: Correct answer: 1.0. Explanation: PEMDAS: 2 × 3 = 6, then 7 - 6 = 1.
56
+
57
+ Prompt: Correct this incorrect math meme: '4 ÷ 2 + 2 = 3.0'
58
+ Please reason step by step and provide the correct answer with an explanation.
59
+ Response: Correct answer: 4.0. Explanation: PEMDAS: 4 ÷ 2 = 2, then 2 + 2 = 4.
60
+
61
+ Prompt: Correct this incorrect math meme: '3² + 1 = 10.0'
62
+ Please reason step by step and provide the correct answer with an explanation.
63
+ Response: Correct answer: 10.0. Explanation: 3² = 9, then 9 + 1 = 10. Exponents first.
64
+
65
+ Prompt: Correct this incorrect math meme: '8 ÷ 4 × 2 = 1.0'
66
+ Please reason step by step and provide the correct answer with an explanation.
67
+ Response: Correct answer: 4.0. Explanation: PEMDAS: 8 ÷ 4 = 2, then 2 × 2 = 4, left-to-right.
68
+
69
+ Prompt: Correct this incorrect math meme: '5 × 2 - 3 = 13.0'
70
+ Please reason step by step and provide the correct answer with an explanation.
71
+ Response: Correct answer: 7.0. Explanation: PEMDAS: 5 × 2 = 10, then 10 - 3 = 7.
72
+
73
+ Prompt: Correct this incorrect math meme: '1 + 1 ÷ 2 = 1.0'
74
+ Please reason step by step and provide the correct answer with an explanation.
75
+ Response: Correct answer: 1.5. Explanation: PEMDAS: 1 ÷ 2 = 0.5, then 1 + 0.5 = 1.5.
76
+
77
+ Prompt: Correct this incorrect math meme: '9 ÷ 3(3) = 1.0'
78
+ Please reason step by step and provide the correct answer with an explanation.
79
+ Response: Correct answer: 9.0. Explanation: PEMDAS: 9 ÷ 3 × 3 = 3 × 3 = 9, left-to-right.
80
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch