mrbeliever commited on
Commit
0ad33fb
·
verified ·
1 Parent(s): c2c2086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -1,24 +1,19 @@
1
  import os
2
- import base64
3
  import gradio as gr
4
  from openai import OpenAI
5
 
6
  # Initialize the Nebius OpenAI client
7
  client = OpenAI(
8
  base_url="https://api.studio.nebius.ai/v1/",
9
- api_key=os.environ.get("NEBIUS_API_KEY"), # Replace with actual API key if not using environment variables
10
  )
11
 
12
- # Function to encode the image in Base64
13
- def encode_image_to_base64(image_path):
14
- with open(image_path, "rb") as image_file:
15
- return base64.b64encode(image_file.read()).decode("utf-8")
16
-
17
  # Function to interact with the Nebius OpenAI API
18
- def analyze_image(image):
19
  try:
20
- # Convert the image to Base64
21
- image_base64 = encode_image_to_base64(image)
 
22
 
23
  # API request
24
  response = client.chat.completions.create(
@@ -29,8 +24,8 @@ def analyze_image(image):
29
  "content": [
30
  {"type": "text", "text": "What’s in this image?"},
31
  {
32
- "type": "image_base64",
33
- "image_base64": image_base64,
34
  },
35
  ],
36
  }
@@ -52,11 +47,14 @@ def analyze_image(image):
52
  with gr.Blocks() as app:
53
  gr.Markdown("# Image Analysis with Nebius OpenAI")
54
  with gr.Row():
55
- image_input = gr.Image(type="filepath", label="Upload an Image")
 
 
 
56
  output_text = gr.Textbox(label="AI Response")
57
 
58
  analyze_button = gr.Button("Analyze Image")
59
- analyze_button.click(analyze_image, inputs=image_input, outputs=output_text)
60
 
61
  # Launch the Gradio app
62
  if __name__ == "__main__":
 
1
  import os
 
2
  import gradio as gr
3
  from openai import OpenAI
4
 
5
  # Initialize the Nebius OpenAI client
6
  client = OpenAI(
7
  base_url="https://api.studio.nebius.ai/v1/",
8
+ api_key=os.environ.get("NEBIUS_API_KEY"), # Replace with your API key if not using environment variables
9
  )
10
 
 
 
 
 
 
11
  # Function to interact with the Nebius OpenAI API
12
+ def analyze_image(image_path):
13
  try:
14
+ # Upload the image to a hosting service or convert its path to an accessible URL
15
+ # For simplicity, assume the user provides a valid image URL here
16
+ image_url = image_path # Replace this with a real image URL if needed
17
 
18
  # API request
19
  response = client.chat.completions.create(
 
24
  "content": [
25
  {"type": "text", "text": "What’s in this image?"},
26
  {
27
+ "type": "image_url",
28
+ "image_url": {"url": image_url}, # Use the proper field for image_url
29
  },
30
  ],
31
  }
 
47
  with gr.Blocks() as app:
48
  gr.Markdown("# Image Analysis with Nebius OpenAI")
49
  with gr.Row():
50
+ image_url_input = gr.Textbox(
51
+ label="Image URL",
52
+ placeholder="Enter a valid image URL for analysis",
53
+ )
54
  output_text = gr.Textbox(label="AI Response")
55
 
56
  analyze_button = gr.Button("Analyze Image")
57
+ analyze_button.click(analyze_image, inputs=image_url_input, outputs=output_text)
58
 
59
  # Launch the Gradio app
60
  if __name__ == "__main__":