Spaces:
Runtime error
Runtime error
Setup Gradio app with Unsloth
Browse files- app.py +35 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from unsloth import FastVisionModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Load the Unsloth FastVisionModel
|
| 9 |
+
model, tokenizer = FastVisionModel.from_pretrained(
|
| 10 |
+
model_name = "unsloth/mllama-vision",
|
| 11 |
+
max_seq_length = 2048,
|
| 12 |
+
dtype = torch.float16,
|
| 13 |
+
load_in_4bit = True,
|
| 14 |
+
).to(device)
|
| 15 |
+
|
| 16 |
+
def analyze_image(image, prompt):
|
| 17 |
+
inputs = tokenizer(
|
| 18 |
+
image,
|
| 19 |
+
prompt,
|
| 20 |
+
return_tensors="pt"
|
| 21 |
+
).to(device)
|
| 22 |
+
|
| 23 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 24 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=analyze_image,
|
| 29 |
+
inputs=[gr.Image(type="pil"), "text"],
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="FloorPlan Vision AI",
|
| 32 |
+
description="Upload an image and enter your prompt for AI analysis."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
unsloth
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
sentencepiece
|
| 5 |
+
datasets
|
| 6 |
+
Pillow
|