Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| import requests | |
| from groq import Groq | |
| # Initialize Groq client | |
| visionkey = os.getenv("GroqVision") | |
| client = Groq(api_key=visionkey) | |
| # Your imgbb API key | |
| imagekey = os.getenv("ImageAPI") | |
| IMGBB_API_KEY = imagekey | |
| def upload_image_to_imgbb(image_path): | |
| """Uploads an image to imgbb and returns the URL.""" | |
| url = f"https://api.imgbb.com/1/upload?key={IMGBB_API_KEY}" | |
| with open(image_path, "rb") as image_file: | |
| payload = {"image": image_file.read()} | |
| response = requests.post(url, files=payload) | |
| if response.status_code == 200: | |
| return response.json()["data"]["url"] | |
| else: | |
| raise ValueError(f"Image upload failed: {response.json()}") | |
| def analyze_image(image, instruction): | |
| """Analyzes the image using Groq's Llama 3.2 based on the instruction provided.""" | |
| try: | |
| # Save the uploaded image locally | |
| image_path = "uploaded_image.png" | |
| image.save(image_path) | |
| # Upload the image to imgbb | |
| image_url = upload_image_to_imgbb(image_path) | |
| # Call the Groq API to analyze the image | |
| completion = client.chat.completions.create( | |
| model="llama-3.2-90b-vision-preview", | |
| messages=[{ | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": instruction}, | |
| {"type": "image_url", "image_url": {"url": image_url}} | |
| ] | |
| }], | |
| temperature=1, | |
| max_tokens=1024, | |
| top_p=1, | |
| stream=False, | |
| stop=None, | |
| ) | |
| # Extract and return the response | |
| analysis = completion.choices[0].message.content # Fix here to access content directly | |
| return analysis | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=analyze_image, | |
| inputs=[ | |
| gr.Image(type="pil", label="Upload Image"), | |
| gr.Textbox(label="Instruction", placeholder="Enter your analysis instruction here.", lines=2) | |
| ], | |
| outputs="text", | |
| title="Deep Image Analysis using LLM", | |
| description="Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision.", | |
| live=False, | |
| ) | |
| # Launch the app | |
| iface.launch() | |