Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
# Initialize Groq client
|
| 7 |
+
visionkey = os.getenv("GroqVision")
|
| 8 |
+
client = Groq(api_key=visionkey)
|
| 9 |
+
|
| 10 |
+
# Your imgbb API key
|
| 11 |
+
imagekey = os.getenv("ImageAPI")
|
| 12 |
+
IMGBB_API_KEY = imagekey
|
| 13 |
+
|
| 14 |
+
def upload_image_to_imgbb(image_path):
|
| 15 |
+
"""Uploads an image to imgbb and returns the URL."""
|
| 16 |
+
url = f"https://api.imgbb.com/1/upload?key={IMGBB_API_KEY}"
|
| 17 |
+
with open(image_path, "rb") as image_file:
|
| 18 |
+
payload = {"image": image_file.read()}
|
| 19 |
+
response = requests.post(url, files=payload)
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
return response.json()["data"]["url"]
|
| 22 |
+
else:
|
| 23 |
+
raise ValueError(f"Image upload failed: {response.json()}")
|
| 24 |
+
|
| 25 |
+
def analyze_image(image, instruction):
|
| 26 |
+
"""Analyzes the image using Groq's Llama 3.2 based on the instruction provided."""
|
| 27 |
+
try:
|
| 28 |
+
# Save the uploaded image locally
|
| 29 |
+
image_path = "uploaded_image.png"
|
| 30 |
+
image.save(image_path)
|
| 31 |
+
|
| 32 |
+
# Upload the image to imgbb
|
| 33 |
+
image_url = upload_image_to_imgbb(image_path)
|
| 34 |
+
|
| 35 |
+
# Call the Groq API to analyze the image
|
| 36 |
+
completion = client.chat.completions.create(
|
| 37 |
+
model="llama-3.2-90b-vision-preview",
|
| 38 |
+
messages=[{
|
| 39 |
+
"role": "user",
|
| 40 |
+
"content": [
|
| 41 |
+
{"type": "text", "text": instruction},
|
| 42 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 43 |
+
]
|
| 44 |
+
}],
|
| 45 |
+
temperature=1,
|
| 46 |
+
max_tokens=1024,
|
| 47 |
+
top_p=1,
|
| 48 |
+
stream=False,
|
| 49 |
+
stop=None,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Extract and return the response
|
| 53 |
+
analysis = completion.choices[0].message.content # Fix here to access content directly
|
| 54 |
+
return analysis
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error: {str(e)}"
|
| 57 |
+
|
| 58 |
+
# Gradio interface
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=analyze_image,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 63 |
+
gr.Textbox(label="Instruction", placeholder="Enter your analysis instruction here.", lines=2)
|
| 64 |
+
],
|
| 65 |
+
outputs="text",
|
| 66 |
+
title="Deep Image Analysis using LLM",
|
| 67 |
+
description="Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision.",
|
| 68 |
+
live=False,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Launch the app
|
| 72 |
+
iface.launch()
|