Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 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={
|
| 17 |
with open(image_path, "rb") as image_file:
|
| 18 |
files = {"image": image_file.read()}
|
| 19 |
response = requests.post(url, files=files)
|
|
@@ -23,7 +24,7 @@ def upload_image_to_imgbb(image_path):
|
|
| 23 |
raise ValueError(f"Image upload failed: {response.json()}")
|
| 24 |
|
| 25 |
def analyze_image(image, instruction):
|
| 26 |
-
"""Analyzes the image using
|
| 27 |
try:
|
| 28 |
# Save the uploaded image locally
|
| 29 |
image_path = "uploaded_image.png"
|
|
@@ -35,22 +36,20 @@ def analyze_image(image, instruction):
|
|
| 35 |
# Debug: Log the uploaded image URL
|
| 36 |
print(f"Uploaded Image URL: {image_url}")
|
| 37 |
|
| 38 |
-
# Call the
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
top_p=1,
|
| 52 |
-
stream=False,
|
| 53 |
-
stop=None,
|
| 54 |
)
|
| 55 |
|
| 56 |
# Extract and return the response
|
|
@@ -77,4 +76,4 @@ iface = gr.Interface(
|
|
| 77 |
)
|
| 78 |
|
| 79 |
# Launch the app
|
| 80 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import openai
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Set your API keys (replace with your actual keys or environment variables)
|
| 8 |
+
sambanova_api_key = os.getenv("Vision") # Sambanova API Key
|
| 9 |
+
imagebb_api_key = os.getenv("ImageAPI") # Your imgbb API key from environment variables
|
| 10 |
|
| 11 |
+
# Initialize Sambanova OpenAI client
|
| 12 |
+
openai.api_key = sambanova_api_key
|
| 13 |
+
openai.api_base = "https://api.sambanova.ai/v1"
|
| 14 |
|
| 15 |
def upload_image_to_imgbb(image_path):
|
| 16 |
"""Uploads an image to imgbb and returns the URL."""
|
| 17 |
+
url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}"
|
| 18 |
with open(image_path, "rb") as image_file:
|
| 19 |
files = {"image": image_file.read()}
|
| 20 |
response = requests.post(url, files=files)
|
|
|
|
| 24 |
raise ValueError(f"Image upload failed: {response.json()}")
|
| 25 |
|
| 26 |
def analyze_image(image, instruction):
|
| 27 |
+
"""Analyzes the image using Sambanova's Llama 3.2 Vision Instruct based on the provided instruction."""
|
| 28 |
try:
|
| 29 |
# Save the uploaded image locally
|
| 30 |
image_path = "uploaded_image.png"
|
|
|
|
| 36 |
# Debug: Log the uploaded image URL
|
| 37 |
print(f"Uploaded Image URL: {image_url}")
|
| 38 |
|
| 39 |
+
# Call the Sambanova API to analyze the image
|
| 40 |
+
completion = openai.ChatCompletion.create(
|
| 41 |
+
model="Llama-3.2-90B-Vision-Instruct",
|
| 42 |
+
messages=[
|
| 43 |
+
{
|
| 44 |
+
"role": "user",
|
| 45 |
+
"content": [
|
| 46 |
+
{"type": "text", "text": instruction},
|
| 47 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
temperature=0.1,
|
| 52 |
+
top_p=0.1,
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
# Extract and return the response
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
# Launch the app
|
| 79 |
+
iface.launch()
|