Skip to content

Instantly share code, notes, and snippets.

@AIAnytime
Created May 14, 2024 09:16
Show Gist options
  • Save AIAnytime/f8e3ee3ce4488eceb70882c962e3585e to your computer and use it in GitHub Desktop.
Save AIAnytime/f8e3ee3ce4488eceb70882c962e3585e to your computer and use it in GitHub Desktop.
GPT-4o
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import OpenAI
from dotenv import load_dotenv
import os
load_dotenv()
app = FastAPI()
client = OpenAI()
class ImageRequest(BaseModel):
url: str
@app.post("/analyze-image/")
async def analyze_image(image_request: ImageRequest):
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {"url": image_request.url},
},
],
}
],
max_tokens=300,
)
return {"response": response.choices[0]}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment