Spaces:
Running
Running
import os | |
import gradio as gr | |
from openai import OpenAI | |
# Initialize the Nebius OpenAI client | |
client = OpenAI( | |
base_url="https://api.studio.nebius.ai/v1/", | |
api_key=os.environ.get("NEBIUS_API_KEY"), # Replace with your API key if not using environment variables | |
) | |
# Function to interact with the Nebius OpenAI API | |
def analyze_image(image_path): | |
try: | |
# Upload the image to a hosting service or convert its path to an accessible URL | |
# For simplicity, assume the user provides a valid image URL here | |
image_url = image_path # Replace this with a real image URL if needed | |
# API request | |
response = client.chat.completions.create( | |
model="Qwen/Qwen2-VL-72B-Instruct", # Ensure this model name is correct | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": "What’s in this image?"}, | |
{ | |
"type": "image_url", | |
"image_url": {"url": image_url}, # Use the proper field for image_url | |
}, | |
], | |
} | |
], | |
max_tokens=300, | |
) | |
# Extract the AI's response | |
if response.choices and "message" in response.choices[0]: | |
return response.choices[0]["message"]["content"] | |
else: | |
return "No valid response received from the API." | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Gradio interface for uploading an image | |
with gr.Blocks() as app: | |
gr.Markdown("# Image Analysis with Nebius OpenAI") | |
with gr.Row(): | |
image_url_input = gr.Textbox( | |
label="Image URL", | |
placeholder="Enter a valid image URL for analysis", | |
) | |
output_text = gr.Textbox(label="AI Response") | |
analyze_button = gr.Button("Analyze Image") | |
analyze_button.click(analyze_image, inputs=image_url_input, outputs=output_text) | |
# Launch the Gradio app | |
if __name__ == "__main__": | |
app.launch() |