LaibaIrfan commited on
Commit
f6635e8
·
verified ·
1 Parent(s): 7c3e543

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ from peft import PeftModel
6
+
7
+ from functools import lru_cache
8
+
9
+ # Define models
10
+ BASE_MODEL = "deepseek-ai/deepseek-math-7b-rl"
11
+ FINETUNED_MODEL = "LaibaIrfan/emoji_math"
12
+
13
+ # Load tokenizer and model
14
+ @lru_cache()
15
+ def load_model():
16
+ tokenizer = AutoTokenizer.from_pretrained(FINETUNED_MODEL)
17
+
18
+ base_model = AutoModelForCausalLM.from_pretrained(
19
+ BASE_MODEL,
20
+ torch_dtype=torch.float16, # Use float16 for efficiency
21
+ device_map="auto", # Auto-assign device (GPU if available)
22
+ load_in_8bit=True # Reduce memory usage (slightly increases inference time)
23
+ )
24
+
25
+ model = PeftModel.from_pretrained(
26
+ base_model,
27
+ FINETUNED_MODEL,
28
+ device_map="auto"
29
+ )
30
+
31
+ return tokenizer, model
32
+
33
+ # Load the model
34
+ tokenizer, model = load_model()
35
+
36
+ # Function to generate the result
37
+ def generate_result(incorrect_math):
38
+ input_text = f"Incorrect: {incorrect_math}\nCorrect:"
39
+
40
+ # Move input to GPU
41
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
42
+
43
+ # Generate output on GPU
44
+ output = model.generate(**inputs, max_length=200)
45
+
46
+ return tokenizer.decode(output[0], skip_special_tokens=True)
47
+
48
+ # Gradio Interface
49
+ iface = gr.Interface(
50
+ fn=generate_result,
51
+ inputs="text",
52
+ outputs="text",
53
+ title="Emoji Math Solver 🧮",
54
+ description="Enter an emoji-based math equation, and the model will generate the correct answer!"
55
+ )
56
+
57
+ iface.launch(debug=True, share=True, inline=True)
58
+
59
+ # Function to generate result
60
+ def generate_result(incorrect_math):
61
+ input_text = f"Incorrect: {incorrect_math}\nCorrect:"
62
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda") # Use GPU if available
63
+ output = model.generate(**inputs, max_length=200)
64
+ return tokenizer.decode(output[0], skip_special_tokens=True)
65
+
66
+ # Gradio Interface
67
+ iface = gr.Interface(
68
+ fn=generate_result,
69
+ inputs="text",
70
+ outputs="text",
71
+ title="Emoji Math Solver 🧮",
72
+ description="Enter an emoji-based math equation, and the model will generate the correct answer!"
73
+ )
74
+
75
+ iface.launch(share=True)