Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,28 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 2 |
-
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Load model
|
| 20 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
-
"m-a-p/YuE-s1-7B-anneal-en-cot",
|
| 22 |
-
quantization_config=quant_config,
|
| 23 |
-
device_map="cpu", # force CPU usage
|
| 24 |
-
torch_dtype=torch.float16,
|
| 25 |
-
attn_implementation="eager",
|
| 26 |
-
trust_remote_code=True
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
model.eval()
|
| 30 |
-
print("β
Model loaded (8-bit, ~6β8 GB RAM).")
|
| 31 |
-
|
| 32 |
-
# --- Define generation function ---
|
| 33 |
-
def generate_text(prompt, max_tokens=200):
|
| 34 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 35 |
-
|
| 36 |
-
outputs = model.generate(**inputs, max_new_tokens=int(max_tokens))
|
| 37 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
|
| 39 |
-
# --- Gradio Interface ---
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=generate_text,
|
| 42 |
-
inputs=
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
outputs=gr.Textbox(label="Generated Text"),
|
| 47 |
-
title="YuE-s1-7B Text Generator (8-bit CPU)",
|
| 48 |
-
description="A lightweight quantized version running on CPU (~6β8 GB RAM)."
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
MODEL_NAME = "m-a-p/YuE-s1-7B-anneal-en-cot"
|
| 6 |
+
|
| 7 |
+
def generate_text(prompt):
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL_NAME,
|
| 10 |
+
torch_dtype=torch.float32,
|
| 11 |
+
attn_implementation="eager"
|
| 12 |
+
)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 16 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
|
|
|
| 17 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 18 |
|
|
|
|
| 19 |
demo = gr.Interface(
|
| 20 |
fn=generate_text,
|
| 21 |
+
inputs=gr.Textbox(lines=3, label="Input Prompt"),
|
| 22 |
+
outputs="text",
|
| 23 |
+
title="YuE Text Generator",
|
| 24 |
+
description="A Hugging Face Space for generating text using YuE 7B model."
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
if __name__ == "__main__":
|
| 28 |
+
demo.launch()
|