Spaces:
Runtime error
Runtime error
Add application file
Browse files- app.py +218 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from groq import Groq
|
3 |
+
import base64
|
4 |
+
import os
|
5 |
+
import io
|
6 |
+
import json
|
7 |
+
from PIL import Image
|
8 |
+
import traceback
|
9 |
+
|
10 |
+
# Custom CSS for styling
|
11 |
+
custom_css = """
|
12 |
+
.center-aligned {
|
13 |
+
text-align: center !important;
|
14 |
+
color: #ff4081;
|
15 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
16 |
+
}
|
17 |
+
.input-background {
|
18 |
+
background-color: #B7E0FF !important;
|
19 |
+
padding: 15px !important;
|
20 |
+
border-radius: 10px !important;
|
21 |
+
margin: 0 !important;
|
22 |
+
}
|
23 |
+
.input-background textarea {
|
24 |
+
font-size: 18px !important;
|
25 |
+
background-color: #ffffff;
|
26 |
+
border: 1px solid #f0f8ff;
|
27 |
+
border-radius: 8px;
|
28 |
+
}
|
29 |
+
.image-background {
|
30 |
+
border-radius: 10px !important;
|
31 |
+
border: 2px solid #B7E0FF !important;
|
32 |
+
}
|
33 |
+
.lng-background {
|
34 |
+
background-color: #FFF5CD !important;
|
35 |
+
padding: 5px !important;
|
36 |
+
border-radius: 10px !important;
|
37 |
+
margin: 0 !important;
|
38 |
+
}
|
39 |
+
.api-background {
|
40 |
+
background-color: #FFCFB3 !important;
|
41 |
+
padding: 5px !important;
|
42 |
+
border-radius: 10px !important;
|
43 |
+
margin: 0 !important;
|
44 |
+
}
|
45 |
+
.script-background {
|
46 |
+
background-color: #FEF9D9 !important;
|
47 |
+
padding: 15px !important;
|
48 |
+
border-radius: 10px !important;
|
49 |
+
margin: 0 !important;
|
50 |
+
}
|
51 |
+
.script-background textarea {
|
52 |
+
font-size: 18px !important;
|
53 |
+
background-color: #ffffff;
|
54 |
+
border: 1px solid #f0f8ff;
|
55 |
+
border-radius: 8px;
|
56 |
+
}
|
57 |
+
.model-background {
|
58 |
+
background-color: #FFF4B5 !important;
|
59 |
+
padding: 5px !important;
|
60 |
+
border-radius: 10px !important;
|
61 |
+
margin: 0 !important;
|
62 |
+
}
|
63 |
+
.gen-button {
|
64 |
+
border-radius: 10px !important;
|
65 |
+
border: none !important;
|
66 |
+
background-color: #ff4081 !important;
|
67 |
+
color: white !important;
|
68 |
+
font-weight: bold !important;
|
69 |
+
transition: all 0.3s ease !important;
|
70 |
+
margin: 0 !important;
|
71 |
+
}
|
72 |
+
.gen-button:hover {
|
73 |
+
background-color: #f50057 !important;
|
74 |
+
transform: scale(1.05);
|
75 |
+
}
|
76 |
+
.clear-button {
|
77 |
+
color: white !important;
|
78 |
+
background-color: #000000 !important;
|
79 |
+
padding: 5px !important;
|
80 |
+
border-radius: 10px !important;
|
81 |
+
margin: 0 !important;
|
82 |
+
}
|
83 |
+
.clear-button:hover {
|
84 |
+
background-color: #000000 !important;
|
85 |
+
transform: scale(1.05);
|
86 |
+
}
|
87 |
+
"""
|
88 |
+
|
89 |
+
# List of available models
|
90 |
+
MODELS = [
|
91 |
+
"llama-3.2-90b-vision-preview",
|
92 |
+
"llama-3.2-11b-vision-preview",
|
93 |
+
"llava-v1.5-7b-4096-preview"
|
94 |
+
]
|
95 |
+
|
96 |
+
def compress_image(image, max_size=(800, 800), quality=95):
|
97 |
+
img = Image.open(image) if isinstance(image, str) else image
|
98 |
+
img.thumbnail(max_size)
|
99 |
+
buffered = io.BytesIO()
|
100 |
+
img.save(buffered, format="JPEG", quality=quality)
|
101 |
+
return buffered.getvalue()
|
102 |
+
|
103 |
+
def encode_image(image):
|
104 |
+
if isinstance(image, Image.Image):
|
105 |
+
buffered = io.BytesIO()
|
106 |
+
image.save(buffered, format="JPEG", quality=95)
|
107 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
108 |
+
else:
|
109 |
+
compressed = compress_image(image)
|
110 |
+
return base64.b64encode(compressed).decode('utf-8')
|
111 |
+
|
112 |
+
def create_client():
|
113 |
+
api_key = "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" # Directly using API key in the code - Hardcoding it for a purpose. I know I should keep it separate but I am not doing it on purpose<3.
|
114 |
+
return Groq(api_key=api_key)
|
115 |
+
|
116 |
+
def analyze_input(text_input, Quick_Input):
|
117 |
+
if Quick_Input == "Input Manually":
|
118 |
+
return text_input.strip()
|
119 |
+
elif Quick_Input == "Describe Image":
|
120 |
+
return "Take a close look at the image and describe it in as much detail as possible. Be sure to mention the main subject, the background, the colors used, the mood or feeling it evokes, and any specific elements that stand out."
|
121 |
+
elif Quick_Input == "Text in Image":
|
122 |
+
return "What does the text in this photo say?"
|
123 |
+
elif Quick_Input == "Image Reasoning":
|
124 |
+
return "Let's work this out in a step by step way to be sure we have the right answer. Deduce from the image and provide a quick answer."
|
125 |
+
else:
|
126 |
+
return text_input.strip()
|
127 |
+
|
128 |
+
def process_image_and_text(image, text_input, Quick_Input, model):
|
129 |
+
gr.Info("Image is being analyzed, please wait a moment...")
|
130 |
+
if Quick_Input == "Input Manually" and not text_input.strip():
|
131 |
+
return "Error: Please enter a question or select a quick input option!"
|
132 |
+
|
133 |
+
text_input = text_input.strip()
|
134 |
+
client = create_client()
|
135 |
+
base64_image = encode_image(image)
|
136 |
+
Input_Text = analyze_input(text_input, Quick_Input)
|
137 |
+
|
138 |
+
try:
|
139 |
+
chat_completion = client.chat.completions.create(
|
140 |
+
messages=[
|
141 |
+
{
|
142 |
+
"role": "user",
|
143 |
+
"content": [
|
144 |
+
{"type": "text", "text": Input_Text},
|
145 |
+
{
|
146 |
+
"type": "image_url",
|
147 |
+
"image_url": {
|
148 |
+
"url": f"data:image/jpeg;base64,{base64_image}",
|
149 |
+
},
|
150 |
+
},
|
151 |
+
],
|
152 |
+
}
|
153 |
+
],
|
154 |
+
model=model,
|
155 |
+
temperature=1,
|
156 |
+
)
|
157 |
+
gr.Info("Vision model has completed the response.")
|
158 |
+
response_content = chat_completion.choices[0].message.content.strip()
|
159 |
+
return response_content # No language conversion needed, only in English.
|
160 |
+
except Exception as e:
|
161 |
+
error_traceback = traceback.format_exc()
|
162 |
+
print(f"Error occurred: {error_traceback}")
|
163 |
+
return f"Error occurred: {str(e)}"
|
164 |
+
|
165 |
+
def update_textbox_based_on_quick_input(Quick_Input):
|
166 |
+
return analyze_input("", Quick_Input)
|
167 |
+
|
168 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as iface:
|
169 |
+
gr.Markdown("""
|
170 |
+
# Image Analysis and Reasoning - Large Vision Model
|
171 |
+
> ### **Project by Muhammad John Abbas - Supervised by: Dr. Mudassar Raza**
|
172 |
+
""", elem_classes="center-aligned")
|
173 |
+
|
174 |
+
with gr.Row():
|
175 |
+
with gr.Column(scale=1):
|
176 |
+
image_input = gr.Image(type="pil", label="Upload Image", elem_classes="image-background")
|
177 |
+
model_select = gr.Dropdown(choices=MODELS, label="Select Vision Model", value=MODELS[0], elem_classes="model-background")
|
178 |
+
clear_button = gr.Button("Clear Answer and Image", variant="secondary", elem_classes="clear-button")
|
179 |
+
gr.Markdown("""
|
180 |
+
### **※ Can analyze image and search for specific objects and events**
|
181 |
+
""", elem_classes="center-aligned")
|
182 |
+
|
183 |
+
with gr.Column(scale=1):
|
184 |
+
text_input = gr.Textbox(label="Enter Question", placeholder="Please enter your question...", autofocus=True, elem_classes="input-background", max_lines=5)
|
185 |
+
with gr.Row():
|
186 |
+
Quick_Input = gr.Dropdown(
|
187 |
+
choices=["Input Manually", "Describe Image", "Image Reasoning", "Text in Image"],
|
188 |
+
value="Input Manually",
|
189 |
+
label="Quick Input",
|
190 |
+
interactive=True,
|
191 |
+
elem_classes="lng-background"
|
192 |
+
)
|
193 |
+
submit_button = gr.Button("Submit", variant="primary", elem_classes="gen-button")
|
194 |
+
output = gr.Textbox(label="Vision Model Response", elem_classes="script-background", max_lines=40)
|
195 |
+
|
196 |
+
Quick_Input.change(
|
197 |
+
fn=update_textbox_based_on_quick_input,
|
198 |
+
inputs=[Quick_Input],
|
199 |
+
outputs=[text_input]
|
200 |
+
)
|
201 |
+
|
202 |
+
submit_button.click(
|
203 |
+
fn=process_image_and_text,
|
204 |
+
inputs=[image_input, text_input, Quick_Input, model_select],
|
205 |
+
outputs=[output]
|
206 |
+
)
|
207 |
+
|
208 |
+
def clear_outputs():
|
209 |
+
return None, None, ""
|
210 |
+
|
211 |
+
clear_button.click(
|
212 |
+
fn=clear_outputs,
|
213 |
+
inputs=[],
|
214 |
+
outputs=[image_input, text_input, output]
|
215 |
+
)
|
216 |
+
|
217 |
+
if __name__ == "__main__":
|
218 |
+
iface.launch(share=True, show_api=False)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
Pillow
|
3 |
+
groq
|