Skip to content

Instantly share code, notes, and snippets.

@cryptonaidu
Forked from AIAnytime/fastapi.py
Created May 17, 2024 07:04
Show Gist options
  • Save cryptonaidu/352765fc845011401431b404f926c937 to your computer and use it in GitHub Desktop.
Save cryptonaidu/352765fc845011401431b404f926c937 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