maahin commited on
Commit
2379c94
·
verified ·
1 Parent(s): 2bf176d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -23,20 +23,41 @@ def load_model():
23
  processor, model = load_model()
24
 
25
  # Streamlit UI
26
- st.title("🖼️ Image Q&A using PaliGemma")
27
 
28
  uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
29
 
30
  if uploaded_file:
31
  image = Image.open(uploaded_file).convert("RGB")
32
- st.image(image, caption="Uploaded Image", use_column_width=True)
33
-
34
- question = st.text_input("Ask a question about the image:")
35
- if question:
36
- # Process the image and question
37
- inputs = processor(text=question, images=image, return_tensors="pt")
38
- with torch.no_grad():
39
- output = model.generate(**inputs)
40
-
41
- answer = processor.batch_decode(output, skip_special_tokens=True)[0]
42
- st.success(f"✅ Answer: {answer}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  processor, model = load_model()
24
 
25
  # Streamlit UI
26
+ st.title("🖼️ Image Understanding with PaliGemma")
27
 
28
  uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
29
 
30
  if uploaded_file:
31
  image = Image.open(uploaded_file).convert("RGB")
32
+ st.image(image, caption="Uploaded Image", use_container_width=True)
33
+
34
+ # User selects the task
35
+ task = st.selectbox(
36
+ "Select a task:",
37
+ ["Generate a caption", "Answer a question", "Detect objects", "Generate segmentation"]
38
+ )
39
+
40
+ # User input for question/prompt
41
+ prompt = st.text_area("Enter a prompt (e.g., 'Describe the image' or 'What objects are present?')")
42
+
43
+ if st.button("Run"):
44
+ if prompt:
45
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
46
+
47
+ with torch.no_grad():
48
+ output = model.generate(**inputs)
49
+
50
+ raw_output = processor.batch_decode(output, skip_special_tokens=False)[0]
51
+
52
+ # Handle different outputs
53
+ if task == "Generate a caption":
54
+ answer = raw_output
55
+ elif task == "Answer a question":
56
+ answer = raw_output
57
+ elif task == "Detect objects":
58
+ answer = f"Object bounding boxes: {raw_output}"
59
+ elif task == "Generate segmentation":
60
+ answer = f"Segmentation codes: {raw_output}"
61
+
62
+ st.success(f"✅ Result: {answer}")
63
+