-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update for 3.1 native tool use support
- Loading branch information
1 parent
d57c6a7
commit 7caf0fa
Showing
9 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import gradio as gr | ||
import base64 | ||
from PIL import Image | ||
import io | ||
import requests | ||
from groq import Groq | ||
|
||
# Groq API settings | ||
LLAVA_MODEL = "llava-v1.5-7b-4096-preview" | ||
LLAMA_GUARD_MODEL = "llama-guard-3-8b" | ||
|
||
def encode_image(image): | ||
buffered = io.BytesIO() | ||
image.save(buffered, format="JPEG") | ||
return base64.b64encode(buffered.getvalue()).decode('utf-8') | ||
|
||
def analyze_image(image, prompt, api_key, is_url=False): | ||
client = Groq(api_key=api_key) | ||
|
||
if is_url: | ||
image_content = {"type": "image_url", "image_url": {"url": image}} | ||
else: | ||
base64_image = encode_image(image) | ||
image_content = {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}} | ||
|
||
try: | ||
chat_completion = client.chat.completions.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "text", "text": prompt}, | ||
image_content, | ||
], | ||
} | ||
], | ||
model=LLAVA_MODEL, | ||
) | ||
return chat_completion.choices[0].message.content | ||
except Exception as e: | ||
return f"Error: {str(e)}" | ||
|
||
def check_content_safety(image_description, api_key): | ||
client = Groq(api_key=api_key) | ||
|
||
try: | ||
chat_completion = client.chat.completions.create( | ||
messages=[ | ||
{"role": "system", "content": "You are a content safety classifier. Analyze the given text and determine if it contains any unsafe or inappropriate content."}, | ||
{"role": "user", "content": f"Please analyze this image description for any unsafe or inappropriate content: {image_description}"} | ||
], | ||
model=LLAMA_GUARD_MODEL, | ||
) | ||
return chat_completion.choices[0].message.content | ||
except Exception as e: | ||
return f"Error: {str(e)}" | ||
|
||
def process_image(image, url, prompt, api_key): | ||
if image is not None: | ||
return analyze_image(image, prompt, api_key), check_content_safety(analyze_image(image, prompt, api_key), api_key) | ||
elif url: | ||
try: | ||
response = requests.get(url) | ||
image = Image.open(io.BytesIO(response.content)) | ||
return analyze_image(url, prompt, api_key, is_url=True), check_content_safety(analyze_image(url, prompt, api_key, is_url=True), api_key) | ||
except: | ||
return "Invalid image URL. Please provide a direct link to an image.", "" | ||
else: | ||
return "Please provide an image to analyze.", "" | ||
|
||
def launch(): | ||
with gr.Blocks() as demo: | ||
gr.Markdown("# Image Analysis with Groq API") | ||
|
||
with gr.Row(): | ||
with gr.Column(): | ||
api_key = gr.Textbox(label="Enter your Groq API Key:", type="password") | ||
image_input = gr.Image(type="pil", label="Upload Image") | ||
url_input = gr.Textbox(label="Or paste image URL") | ||
prompt = gr.Textbox(label="Enter a prompt for image analysis:", value="Describe the contents of this image in detail.") | ||
analyze_button = gr.Button("Analyze Image") | ||
|
||
with gr.Column(): | ||
analysis_output = gr.Textbox(label="Analysis Result") | ||
safety_output = gr.Textbox(label="Content Safety Check") | ||
|
||
analyze_button.click( | ||
fn=process_image, | ||
inputs=[image_input, url_input, prompt, api_key], | ||
outputs=[analysis_output, safety_output] | ||
) | ||
|
||
demo.launch() | ||
|
||
if __name__ == "__main__": | ||
launch() |
1 change: 1 addition & 0 deletions
1
tutorials/llamaguard/image-analysis-app/.next/server/app-paths-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
tutorials/llamaguard/image-analysis-app/.next/server/interception-route-rewrite-manifest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
self.__INTERCEPTION_ROUTE_REWRITE_MANIFEST="[]" |
6 changes: 6 additions & 0 deletions
6
tutorials/llamaguard/image-analysis-app/.next/server/middleware-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"version": 3, | ||
"middleware": {}, | ||
"functions": {}, | ||
"sortedMiddleware": [] | ||
} |
1 change: 1 addition & 0 deletions
1
tutorials/llamaguard/image-analysis-app/.next/server/pages-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
tutorials/llamaguard/image-analysis-app/.next/server/server-reference-manifest.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
tutorials/llamaguard/image-analysis-app/.next/server/server-reference-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"node": {}, | ||
"edge": {}, | ||
"encryptionKey": "RsP/1g1afXOf67tygDO4r89ef0cmb0oL2lgcE0BofgA=" | ||
} |
1 change: 1 addition & 0 deletions
1
tutorials/llamaguard/image-analysis-app/.next/types/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "module"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters