Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,29 +4,26 @@ import torch
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
model = PeftModel.from_pretrained(base_model, "waliaMuskaan011/calendar-event-extractor-smollm")
|
| 26 |
-
model.eval()
|
| 27 |
-
MODEL, TOKENIZER = model, tokenizer
|
| 28 |
-
print("Model loaded successfully!")
|
| 29 |
-
return MODEL, TOKENIZER
|
| 30 |
|
| 31 |
def extract_calendar_event(event_text):
|
| 32 |
"""Extract calendar information from natural language text."""
|
|
@@ -34,8 +31,6 @@ def extract_calendar_event(event_text):
|
|
| 34 |
if not event_text.strip():
|
| 35 |
return "Please enter some text describing a calendar event."
|
| 36 |
|
| 37 |
-
model, tokenizer = get_model()
|
| 38 |
-
|
| 39 |
# Build prompt
|
| 40 |
prompt = f"""Extract calendar fields from: "{event_text}".
|
| 41 |
Return ONLY valid JSON with keys [action,date,time,attendees,location,duration,recurrence,notes].
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
@gr.utils.async_cache
|
| 9 |
+
def load_model():
|
| 10 |
+
print("Loading model...")
|
| 11 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
"HuggingFaceTB/SmolLM-360M",
|
| 13 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
+
device_map="auto"
|
| 15 |
+
)
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM-360M")
|
| 17 |
+
if tokenizer.pad_token is None:
|
| 18 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
+
|
| 20 |
+
# Load LoRA adapters
|
| 21 |
+
model = PeftModel.from_pretrained(base_model, "waliaMuskaan011/calendar-event-extractor-smollm")
|
| 22 |
+
model.eval()
|
| 23 |
+
print("Model loaded successfully!")
|
| 24 |
+
return model, tokenizer
|
| 25 |
|
| 26 |
+
model, tokenizer = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def extract_calendar_event(event_text):
|
| 29 |
"""Extract calendar information from natural language text."""
|
|
|
|
| 31 |
if not event_text.strip():
|
| 32 |
return "Please enter some text describing a calendar event."
|
| 33 |
|
|
|
|
|
|
|
| 34 |
# Build prompt
|
| 35 |
prompt = f"""Extract calendar fields from: "{event_text}".
|
| 36 |
Return ONLY valid JSON with keys [action,date,time,attendees,location,duration,recurrence,notes].
|