Spaces:
Sleeping
Sleeping
Update app.py for better compatibility
Browse files
app.py
CHANGED
|
@@ -3,19 +3,26 @@ PlainEnglish-1B Gradio Demo App
|
|
| 3 |
Interactive text generation interface for HuggingFace Spaces and ModelScope Studio.
|
| 4 |
"""
|
| 5 |
|
|
|
|
| 6 |
import torch
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
torch_dtype=torch.float32,
|
|
|
|
| 16 |
trust_remote_code=True,
|
| 17 |
)
|
| 18 |
model.eval()
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def generate_text(
|
|
@@ -30,6 +37,8 @@ def generate_text(
|
|
| 30 |
return "Please enter a prompt."
|
| 31 |
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
| 33 |
|
| 34 |
with torch.no_grad():
|
| 35 |
outputs = model.generate(
|
|
|
|
| 3 |
Interactive text generation interface for HuggingFace Spaces and ModelScope Studio.
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
import torch
|
| 8 |
import gradio as gr
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
|
| 11 |
+
HF_MODEL_ID = "itriedcoding/PlainEnglish-1B"
|
| 12 |
+
MS_MODEL_ID = "NeuraAI/PlainEnglish-1B"
|
| 13 |
|
| 14 |
+
MODEL_ID = MS_MODEL_ID if os.environ.get("MODELSCOPE_API_TOKEN") else HF_MODEL_ID
|
| 15 |
+
|
| 16 |
+
print(f"Loading model from: {MODEL_ID}")
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
MODEL_ID,
|
| 20 |
torch_dtype=torch.float32,
|
| 21 |
+
device_map="auto",
|
| 22 |
trust_remote_code=True,
|
| 23 |
)
|
| 24 |
model.eval()
|
| 25 |
+
print("Model loaded successfully!")
|
| 26 |
|
| 27 |
|
| 28 |
def generate_text(
|
|
|
|
| 37 |
return "Please enter a prompt."
|
| 38 |
|
| 39 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 40 |
+
if torch.cuda.is_available():
|
| 41 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 42 |
|
| 43 |
with torch.no_grad():
|
| 44 |
outputs = model.generate(
|