Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ A math reading coach for grades 3–8.
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
| 10 |
from peft import PeftModel
|
|
@@ -50,24 +51,14 @@ EXAMPLES = [
|
|
| 50 |
|
| 51 |
print(f"Loading base model: {BASE_MODEL_ID}")
|
| 52 |
|
| 53 |
-
if torch.cuda.is_available():
|
| 54 |
-
model_dtype = torch.float16
|
| 55 |
-
device_map = "auto"
|
| 56 |
-
print("GPU detected. Loading model in float16.")
|
| 57 |
-
else:
|
| 58 |
-
model_dtype = torch.float32
|
| 59 |
-
device_map = None
|
| 60 |
-
print("No GPU detected. CPU loading may be slow or require more memory.")
|
| 61 |
-
|
| 62 |
-
|
| 63 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 64 |
BASE_MODEL_ID,
|
| 65 |
-
torch_dtype=
|
| 66 |
-
device_map=device_map,
|
| 67 |
low_cpu_mem_usage=True,
|
| 68 |
**HUB_ARGS,
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
|
| 72 |
print(f"Loading Estella adapter: {ADAPTER_ID}")
|
| 73 |
|
|
@@ -77,21 +68,26 @@ model = PeftModel.from_pretrained(
|
|
| 77 |
**HUB_ARGS,
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 81 |
ADAPTER_ID,
|
| 82 |
**HUB_ARGS,
|
| 83 |
)
|
| 84 |
|
| 85 |
-
model.eval()
|
| 86 |
-
|
| 87 |
print("Estella is ready.")
|
| 88 |
|
| 89 |
|
|
|
|
| 90 |
def ask_estella(message: str, history: list) -> str:
|
| 91 |
"""Return one reading hint without solving the math problem."""
|
| 92 |
|
| 93 |
if not message or not message.strip():
|
| 94 |
-
return
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
messages = [
|
| 97 |
{
|
|
@@ -120,6 +116,7 @@ def ask_estella(message: str, history: list) -> str:
|
|
| 120 |
)
|
| 121 |
|
| 122 |
input_device = next(model.parameters()).device
|
|
|
|
| 123 |
inputs = {
|
| 124 |
key: value.to(input_device)
|
| 125 |
for key, value in inputs.items()
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
| 11 |
from peft import PeftModel
|
|
|
|
| 51 |
|
| 52 |
print(f"Loading base model: {BASE_MODEL_ID}")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 55 |
BASE_MODEL_ID,
|
| 56 |
+
torch_dtype=torch.float16,
|
|
|
|
| 57 |
low_cpu_mem_usage=True,
|
| 58 |
**HUB_ARGS,
|
| 59 |
)
|
| 60 |
|
| 61 |
+
base_model = base_model.to("cuda")
|
| 62 |
|
| 63 |
print(f"Loading Estella adapter: {ADAPTER_ID}")
|
| 64 |
|
|
|
|
| 68 |
**HUB_ARGS,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
model = model.to("cuda")
|
| 72 |
+
model.eval()
|
| 73 |
+
|
| 74 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 75 |
ADAPTER_ID,
|
| 76 |
**HUB_ARGS,
|
| 77 |
)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
print("Estella is ready.")
|
| 80 |
|
| 81 |
|
| 82 |
+
@spaces.GPU(duration=60)
|
| 83 |
def ask_estella(message: str, history: list) -> str:
|
| 84 |
"""Return one reading hint without solving the math problem."""
|
| 85 |
|
| 86 |
if not message or not message.strip():
|
| 87 |
+
return (
|
| 88 |
+
"Please type a math word problem and I’ll help explain "
|
| 89 |
+
"what it is asking!"
|
| 90 |
+
)
|
| 91 |
|
| 92 |
messages = [
|
| 93 |
{
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
input_device = next(model.parameters()).device
|
| 119 |
+
|
| 120 |
inputs = {
|
| 121 |
key: value.to(input_device)
|
| 122 |
for key, value in inputs.items()
|