Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,77 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
# import spaces
|
4 |
|
5 |
|
6 |
-
"""
|
7 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
-
"""
|
9 |
-
client = InferenceClient("meta-llama/Llama-3.2-90B-Vision-Instruct")
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
if
|
25 |
-
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
stream=True,
|
35 |
-
temperature=temperature,
|
36 |
-
top_p=top_p,
|
37 |
-
):
|
38 |
-
token = message.choices[0].delta.content
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
5 |
import gradio as gr
|
6 |
+
import spaces
|
|
|
7 |
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
class VisionInstructChat:
|
11 |
+
def __init__(self):
|
12 |
+
# Initialize the model and processor
|
13 |
+
self.model_id = "meta-llama/Llama-3.2-90B-Vision-Instruct"
|
14 |
+
self.model = MllamaForConditionalGeneration.from_pretrained(
|
15 |
+
self.model_id,
|
16 |
+
torch_dtype=torch.bfloat16,
|
17 |
+
device_map="auto",
|
18 |
+
)
|
19 |
+
self.processor = AutoProcessor.from_pretrained(self.model_id)
|
20 |
|
21 |
+
# Method to handle the model's response to an image and text input
|
22 |
+
@spaces
|
23 |
+
def chat_with_model(self, history, image, user_text):
|
24 |
+
if image is None or not user_text.strip():
|
25 |
+
return history + [["Please upload an image and enter a prompt."]]
|
26 |
|
27 |
+
# Prepare messages for the model
|
28 |
+
messages = [
|
29 |
+
{"role": "user", "content": [
|
30 |
+
{"type": "image"},
|
31 |
+
{"type": "text", "text": user_text}
|
32 |
+
]}
|
33 |
+
]
|
34 |
+
input_text = self.processor.apply_chat_template(messages, add_generation_prompt=True)
|
35 |
+
inputs = self.processor(image, input_text, return_tensors="pt").to(self.model.device)
|
36 |
|
37 |
+
# Generate response
|
38 |
+
output = self.model.generate(**inputs, max_new_tokens=100)
|
39 |
+
response = self.processor.decode(output[0])
|
40 |
|
41 |
+
# Add user prompt and model response to chat history
|
42 |
+
history.append([user_text, response])
|
43 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Method to reset the chat history
|
46 |
+
@spaces
|
47 |
+
def reset_chat(self):
|
48 |
+
return []
|
49 |
|
50 |
+
# Method to create the Gradio interface
|
51 |
+
def launch_interface(self):
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("### Chat with Vision-Instruct Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Chat history
|
56 |
+
chat_history = gr.Chatbot(label="Chat History")
|
57 |
|
58 |
+
# Inputs: Image and Text
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column(scale=3):
|
61 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
62 |
+
with gr.Column(scale=7):
|
63 |
+
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Prompt")
|
64 |
+
|
65 |
+
# Submit and Clear buttons
|
66 |
+
submit_button = gr.Button("Send")
|
67 |
+
clear_button = gr.Button("Clear Chat")
|
68 |
+
|
69 |
+
# Button actions
|
70 |
+
submit_button.click(fn=self.chat_with_model, inputs=[chat_history, image_input, user_input], outputs=chat_history)
|
71 |
+
clear_button.click(fn=self.reset_chat, outputs=chat_history)
|
72 |
+
|
73 |
+
demo.launch()
|
74 |
+
|
75 |
+
# Create an instance of the class and launch the interface
|
76 |
+
chat_app = VisionInstructChat()
|
77 |
+
chat_app.launch_interface()
|