Spaces:
Running
on
Zero
Running
on
Zero
mjavaid
commited on
Commit
·
313270e
1
Parent(s):
30b24ff
first commit
Browse files
app.py
CHANGED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the Gemma 3 pipeline.
|
7 |
+
# Gemma 3 is a multimodal model that accepts text and image inputs.
|
8 |
+
pipe = pipeline(
|
9 |
+
"image-text-to-text",
|
10 |
+
model="google/gemma-3-4b-it",
|
11 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
12 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else None
|
13 |
+
)
|
14 |
+
@spaces.GPU
|
15 |
+
def generate_response(user_text, user_image, history):
|
16 |
+
"""
|
17 |
+
This function takes the user's text and an optional image,
|
18 |
+
creates a message list (including a system message), and then
|
19 |
+
uses the Gemma 3 pipeline to generate a response.
|
20 |
+
"""
|
21 |
+
# Start with a system prompt.
|
22 |
+
messages = [
|
23 |
+
{
|
24 |
+
"role": "system",
|
25 |
+
"content": [{"type": "text", "text": "You are a helpful assistant."}]
|
26 |
+
}
|
27 |
+
]
|
28 |
+
# Build user content from the provided image and/or text.
|
29 |
+
user_content = []
|
30 |
+
if user_image is not None:
|
31 |
+
# The image input is a PIL image.
|
32 |
+
user_content.append({"type": "image", "image": user_image})
|
33 |
+
if user_text:
|
34 |
+
user_content.append({"type": "text", "text": user_text})
|
35 |
+
messages.append({"role": "user", "content": user_content})
|
36 |
+
|
37 |
+
# Generate output using the model pipeline.
|
38 |
+
output = pipe(text=messages, max_new_tokens=200)
|
39 |
+
# Extract the generated text.
|
40 |
+
# (This extraction follows the code snippet provided in the model card.)
|
41 |
+
response = output[0][0]["generated_text"][-1]["content"]
|
42 |
+
|
43 |
+
# Append the new exchange to the conversation history.
|
44 |
+
history.append((user_text, response))
|
45 |
+
return history, history
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# Gemma 3 Chat Interface")
|
49 |
+
gr.Markdown(
|
50 |
+
"This interface lets you chat with the Gemma 3 model. "
|
51 |
+
"You can type a message and optionally attach an image."
|
52 |
+
)
|
53 |
+
chatbot = gr.Chatbot()
|
54 |
+
with gr.Row():
|
55 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message here...", container=False)
|
56 |
+
img = gr.Image(source="upload", tool="editor", type="pil", label="Attach an image (optional)")
|
57 |
+
state = gr.State([])
|
58 |
+
|
59 |
+
# When the user submits text (and possibly an image), call generate_response.
|
60 |
+
txt.submit(generate_response, inputs=[txt, img, state], outputs=[chatbot, state])
|
61 |
+
demo.launch()
|