Afeezee commited on
Commit
be56d43
·
verified ·
1 Parent(s): 642e5bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -38
app.py CHANGED
@@ -1,45 +1,43 @@
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)
21
- if response.status_code == 200:
22
- return response.json()["data"]["url"]
23
- else:
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"
31
  image.save(image_path)
32
 
33
- # Upload the image to imgbb
34
  image_url = upload_image_to_imgbb(image_path)
35
 
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": [
@@ -48,18 +46,23 @@ def analyze_image(image, instruction):
48
  ]
49
  }
50
  ],
51
- temperature=0.1,
52
- top_p=0.1,
53
- )
54
- print("API Response:", response)
55
- # Extract the content from the first choice
56
- analysis = response["choices"][0]["message"]["content"]
57
- return analysis.strip()
58
- except KeyError:
59
- return "Error: Response format from the API is unexpected."
 
 
 
 
 
60
  except Exception as e:
61
- return f"Error:"
62
-
63
  # Gradio interface
64
  iface = gr.Interface(
65
  fn=analyze_image,
@@ -70,9 +73,8 @@ iface = gr.Interface(
70
  outputs="text",
71
  title="Deep Image Analysis using LLM",
72
  description=(
73
- "Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision. "
74
- "You can upload and analyze as many pictures as possible with no restrictions. "
75
- "However, it's one at a time."
76
  ),
77
  live=False,
78
  )
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  from PIL import Image
5
 
6
+ # Set your API keys (replace with your actual keys or use environment variables)
7
+ nvidia_api_key = os.getenv("Vision") # NVIDIA API Key
8
+ imagebb_api_key = os.getenv("ImageAPI") # Imgbb API Key
9
 
10
+ # NVIDIA API Endpoint
11
+ invoke_url = "https://ai.api.nvidia.com/v1/gr/meta/llama-3.2-90b-vision-instruct/chat/completions"
 
12
 
13
  def upload_image_to_imgbb(image_path):
14
+ """Uploads an image to ImgBB and returns the URL."""
15
  url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}"
16
  with open(image_path, "rb") as image_file:
17
+ response = requests.post(url, files={"image": image_file})
18
+ if response.status_code == 200:
19
+ return response.json()["data"]["url"]
20
+ else:
21
+ raise ValueError(f"Image upload failed: {response.json()}")
 
22
 
23
  def analyze_image(image, instruction):
24
+ """Analyzes the image using NVIDIA’s Llama 3.2 Vision Instruct model based on the provided instruction."""
25
  try:
26
+ # Save image locally
27
  image_path = "uploaded_image.png"
28
  image.save(image_path)
29
 
30
+ # Upload image to ImgBB and get the URL
31
  image_url = upload_image_to_imgbb(image_path)
32
 
33
+ # NVIDIA API Request
34
+ headers = {
35
+ "Authorization": f"Bearer {nvidia_api_key}",
36
+ "Accept": "application/json"
37
+ }
38
+ payload = {
39
+ "model": "meta/llama-3.2-90b-vision-instruct",
40
+ "messages": [
41
  {
42
  "role": "user",
43
  "content": [
 
46
  ]
47
  }
48
  ],
49
+ "max_tokens": 512,
50
+ "temperature": 0.1,
51
+ "top_p": 0.1
52
+ }
53
+
54
+ response = requests.post(invoke_url, headers=headers, json=payload)
55
+ response_data = response.json()
56
+
57
+ # Extract the response
58
+ if "choices" in response_data:
59
+ return response_data["choices"][0]["message"]["content"]
60
+ else:
61
+ return f"Error in response: {response_data}"
62
+
63
  except Exception as e:
64
+ return f"Error: {str(e)}"
65
+
66
  # Gradio interface
67
  iface = gr.Interface(
68
  fn=analyze_image,
 
73
  outputs="text",
74
  title="Deep Image Analysis using LLM",
75
  description=(
76
+ "Upload an image and provide instructions to analyze it using Llama 3.2 90B Vision. "
77
+ "You can upload and analyze multiple pictures, but one at a time."
 
78
  ),
79
  live=False,
80
  )