Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def chat(prompt):
|
| 11 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
if __name__ == "__main__":
|
| 18 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import spaces
|
| 5 |
|
| 6 |
+
MODEL_ID = "bmiller22000/xyntrai-mistral-2.5-7b-chat-nsfw"
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype=torch.float16,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
@spaces.GPU(duration=60) # cấp GPU tạm cho 1 phút
|
| 17 |
+
def chat_with_model(prompt):
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
MODEL_ID,
|
| 21 |
+
torch_dtype=torch.float16,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
|
|
|
|
| 26 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 27 |
+
output = model.generate(
|
| 28 |
+
**inputs,
|
| 29 |
+
max_new_tokens=256,
|
| 30 |
+
do_sample=True,
|
| 31 |
+
temperature=0.7,
|
| 32 |
+
top_p=0.9
|
| 33 |
+
)
|
| 34 |
+
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 35 |
+
return text
|
| 36 |
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=chat_with_model,
|
| 39 |
+
inputs=gr.Textbox(label="Prompt"),
|
| 40 |
+
outputs="text",
|
| 41 |
+
title="Xyntrai-Mistral 2.5 7B Chat (ZeroGPU)"
|
| 42 |
+
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
demo.launch()
|