MalikAyaanAhmed1123 commited on
Commit
b966423
·
verified ·
1 Parent(s): 5ce76b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -29
app.py CHANGED
@@ -1,37 +1,26 @@
1
  import gradio as gr
2
- from model import MathSolverAI
3
- from dataset_utils import MathDataset
4
- from torch.utils.data import DataLoader
5
 
6
- solver = MathSolverAI()
 
 
7
 
8
- def solve_question(text_input, image_input):
9
- if image_input:
10
- answer = solver.solve_image(image_input.name)
11
- else:
12
- answer = solver.solve_text(text_input)
 
 
13
  return answer
14
 
15
- def train_model(file_input):
16
- dataset = MathDataset(file_input.name)
17
- loader = DataLoader(dataset, batch_size=2, shuffle=True)
18
- solver.train(loader, epochs=1)
19
- return "✅ Training completed!"
20
-
21
  with gr.Blocks() as demo:
22
  with gr.Row():
23
- with gr.Column(scale=3):
24
- text_input = gr.Textbox(label="Enter math question", placeholder="Type your math problem here")
25
- image_input = gr.Image(label="Or upload question image", type="filepath")
26
- solve_btn = gr.Button("Solve")
27
- output = gr.Textbox(label="Solution", interactive=False)
28
- solve_btn.click(solve_question, inputs=[text_input, image_input], outputs=output)
29
- with gr.Column(scale=1):
30
- with gr.Tab("Options"):
31
- train_btn = gr.File(label="Upload Dataset to Train", file_types=[".txt", ".json"])
32
- train_output = gr.Textbox(label="Training Status")
33
- train_btn.change(train_model, inputs=train_btn, outputs=train_output)
34
- with gr.Tab("Chat History"):
35
- chat_history_box = gr.Textbox(label="Chat history (login with Google to save)", interactive=False)
36
-
37
  demo.launch()
 
1
  import gradio as gr
2
+ from model_scratch import MathTransformer
3
+ from dataset_utils_scratch import CharTokenizer
4
+ import torch
5
 
6
+ # Load trained model (or new untrained)
7
+ tokenizer = CharTokenizer([""]) # initialize empty, replace with your trained tokenizer
8
+ model = MathTransformer(vocab_size=tokenizer.vocab_size).to("cpu")
9
 
10
+ def solve_question(text_input):
11
+ model.eval()
12
+ with torch.no_grad():
13
+ x = torch.tensor([tokenizer.encode(text_input)], dtype=torch.long)
14
+ out = model(x)
15
+ pred_idx = torch.argmax(out, dim=-1).squeeze().tolist()
16
+ answer = tokenizer.decode(pred_idx)
17
  return answer
18
 
 
 
 
 
 
 
19
  with gr.Blocks() as demo:
20
  with gr.Row():
21
+ text_input = gr.Textbox(label="Enter Math Question")
22
+ solve_btn = gr.Button("Solve")
23
+ output = gr.Textbox(label="Solution")
24
+ solve_btn.click(solve_question, inputs=text_input, outputs=output)
25
+
 
 
 
 
 
 
 
 
 
26
  demo.launch()