Spaces:
Running
Running
File size: 2,095 Bytes
1da3de1 a44a876 0ad33fb 1da3de1 c2c2086 0ad33fb 3f1e688 0ad33fb c2c2086 3f1e688 c2c2086 3f1e688 c2c2086 0ad33fb c2c2086 3f1e688 c2c2086 c8ade47 3f1e688 1da3de1 c2c2086 1da3de1 c2c2086 0ad33fb c2c2086 0ad33fb c2c2086 1da3de1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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() |