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 AutoProcessor, LlavaForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Load the Pixtral model and processor
|
8 |
+
model_id = "mistral-community/pixtral-12b"
|
9 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
10 |
+
model = LlavaForConditionalGeneration.from_pretrained(model_id).to("cuda")
|
11 |
+
|
12 |
+
def generate_text(input_text, image_url):
|
13 |
+
# Load image
|
14 |
+
response = requests.get(image_url)
|
15 |
+
image = Image.open(BytesIO(response.content))
|
16 |
+
|
17 |
+
# Prepare inputs
|
18 |
+
inputs = processor(text=input_text, images=image, return_tensors="pt").to("cuda")
|
19 |
+
|
20 |
+
# Generate output
|
21 |
+
outputs = model.generate(**inputs)
|
22 |
+
generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
23 |
+
|
24 |
+
return generated_text
|
25 |
+
|
26 |
+
# Create Gradio interface
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=generate_text,
|
29 |
+
inputs=[gr.Textbox(label="Enter your text here"), gr.Textbox(label="Enter image URL")],
|
30 |
+
outputs=gr.Textbox(label="Generated Text"),
|
31 |
+
title="Pixtral Model Interaction",
|
32 |
+
description="Interact with the Pixtral model using text and image inputs."
|
33 |
+
)
|
34 |
+
|
35 |
+
# Launch the interface
|
36 |
+
iface.launch()
|