Created
May 14, 2024 09:16
-
-
Save AIAnytime/f8e3ee3ce4488eceb70882c962e3585e to your computer and use it in GitHub Desktop.
GPT-4o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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