Spaces:
Build error
Build error
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def solve_question(text_input
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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()
|