Spaces:
Running
Running
File size: 1,501 Bytes
1da3de1 3f1e688 1da3de1 3f1e688 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 |
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.getenv("OPENAI_API_KEY"), # Use the environment variable
)
# Function to process input and return the response
def analyze_image(image_url):
# Construct the prompt with the image URL
prompt = f"What’s in this image? [image_url: {image_url}]"
try:
# Call the Nebius OpenAI API
response = client.chat.completions.create(
model="Qwen/Qwen2-VL-72B-Instruct",
messages=[
{"role": "user", "content": prompt} # Simplified messages format
],
max_tokens=300,
)
# Extract and return the AI's response
return response.choices[0].get("message", {}).get("content", "No response found.")
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Image Analysis with Nebius OpenAI")
image_url_input = gr.Textbox(
label="Image URL",
placeholder="Enter an image URL for analysis",
)
output = gr.Textbox(
label="AI Response",
placeholder="The description of the image will appear here.",
)
submit_button = gr.Button("Analyze Image")
submit_button.click(analyze_image, inputs=image_url_input, outputs=output)
# Launch the app
if __name__ == "__main__":
app.launch() |