Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
+
import gradio as gr
|
4 |
+
from groq import Groq
|
5 |
+
from PIL import Image
|
6 |
+
import requests
|
7 |
+
|
8 |
+
def encode_image(image):
|
9 |
+
buffered = io.BytesIO()
|
10 |
+
image.save(buffered, format="JPEG")
|
11 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
12 |
+
|
13 |
+
def analyze_image(image, prompt, api_key, is_url=False):
|
14 |
+
client = Groq(api_key=api_key)
|
15 |
+
|
16 |
+
if is_url:
|
17 |
+
image_content = {"type": "image_url", "image_url": {"url": image}}
|
18 |
+
else:
|
19 |
+
base64_image = encode_image(image)
|
20 |
+
image_content = {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
21 |
+
|
22 |
+
try:
|
23 |
+
chat_completion = client.chat.completions.create(
|
24 |
+
messages=[
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
+
"content": [
|
28 |
+
{"type": "text", "text": prompt},
|
29 |
+
image_content,
|
30 |
+
],
|
31 |
+
}
|
32 |
+
],
|
33 |
+
model="llava-v1.5-7b-4096-preview",
|
34 |
+
)
|
35 |
+
return chat_completion.choices[0].message.content
|
36 |
+
except Exception as e:
|
37 |
+
return f"Error: {str(e)}"
|
38 |
+
|
39 |
+
def check_content_safety(image_description, api_key):
|
40 |
+
client = Groq(api_key=api_key)
|
41 |
+
|
42 |
+
try:
|
43 |
+
chat_completion = client.chat.completions.create(
|
44 |
+
messages=[
|
45 |
+
{"role": "system", "content": "You are a content safety classifier. Analyze the given text and determine if it contains any unsafe or inappropriate content."},
|
46 |
+
{"role": "user", "content": f"Please analyze this image description for any unsafe or inappropriate content: {image_description}"}
|
47 |
+
],
|
48 |
+
model="llama-guard-3-8b",
|
49 |
+
)
|
50 |
+
return chat_completion.choices[0].message.content
|
51 |
+
except Exception as e:
|
52 |
+
return f"Error: {str(e)}"
|
53 |
+
|
54 |
+
def process_image(image, url, prompt, api_key):
|
55 |
+
if image is not None:
|
56 |
+
return analyze_image(image, prompt, api_key), check_content_safety(analyze_image(image, prompt, api_key), api_key)
|
57 |
+
elif url:
|
58 |
+
try:
|
59 |
+
response = requests.get(url)
|
60 |
+
image = Image.open(io.BytesIO(response.content))
|
61 |
+
return analyze_image(url, prompt, api_key, is_url=True), check_content_safety(analyze_image(url, prompt, api_key, is_url=True), api_key)
|
62 |
+
except:
|
63 |
+
return "Invalid image URL. Please provide a direct link to an image.", ""
|
64 |
+
else:
|
65 |
+
return "Please provide an image to analyze.", ""
|
66 |
+
|
67 |
+
def launch():
|
68 |
+
with gr.Blocks(
|
69 |
+
theme=gr.themes.Default(primary_hue="orange"),
|
70 |
+
css="""
|
71 |
+
#app-container { max-width: 1000px; margin: auto; padding: 10px; }
|
72 |
+
#title { text-align: center; margin-bottom: 10px; font-size: 24px; }
|
73 |
+
#groq-badge { text-align: center; margin-top: 10px; }
|
74 |
+
.gr-button { border-radius: 15px; }
|
75 |
+
.gr-input, .gr-box { border-radius: 10px; }
|
76 |
+
.gr-form { gap: 5px; }
|
77 |
+
.gr-block.gr-box { padding: 10px; }
|
78 |
+
.gr-paddle { height: auto; }
|
79 |
+
"""
|
80 |
+
) as demo:
|
81 |
+
with gr.Column(elem_id="app-container"):
|
82 |
+
gr.Markdown("# 🖼️ Groq x Gradio Image Analysis and Content Safety Check", elem_id="title")
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
api_key = gr.Textbox(label="Groq API Key:", type="password", scale=2)
|
86 |
+
prompt = gr.Textbox(
|
87 |
+
label="Image Analysis Prompt:",
|
88 |
+
value="Describe the image content.",
|
89 |
+
scale=3
|
90 |
+
)
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column(scale=1):
|
94 |
+
image_input = gr.Image(type="pil", label="Upload Image:", height=200, sources=["upload"])
|
95 |
+
with gr.Column(scale=1):
|
96 |
+
url_input = gr.Textbox(label="Or Paste Image URL:", lines=1)
|
97 |
+
analyze_button = gr.Button("🚀 Analyze Image", variant="primary")
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
with gr.Column():
|
101 |
+
analysis_output = gr.Textbox(label="Image Analysis with LlaVA 1.5 7B:", lines=6)
|
102 |
+
with gr.Column():
|
103 |
+
safety_output = gr.Textbox(label="Safety Check with Llama Guard 3 8B:", lines=6)
|
104 |
+
|
105 |
+
analyze_button.click(
|
106 |
+
fn=process_image,
|
107 |
+
inputs=[image_input, url_input, prompt, api_key],
|
108 |
+
outputs=[analysis_output, safety_output]
|
109 |
+
)
|
110 |
+
|
111 |
+
with gr.Row():
|
112 |
+
with gr.Column():
|
113 |
+
gr.HTML("""
|
114 |
+
<div id="groq-badge">
|
115 |
+
<div style="color: #f55036; font-weight: bold; font-size: 1em;">⚡ POWERED BY GROQ ⚡</div>
|
116 |
+
</div>
|
117 |
+
""")
|
118 |
+
with gr.Column():
|
119 |
+
gr.Markdown("""
|
120 |
+
**How to use this app:**
|
121 |
+
1. Enter your [Groq API Key](https://console.groq.com/keys) in the provided field.
|
122 |
+
2. Upload an image file or paste an image URL.
|
123 |
+
3. Use default prompt or enter custom prompt for image analysis.
|
124 |
+
4. Click "Analyze Image" to check for content safety.
|
125 |
+
""")
|
126 |
+
|
127 |
+
demo.launch()
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
launch()
|