Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Replace with your model repository ID
|
| 7 |
+
model_repo_id = "ubiodee/Plutuslearn-Llama-3.2-3B-Instruct"
|
| 8 |
+
|
| 9 |
+
# Load the tokenizer
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_repo_id)
|
| 11 |
+
|
| 12 |
+
# Load the base model and apply the PEFT adapter
|
| 13 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
"meta-llama/Llama-3.2-3B-Instruct",
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
model = PeftModel.from_pretrained(base_model, model_repo_id)
|
| 19 |
+
|
| 20 |
+
# Define the prediction function
|
| 21 |
+
def predict(text):
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt").to("cuda")
|
| 23 |
+
outputs = model.generate(**inputs, max_length=100) # Adjust parameters as needed
|
| 24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs=gr.Textbox(label="Input Text"),
|
| 30 |
+
outputs=gr.Textbox(label="Model Output"),
|
| 31 |
+
title="My Model Demo",
|
| 32 |
+
description="Test the fine-tuned model hosted on Hugging Face."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the app
|
| 36 |
+
demo.launch()
|